C++ 能否在C+;中动态创建for循环+;?

C++ 能否在C+;中动态创建for循环+;?,c++,C++,我的问题在代码中有注释,有什么方法可以实现我想要的吗 #include <iostream> int main() { std::cin >> n_loops; //I want to specify the number of nested loops and create new variables dynamically: // variables names: x1, x2, x3, ... x(n_loops) // if n_loop

我的问题在代码中有注释,有什么方法可以实现我想要的吗

#include <iostream>

int main()
{
    std::cin >> n_loops; //I want to specify the number of nested loops and create new variables dynamically:
    // variables names: x1, x2, x3, ... x(n_loops)
    // if n_loops is 3, for example, I want this code to be executed.
    for (int x1 = 0; x1 < 10; x1++)
        for (int x2 = 0; x2 < 10; x2++)
            for (int x3 = 0; x3 < 10; x3++)
            {
                std::cout << x1 << ", " << x2 << ", " << x3 << std::endl;
            }
    std::cin.get();
}
#包括
int main()
{
std::cin>>n_循环;//我想指定嵌套循环的数量并动态创建新变量:
//变量名称:x1,x2,x3,…x(n_循环)
//例如,如果n_循环是3,我希望执行此代码。
对于(int-x1=0;x1<10;x1++)
对于(int x2=0;x2<10;x2++)
对于(int x3=0;x3<10;x3++)
{

std::cout不直接,但您可以实现“里程表样式”的行为,如下所示:

#include <iostream>
#include <vector>

static bool AdvanceOdometer(std::vector<int> & counters, int idxToIncrement, int counter_max)
{
    if (++counters[idxToIncrement] == counter_max)
    {
       if (idxToIncrement == 0) return false;  // signal that we've reached the end of all loops

       counters[idxToIncrement] = 0;
       return AdvanceOdometer(counters, idxToIncrement-1, counter_max);
    }
    return true;
}

int main()
{
   int n_loops;
   std::cin >> n_loops;

   std::vector<int> counters;
   for (size_t i=0; i<n_loops; i++) counters.push_back(0);

   const int counter_max = 10;  // each "digit" in the odometer should roll-over to zero when it reaches this value
   while(true)
   {
      std::cout << "count: ";
      for (size_t i=0; i<n_loops; i++) std::cout << counters[i] << " ";
      std::cout << std::endl;

      if (AdvanceOdometer(counters, counters.size()-1, counter_max) == false) break;
   }
   return 0;
}
#include <iostream>
#include <string>           // std::stoi
#include <vector>           // std::vector
using namespace std;

auto advance( vector<int> & digits, int const radix )
    -> bool      // true => advanced without wrapping back to all zeroes.
{
    for( int& d : digits )
    {
        ++d;
        if( d < radix ) { return true; }
        d = 0;
    }
    return false;
}

auto main( int n_args, char** args )
    -> int
{
   int const n_loops = stoi( args[1] );
   std::vector<int> digits( n_loops );

   const int radix = 10;

   do
   {
      for( int i = digits.size() - 1; i >= 0; --i )
      {
          cout << digits[i] << " ";
      }
      cout << std::endl;
   } while( advance( digits, radix ) );
}
#包括
#包括
静态bool AdvanceOdometer(标准::向量和计数器、整数idxToIncrement、整数计数器_max)
{
如果(++计数器[idxToIncrement]==计数器最大值)
{
if(idxToIncrement==0)返回false;//表示我们已到达所有循环的末尾
计数器[idxToIncrement]=0;
返回进位计数器(计数器,idxToIncrement-1,计数器最大值);
}
返回true;
}
int main()
{
int n_循环;
标准::cin>>n_循环;
向量计数器;

对于(size_t i=0;i不是直接的,但是您可以实现“里程表样式”的行为,如下所示:

#include <iostream>
#include <vector>

static bool AdvanceOdometer(std::vector<int> & counters, int idxToIncrement, int counter_max)
{
    if (++counters[idxToIncrement] == counter_max)
    {
       if (idxToIncrement == 0) return false;  // signal that we've reached the end of all loops

       counters[idxToIncrement] = 0;
       return AdvanceOdometer(counters, idxToIncrement-1, counter_max);
    }
    return true;
}

int main()
{
   int n_loops;
   std::cin >> n_loops;

   std::vector<int> counters;
   for (size_t i=0; i<n_loops; i++) counters.push_back(0);

   const int counter_max = 10;  // each "digit" in the odometer should roll-over to zero when it reaches this value
   while(true)
   {
      std::cout << "count: ";
      for (size_t i=0; i<n_loops; i++) std::cout << counters[i] << " ";
      std::cout << std::endl;

      if (AdvanceOdometer(counters, counters.size()-1, counter_max) == false) break;
   }
   return 0;
}
#include <iostream>
#include <string>           // std::stoi
#include <vector>           // std::vector
using namespace std;

auto advance( vector<int> & digits, int const radix )
    -> bool      // true => advanced without wrapping back to all zeroes.
{
    for( int& d : digits )
    {
        ++d;
        if( d < radix ) { return true; }
        d = 0;
    }
    return false;
}

auto main( int n_args, char** args )
    -> int
{
   int const n_loops = stoi( args[1] );
   std::vector<int> digits( n_loops );

   const int radix = 10;

   do
   {
      for( int i = digits.size() - 1; i >= 0; --i )
      {
          cout << digits[i] << " ";
      }
      cout << std::endl;
   } while( advance( digits, radix ) );
}
#包括
#包括
静态bool AdvanceOdometer(标准::向量和计数器、整数idxToIncrement、整数计数器_max)
{
如果(++计数器[idxToIncrement]==计数器最大值)
{
if(idxToIncrement==0)返回false;//表示我们已到达所有循环的末尾
计数器[idxToIncrement]=0;
返回进位计数器(计数器,idxToIncrement-1,计数器最大值);
}
返回true;
}
int main()
{
int n_循环;
标准::cin>>n_循环;
向量计数器;

对于(size_t i=0;i)您为什么需要这样做?您试图解决什么问题?将循环放入函数中并传递当前深度和目标深度。您确实意识到,对于n>=20(可能更小),此程序将在您死后结束,对吗?有两种主要方法:递归和使用索引向量(对于2个索引的情况,这有时通过算术编码完成)。这确实是一个答案,但我认为你通过自己做这件事学到了很多东西,如果没有具体的例子,很可能会遭到严重的否决,因此,这是一种风格;因此,作为一种评论发布。他们希望他们的鱼烤好,可以随时食用。匿名的否决者,这是一个答案简单的明确问题。请不要完全出于无知而投票,因为你是正在进行。您为什么需要这样做?您试图解决什么问题?将循环放入函数中并传递当前深度和目标深度。您确实意识到,对于n>=20(可能更少),此程序将在您死后结束,对吗?有两种主要方法:递归和使用索引向量(对于2个索引的情况,这有时通过算术编码完成)。这确实是一个答案,但我认为你通过自己做这件事学到了很多东西,如果没有具体的例子,很可能会遭到严重的否决,因此,这是一种风格;因此,作为一种评论发布。他们希望他们的鱼烤好,可以随时食用。匿名的否决者,这是一个答案简单的明确问题。请不要完全出于无知而投票,因为你是做。抱歉,这里的递归是一个不必要的低效:它不能带来清晰。纯迭代会更好。不要因为它看起来不正确而进行向下表决,只是不惯用,并导致不必要的低效。好吧,你为什么不发布非递归版本,读者可以自己决定他们喜欢哪个版本ter?抱歉,这里的递归是一个不必要的低效:它不能带来清晰性。纯迭代会更好。不要因为它看起来不正确而进行向下表决,只是不惯用,并导致不必要的低效。好吧,为什么不发布非递归版本,读者可以自己决定他们更喜欢哪个版本?