0

Is there a way to make multiple vectors and name them each inside a while loop?

Example:

string name;
while(true){
    cout << "Name the vector :" << endl;
    cin >> name;
    vector<string> ?name?;
}

I know this is bad code, just want to see how it would work Couldn't find anything online, thanks in advance.

Oddments
  • 3
  • 2
  • You can initialize vectors in any statement but execution is only possible after declaration. I think this solve your problem, If not add a comment here. This can help you [Initialization of vectors](https://stackoverflow.com/questions/2236197/what-is-the-easiest-way-to-initialize-a-stdvector-with-hardcoded-elements) – kvk30 Oct 14 '18 at 01:42

1 Answers1

1

Yes, you can use a std::map to bind names to objects:

string name;
map<string, vector<string>> vecs;
while(true){
    cout << "Name the vector :" << endl;
    cin >> name;
    vecs[name]; // inserts if does not exist
    vecs[name].push_back("example"); // or you can do this without the prior line
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436