Powereleven
Powereleven

Reputation: 329

Can you create for loops dynamically in C++?

My question is commented in the code, is there any way to achieve what I want?

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

Upvotes: 0

Views: 104

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73071

Not directly, but you could implement that behavior "odometer-style", like this:

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

The same concept expressed purely iteratively (some readers might find that more clear, and it avoids a possible marginal inefficiency for the recursive call) can go like this:

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

Upvotes: 3

Related Questions