Hey Guys,

I'm pretty new to containers, but need to use them for a project I'm doing.

I've figured out that I can use a map iterator to iterate through values stored in the map displaying the key, followed by the second value.

In my case, the key is an`int` and the second value is a nested vector of the form `vector<vector<double> >`.

My question is how would I go about using a the map iterator to display the contents of the second value? Would I need a for loop somewhere?

Here's the relevant code snippet.

``````        map<int, vector<vector<double> > >::iterator miter;

for(miter = triangleMap.begin(); miter != triangleMap.end(); miter++)
{
cout<<"\nSet "<<(*miter).first;//<<", holding "<<(*miter).second;
}

//The part that is commented out above is what does not work.
``````

Edited by collin_ola

2
Contributors
8
Replies
20
Views
2 Years
Discussion Span
Last Post by collin_ola

Forget that you're dealing with `miter->second` for a moment. How would you display the contents of a vector? We know `cout` doesn't have an overloaded `<<` operator for collections, so you're stuck with some variant of a loop. This comes immediately to mind:

``````for (auto it = v.begin(); it != v.end(); ++it)
{
cout << *it << ' ';
}
``````

Since this is a vector of vectors, you have a nested loop:

``````for (auto row = v.begin(); row != v.end(); ++row)
{
for (auto col = row->begin(); col != row->end(); ++col)
{
cout << *col << ' ';
}

cout << '\n';
}
``````

Now replace `v` with `miter->second`, as that's your reference to the mapped vector object.

The idea here is to start with something you understand, then add complexity until you have the final solution that you're looking for.

I'm using C++98, which doesn't allow me to use the `auto`. What could I use instead?

`vector<vector<double> >::iterator` and `vector<double>::iterator`, respectively for the nested loops. Just like you're doing with `miter`, which is why I didn't mention it...

I wasn't aware of it, I just received an error message saying to remove the auto keyword, but I wasn't sure what to replace it with, so I thought I would ask.

The `auto` keyword is compiler magic from C++11 that interprets the type of a variable from its initializer. It's not a dynamic type or anything, so as an example if you're using `v.begin()` as the initializer, `auto` will choose the type of the collection's non-const iterator (in this case `vector<vector<double> >::iterator`). Really the only difference is you don't have to manually use the type name or worry about changing any use of it if the if the type changes.

Oh okay. Thanks!

So I attempted using your code above following your instructions. It runs, but inexplicably crashes. I replaced the `cout<<*col;` with a counter variable `cout<<i++;` just to see what was happening and for some reason, it seems to be in an infinite loop - `i` just keeps increasing.

Any ideas?

Please post the whole of your relevant code. My guess would be the counter is being incremented incorrectly, or the condition of the loop doesn't check the counter the way it should.

I've just realised I grave mistake I've made...

Initially, I was intending to use a `vector<vector<double> >`, but I then realised that I actually only needed a `vector<double>`. I updated the relevant parts as required, but forgot to update this.

I'll resolve this firstly and attempt to work from this, using part of the nested loop you provided above.

I'll be sure to post again if I encounter any problems.

Thanks for all your help, really appreciate it.