Search code examples
c++variablesdynamiccycle

Use the index in naming variables during a cycle C++


I need a tip in creating a C++ utility. I'm implementening an optimization algorithm, and at a certain step, I would need to create as much new variables as the iterations of a cycle. There's some code that would explain it better:

for(int i=1;i<112;i++){
   struct nodo n_2i[111-i];           
}

The structure nodo is defined as:

struct nodo{
  int last_prod;
  int last_slot;
  float Z_L;
  float Z_U;
  float g;
  bool fathomed;
};

I would like the names of the new variables (arrays of structures) to be n_21,n_22,n_23,...etc. How can I handle that?


Solution

  • Why do you need the name to be n_21. you can use a vector of vector.

    #include <vector>
    using namespace std;
    int main() {
        vector<vector<struct nodo> > n;
        for(int i=1;i<112;i++){
            n.push_back(vector<struct nodo>(111-i));       
        }
        // you can use n[0] ... n[111] now
    }