您可以在 C++ 中动态创建 for 循环吗?

十一

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

#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();
}
杰里米·弗里斯纳

不是直接的,但您可以实现“里程表风格”的行为,如下所示:

#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 ) );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

您可以在c中的函数的callong中调用函数吗

来自分类Dev

您可以在Redis c#中检索TTL吗?

来自分类Dev

您可以在C ++中定义同义词吗?

来自分类Dev

您可以确定C中的堆栈深度吗?

来自分类Dev

您可以在Linux终端中编译并运行c ++文件吗?

来自分类Dev

在C ++中,您可以让类成员成为传入的引用吗?

来自分类Dev

您可以在C#中向对象动态添加属性吗?

来自分类Dev

您可以在ARM模板中执行嵌套复制循环吗?

来自分类Dev

您可以在python中覆盖循环名称吗

来自分类Dev

您可以在c#wpf中访问在XAML中创建的渐变颜色吗

来自分类Dev

您可以使用#define宏在C中创建文件吗?

来自分类Dev

您可以使用JNI在从Java调用的c ++函数中创建新的JVM吗?

来自分类Dev

您可以动态制作SVG动画吗?

来自分类Dev

您可以从lua修改C结构吗?

来自分类Dev

您可以从geotiff创建XYZ目录吗

来自分类Dev

您可以创建旋转的JPanel吗?

来自分类Dev

您可以从XML创建表吗

来自分类Dev

您可以创建自己的任务配置吗?

来自分类Dev

您可以在创建后向承诺添加 .then 吗?

来自分类Dev

您可以在打字稿中动态扩展类型吗?

来自分类Dev

您可以在GO中动态生成XML吗?

来自分类Dev

您可以在Arduino(C / C ++)中制作128位无符号整数吗?

来自分类Dev

您可以在TypeScript中扩展HTMLDivElement吗?

来自分类Dev

您可以在python中乘以单词吗?

来自分类Dev

您可以覆盖Java中==的内容吗?

来自分类Dev

您可以反转Java中的开关吗?

来自分类Dev

您可以在div标签中更新吗

来自分类Dev

您可以在GraphQL中嵌套片段吗?

来自分类Dev

您可以在JPanel中添加JButton吗

Related 相关文章

热门标签

归档