Accessing Vectors Inside Vectors

This might be a very silly question to some and forgive me if it is.
I am to make a game of a sneaky hangman, where I will be using a dictionary of 172 thousand words. What I want to do is to read in the .txt file containing one word per line ordered alphabetically. What I want to do then is to put each word into an two-dimensional array where it would be sorting words by their length. So the array on position myArray[4][x] will store words of length 4, array that is placed on position myArray[5][x] will store words of length 5. Of course in String format. But one thing occurred to me is that I have to pre-initialize the array to state it's length but I don't know how many 5 lettered words are there in those 172 thousand words, so long story short I decided to use a Vector due to it's self resizing nature.

I declared the vector as:

 
  vector<vector<string>> dictionary;


Then when I read each single word from the file I first store it in a tempInput variable of type string which I then use to store in the vector on position tempInput.length() by using the push_back function but I don't know how to use it. I know that if I have a simple vector i simply use "myVector.push_back(value);" but in this case I get confused. Working with two-dimensional arrays looks easier. I tried the STL reference for vector and found the "at()" but that does only check value "at" position, then I found the "insert()" function which might be of use for me, but I don't know how to use the insert() in conjunction with the push_back() for the vector in the position I try to access.

Simply put I don't know how to work with a vector inside a vector. :(

Thank you VERY much in advance!
Last edited on
Vector does have a [] operator. It returns a reference to a value_type object.

If you had vector<double> dict; then dict[2] would return a reference to the third double element (if there is one).

Now replace double with vector<string>.

If you have a reference to a vector, you can use its []. When you do, you will get a reference to a std::string object within. Since strings have a [] operator too, you could refer to individual char like this:
char x = dictionary[3][4][2];

So, you want to add "hello"? And you have decided that words that long belong to the dictionary[5]? (How many words will be in the first element dictionary[0]?)
dictionary[5].push_back( "hello" );


You do have serious issue though. Bob shuffled the file. It starts: "Bob is wonderful".
You should add to elements 3, 2, 9. You don't know how long is the longest word, so you don't know how many vectors the dictionary should contain, nor cannot simply append.

Therefore, std::vector is not your friend tonight. There are, however:
std::map<size_t,std::set<std::string>> dictionary;
keskiverto wow that was helpful a lot!!! Thank you very much!

Yes I have seen a similar exercise done using maps but I have no clue of how to use them properly as well, I used them in Java briefly but never had chance to familiarize myself with them, could you recommend a tutorial?

(I know I can find a tutorial on google, just asking if You might have a specific one you liked)

Thank you very much again!
A map is a vector where the index is not required to be a number, and where assigning to nonexistent indexes creates them for you instead of crashing.

Vectors are used as lists, whereas maps are used as associative containers.
Last edited on
So now I shall use dictionary words as keys and their values, their lengths? But if I want to select random keys with values of 4 letters? And then based on their "at()" chars keep refining my selected word? Search all keys with value 5 and store in single dimensional array (for example)?
Last edited on
@keskivertiko's solution would fit your needs here. What he has done is he has mapped lists of words to their value: std::map<std::size_t, std::set<std::string>>. The map value is std::size_t (normally an unsigned integral value), so you can for example get a list of all the words of length of 4 by doing std::set<std::string> words = map[4];, where map is your dictionary. From there you can loop through those words and narrow down your search within them.
std::map<size_t,std::set<std::string>> dictionary;

This 'dictionary' is a map, where key is size_t (an integer) and value is a set of strings. Therefore, if you do dictionary[5], you will get a reference to the set<string> that may contain words, perhaps even 5-char long words, if you did put them there.
http://www.cplusplus.com/reference/map/map/operator%5B%5D/

http://www.cplusplus.com/reference/set/set/
Set contains unique values in sorted order. You coud use vector<string> instead of set<string>.
NT3 and keskiverto you are amazing!!! Thank you so much!

I love this forum :)
Topic archived. No new replies allowed.