我正在尝试使用std:copy将3D数组复制到vector。
int myint[3][3][3] = { { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } },
{ { 11,12, 13 }, { 14, 15, 16 }, {17, 18, 19 } },
{ { 21, 22, 23 }, { 24, 25, 26 }, { 27, 28, 29 } } };
int favint[3][3][3];
我可以如下将myint复制到favint
std::copy(&myint[0][0][0],
&myint[0][0][0] + (sizeof(myint) / sizeof(*myint)) * (sizeof(*myint) / sizeof(**myint))*(sizeof(**myint) / sizeof(***myint)),
&favint[0][0][0]);
我可以如下将myint
复制到myvector
:
vector<vector<vector<int> > > myvector;
for (int k = 0; k<sizeof(myint) / sizeof(*myint); k++)
{
myvector.push_back(vector<vector<int> >());
for (int i = 0; i<sizeof(*myint) / sizeof(**myint); i++)
{
myvector[k].push_back(vector<int>());
for (int j = 0; j<sizeof(**myint) / sizeof(***myint); j++)
{
myvector[k][i].push_back(myint[k][i][j]);
}
}
}
使用std :: copy将3D数组复制到3D数组很简单。如何使用std :: copy将myint复制到myvector?还有其他简单的方法吗?
答案 0 :(得分:1)
您可能会做类似的事情:
std::vector<int> to_vector(const int (&a)[3])
{
return {std::begin(a), std::end(a)};
}
std::vector<std::vector<int>> to_vector(const int (&a)[3][3])
{
return {to_vector(a[0]), to_vector(a[1]), to_vector(a[2])};
}
std::vector<std::vector<std::vector<int>>> to_vector(const int (&a)[3][3][3])
{
return {to_vector(a[0]), to_vector(a[1]), to_vector(a[2])};
}
对于通用(但编译时)值,template
可能会有所帮助:
template <std::size_t N>
std::vector<int> to_vector(const int (&a)[N])
{
return {std::begin(a), std::end(a)};
}
template <std::size_t N1, std::size_t N2>
auto to_vector(const int (&a)[N1][N2])
-> std::vector<std::vector<int>>
{
std::vector<std::vector<int>> res;
res.reserve(N1);
for (const auto& inner_a : a) {
res.push_back(to_vector(inner_a));
}
return res;
}
template <std::size_t N1, std::size_t N2, std::size_t N3>
auto to_vector(const int (&a)[N1][N2][N3])
-> std::vector<std::vector<std::vector<int>>>
{
std::vector<std::vector<std::vector<int>>> res;
res.reserve(N1);
for (const auto& inner_a : a) {
res.push_back(to_vector(inner_a));
}
return res;
}
答案 1 :(得分:-1)
只需使用import java.util.Scanner;
public class RomanNumerals {
public static String convert(int n) {
String roman = "";
if ((n >= 0) && (n <= 3999)) {
int thousand = (n / 1000);
int hundred = (n % 1000) / 100;
int tens = (n % 100) / 10;
int units = (n % 10);
if (thousand >= 1) {
if (thousand == 1)
roman += "M";
else if (thousand == 2)
roman += "MM";
else roman += "MMM";
}
if (hundred >= 0) {
if (hundred == 0)
roman += "";
else if (hundred == 1)
roman += "C";
else if (hundred == 2)
roman += "CC";
else if (hundred == 3)
roman += "CCC";
else if (hundred == 4)
roman += "CD";
else if (hundred == 5)
roman += "D";
else if (hundred == 6)
roman += "DC";
else if (hundred == 7)
roman += "DCC";
else if (hundred == 8)
roman += "DCCC";
else
roman += "CM";
}
if (tens >= 0) {
if (tens == 0)
roman += "";
else if (tens == 1)
roman += "X";
else if (tens == 2)
roman += "XX";
else if (tens == 3)
roman += "XXX";
else if (tens == 4)
roman += "XL";
else if (tens == 5)
roman += "L";
else if (tens == 6)
roman += "LX";
else if (tens == 7)
roman += "LXX";
else if (tens == 8)
roman += "LXXX";
else
roman += "XC";
}
if (units >= 0) {
if (units == 0)
roman += "";
else if (units == 1)
roman += "I";
else if (units == 2)
roman += "II";
else if (units == 3)
roman += "III";
else if (units == 4)
roman += "IV";
else if (units == 5)
roman += "V";
else if (units == 6)
roman += "VI";
else if (units == 7)
roman += "VII";
else if (units == 8)
roman += "VIII";
else
roman += "IX";
}
} else {
roman = "Number cannot be converted as it exceeds over 3999";
}
return roman;
std::back_inserter