C++ 在运行时定义循环

C++ 在运行时定义循环,c++,algorithm,nested-loops,C++,Algorithm,Nested Loops,考虑一下这个数组,例如 #include <iostream> #include <vector> int main(int argc, char* argv[]) { std::vector<char> ls(3); ls[0] = 'a'; ls[1] = 'b'; ls[2] = 'c'; std::vector<char> us(3); us[0] = 'A'; us[1] = 'B'; us[2] = 'C'; std

考虑一下这个数组,例如

#include <iostream>
#include <vector>

int main(int argc, char* argv[])
{
   std::vector<char> ls(3); ls[0] = 'a'; ls[1] = 'b'; ls[2] = 'c';
   std::vector<char> us(3); us[0] = 'A'; us[1] = 'B'; us[2] = 'C';
   std::vector<int> ns(3);  ns[0] = 1;   ns[1] = 2;   ns[2] = 3;

   std::vector<char>::const_iterator lIt;
   std::vector<char>::const_iterator uIt;
   std::vector<int>::const_iterator nIt ;

   for(lIt = ls.begin(); lIt != ls.end();++lIt)
       for(uIt = us.begin();uIt != us.end();++uIt)
            for (nIt = ns.begin();nIt != ns.end();++nIt)
                std::cout << *lIt << *uIt << *nIt << "\n";

}
#包括
#包括
int main(int argc,char*argv[])
{
标准::向量ls(3);ls[0]='a';ls[1]='b';ls[2]='c';
标准::向量us(3);us[0]=“A”;us[1]=“B”;us[2]=“C”;
向量ns(3);ns[0]=1;ns[1]=2;ns[2]=3;
std::vector::const_迭代器lIt;
std::vector::const_迭代器uIt;
std::vector::const_迭代器nIt;
对于(lIt=ls.begin();lIt!=ls.end();++lIt)
对于(uIt=us.begin();uIt!=us.end();++uIt)
对于(nIt=ns.begin();nIt!=ns.end();++nIt)

std::cout不,您不能在运行时生成循环块。但是使用递归可以实现类似的效果。类似于这样:

void emulateNestedLoops(vector<size_t>& counters, 
        const vector<pair<size_t, size_t>>& bounds, size_t depth) {
    if (depth == bounds.size()) {
        // The body of the innermost loop.
    } else {
        counters.push_back(0);
        for (size_t i = bounds[depth].first; i < bounds[depth].second; i++) {
            counters.back() = i;
            emulateNestedLoops(counters, bounds, depth + 1);
        }
        counters.pop_back();
    }
}
void模拟循环(向量和计数器,
常量向量和边界、大小和深度){
if(depth==bounds.size()){
//最内层循环的主体。
}否则{
计数器。向后推_(0);
对于(size\u t i=bounds[depth]。第一;i
因此您需要递归地迭代

这段简单的代码可以给您一些提示:

std::vector<int> manyVector[N];
int currentIndexOfEachVector[N];

void Iterate(std::vector<int> &v, int depth)
{
    if(depth==N)
    {
        for(int i=0;i<N;i++)
            std::cout<< manyVector[i][currentIndexOfEachVector[i]];
        return;
    }
    //////////////////////////////////////////////////////////////////////////
    for ( int i=0 ; i< manyVector[depth].size() ; i++ )
    {
        Iterate(manyVector[depth+1], depth+1);
    }

}


int main()
{
    Iterate(manyVector[0], 0);
    return 0;
}
std::vector manyVector[N];
int currentIndexOfEachVector[N];
void迭代(std::vector&v,int-depth)
{
如果(深度==N)
{

对于(int i=0;i,您可以按照以下几行定义递归函数:

template<class Container>
Container combinations(Container last) {
    return last;
}

template<class Container, class... Containers>
Container combinations(Container curr, Containers... rest) {
    auto ret = Container();
    for (auto i : curr)
        for (auto j : combinations(rest...))
            ret.push_back(i + j);
    return ret;
}

当然,假设有一个简单的
print\u vec
函数


您甚至可以通过传递应用于组合两个元素的函子(而不是
运算符+
)来自定义
组合
更多,但这取决于您的需要。

您实际要解决的问题是什么?这似乎是一个XY问题。您不想考虑“在运行时生成循环块”。您可能想考虑如何在运行时计算循环的开始索引和结束索引,以创建所需的行为。哦,我明白了,这不清楚。我将对其进行编辑。从看起来的情况来看,您所要做的就是从字符串“aA1bB2cC3”中提取这些字符的3个组合
C(9,3)
。这不需要多组循环。没有类型省略是不可能的(向量可以有不同的类型).具有有限数量的类型N和没有类型省略的实现·你和你一起使用N*N*N*…实现。我正是在考虑递归,但同时我认为每个递归都可以转换为常规代码,对吗?@Sami1202检查。如果一个或多个容器为空且除非最后一个容器是empy,否则它可以正常工作,有什么建议吗?@Sami1202好吧,我的代码模拟了。所以从技术上讲,如果任何容器是空的,它都不会显示任何内容。我看不出如何修改它以轻松忽略空容器(不会增加太多复杂性).我建议在使用之前先过滤空的容器。
int n = /* number of vectors */;
switch (n) {
    case 1: print_vec(combinations(ls)); break;
    case 2: print_vec(combinations(ls, us)); break;
    case 3: print_vec(combinations(ls, us, ns)); break;
}