Is it possible to generate sets in a loop, where the loop comes up with a new name for the set?
I am trying to do the following:
//create storage structure for options according to hops
int lengthOfStart = start.length();
for (int i = 0; i<start.length();++i) {
string nameOfSet = "Hop" + i;
Set<string> nameOfSet;
wordLadderOptions.enqueue(nameOfSet);
}
I am using a slightly modified version of traditional c++ set which just offers some more functions for data manipulation but otherwise the set is same as the one built in to c++ standard library. When I say Set<string> nameOfSet; the compiler sees this as the actual name of the set and not a variable…
How can I make it see it as a variable in order to create sets in the for loop based on the variable i’s value?
Variable names don’t exist in C++ after compilation (apart from debugging info). So your request isn’t very meaningful. Perhaps you want to associate each set with a name and stick them in a
map<string, Set>?In a language like Python which actually does let you do this, that’s what’s happening behind the scenes. The only difference is that variable names are implicitly looked up in a dict at runtime.