我想要创建几个向量的组合
vector<int> Vec1;
Vec1.push_back(1);
Vec1.push_back(2);
Vec1.push_back(3);
vector <int> Vec2;
Vec2.push_back(5);
Vec2.push_back(6);
Vec2.push_back(7);
Vec2.push_back(8);
vector <int> Vec3;
Vec3.push_back(11);
Vec3.push_back(12);
Vec3.push_back(13);
vector<vector<int>> xt;
xt.push_back(Vec1);
xt.push_back(Vec2);
xt.push_back(Vec3);结果应该是
1 5 11
1 5 12
..。
3 8 13
对于给定数量的向量,我可以使用nest循环。然而,我正在尝试编写一个函数,比如void printAll(ConstVector&xt),我确实找到了类似的东西
Howto create combinations of several vectors without hardcoding loops in C++?
但我很难把它转换成int。请给我一些建议。
发布于 2018-01-15 21:16:33
您可以简单地使用std::to_string()将所建议的算法转换为int。
void printAll(const std::vector<std::vector<int> > &allVecs, size_t vecIndex, std::string strSoFar)
{
if (vecIndex >= allVecs.size())
{
std::cout << strSoFar << std::endl;
return;
}
for (size_t i=0; i<allVecs[vecIndex].size(); i++)
printAll(allVecs, vecIndex+1, strSoFar+std::to_string(allVecs[vecIndex][i]));
}https://stackoverflow.com/questions/48270565
复制相似问题