# Thread: Iterating through a vector of maps and a map of vectors...

1. ## Iterating through a vector of maps and a map of vectors...

Hey guys get this...
I defined a vector of maps, like this:
Code:
`typedef vector<map<string,unsigned int> > myvec;`
And a map of vectors (i called it index) like this:
Code:
`typedef map<string,vector<unsigned int> > index;`
Then I did the following:
In my class, called myCoogle, i declared
Code:
`myvec the_vec;`
and I filled it with maps...
In each map there's a word (string) and a number (unsinged int).
So far so good.
I declared also
Code:
`index the_index;`
Now I want to copy all the different words from maps_vec to the_index.
The words would be the strings...

And for each vector<int> i will be adding the numbers stored in the vector of maps.

For example:
the_vec has 3 maps.
the 1st has: chicken,1 | person,1 | elevator,5 | is,2 | ...
the 2nd has: person,2 | icecream,3 | is,3 | ...
the 3rd has: elevator,1 | bear,1 | is,4 |chicken,3 | ...
*the words might be similar...

So the_index should look like this:
word,[vector of ints]
chicken[1,0,3]
person,[1,2,0]
elevator[5,0,1]
is[2,3,4]
icecream[0,3,0]
bear[0,0,1]

OK here's my function:

Code:
```void Coogle::make_index()
{
//SCAN THE FIRST MAP
myvec::iterator myvec_iter;
map<string,unsigned int>::iterator map_iter;
index::iterator idx_iter = the_index.begin();
for(map_iter=maps_vec[0].begin(); map_iter!=maps_vec[0].end(); ++map_iter)
{
the_index[map_iter->first].push_back(map_iter->second);
}

//SCAN THE OTHER MAPS
myvec_iter=maps_vec.begin();
myvec_iter++;
int i=0; //FILE #
while(myvec_iter!=maps_vec.end())
{
i++;
for(map_iter=maps_vec[i].begin(); map_iter!=maps_vec[i].end(); ++map_iter)
{
string word=map_iter->first;
cout << "DEALING WITH WORD \"" << word << "\"" << endl;
index::iterator location;
location=the_index.find(word);
if(location!=the_index.end()) //if word found in the index
{
cout << "WORD EXISTS!" << endl;
location->second[i]=map_iter->second;
}
else //if not found
{
cout << "WORD DOES NOT EXIST! NEW WORD." << endl;
the_index[word].push_back(map_iter->second);
}
}
cout << endl;

++myvec_iter;
}
}```
clarification: FILE# is the maps number... I'm working with files (*.txt files).

Alright so after I scanned the first map, I tried to print the_index and all was fine.
But I get this when trying to print after scanning also the other maps:

So something is wrong with my 2nd 'for' loop.

Anyone can help please?

Very sorry for the very long post...

Thank you very much !!!

2. As it says in brackets in the last line on that dialog, click Retry because you want to debug the application.
This will break you into the debugger and it will be showing a line of code from vector.

From the Debug menu, select Windows -> Call Stack (Alt + 7)
Find the first item in the call stack that relates to code that you wrote and double-click it.
Now, bring up Debug -> Windows -> Autos and/or Debug -> Windows -> Locals
From these Windows you can see what the values of your variables are at the current point in that piece of code.

Any closer to solving it now? Tell us what you're seeing.

3. I don't have to do all that... I could use a Breakpoint... Much eaiser
But anyway,
I'm looking at the Autos... And man I see lotta weird stuff...

4. The code is unnecessarily complicated. A hint to ponder: operator[] on a map will insert elements in certain circumstances.

Your usage of the variable i is the problem I see (I haven't looked further). In the loop it is keeping track of the current index of myvec_iter. It is also being used to index elements of vectors inside the map of vectors, but you have taken no steps to ensure that i is a valid index of the vector location->second.

5. my_vec is a vector of maps.
then the_vec[0] is the 1st map, the_vec[1] is the second, and so on...
the_vec contains 3 maps...
Besides, the first 'for' loop is ok, because I tried to print that and it showed everything is ok

6. You missed my point completely.

7. Originally Posted by iMalc
As it says in brackets in the last line on that dialog, click Retry because you want to debug the application.
This will break you into the debugger and it will be showing a line of code from vector.

From the Debug menu, select Windows -> Call Stack (Alt + 7)
Find the first item in the call stack that relates to code that you wrote and double-click it.
Now, bring up Debug -> Windows -> Autos and/or Debug -> Windows -> Locals
From these Windows you can see what the values of your variables are at the current point in that piece of code.
You should definitely take this advice on board!

Originally Posted by alaa_137
I don't have to do all that... I could use a Breakpoint... Much eaiser
True -- but since your program has already crashed, you'd have to set the breakpoint and restart the program. This might not be a problem for you right now, but if you have a longer running program that crashes intermittently, it's really useful to be able to debug from the crash.

Anyway, breakpoint or iMalc's way....

Originally Posted by alaa_137
my_vec is a vector of maps.
then the_vec[0] is the 1st map, the_vec[1] is the second, and so on...
the_vec contains 3 maps...
Besides, the first 'for' loop is ok, because I tried to print that and it showed everything is ok
You'll find that the line crashing is;
Code:
`location->second[i]=map_iter->second;`
with word "is" and i=1.

Your problem boils down to the fact that you can't add vector entries like that. You can't access vector entries that don't exist with the array notation, and you certainly can't add new entries using that notation.

Simple example:
Code:
``` vector<int> v;
v.push_back(1);
v.push_back(2);

v[0] = 5; //ok, overwrites first entry
v[1] = 6; //ok, overwrites second entry
v[2] = 10; // BANG - v[2] doesn't exist
cout << v[3]; // BANG - v[3] doesn't exist either```
On the flipside, as grumpy said, using [i] notation on a map will add an entry if one doesn't exist already. But for vector you should use push_back.

Since you want to store '0' in the index vector when the string wasn't present in the map, I think you'll have to add more code to handle this. Make sure 0s are pushed back to existing entries that had no matches, and make sure new entry creation fills the first N entries with 0s.

You shouldn't really need a first and second loop -- if you write it properly, you should be able to just have 1, and on the first map it just goes through the "need new entry" path every time.

8. Oh thank you very much!
I really mis used the [] operator. It doesn't work the way I expected with vectors.
I fixed it, used push_back and all that...
Now it works just perfect

Thanks a lot

Popular pages Recent additions