0

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?

rrazd
  • 1,741
  • 2
  • 32
  • 47
  • 2
    Sounds like a vector is a better fit. What advantage do `hop0` and `hop1` hold over `hop[0]` and `hop[1]`? – chris Aug 17 '12 at 05:05
  • Pretty much the same question as this http://stackoverflow.com/questions/7143120/convert-string-to-variable-name-or-variable-type (and there are further links there to other posts with a lot of background information) – jogojapan Aug 17 '12 at 05:12
  • each set contains a bunch of words which have no particular order. I need to iterate through each of these words later on... just based on the fact that there is no ordering btw the words I thought a set would be better? – rrazd Aug 17 '12 at 05:12
  • @rrazd Chris didn't mean to replace the sets with vectors, but to use a vector as a container for the sets. No need for separate names then any more. – jogojapan Aug 17 '12 at 05:14

1 Answers1

2

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.

Antimony
  • 37,781
  • 10
  • 100
  • 107