So, I have an std::map<int, my_vector>
and I want to go through each int and analyse the vector.
I haven't gotten to the part of analyzing the vector just yet, I'm still trying to figure out how to go through every single element on the map.
I know it is possible to have an iterator, but I didn't quite understood how it works, also, I don't know if there isn't a better way to do what I intend to do
Asked
Active
Viewed 1.2k times
0

FriedRike
- 145
- 2
- 4
- 20
-
1[This](http://stackoverflow.com/a/4844904/1410711) might be helpful.... – Recker Mar 13 '13 at 18:08
2 Answers
6
You can simply iterate over the map. Each map element is an std::pair<key, mapped_type>
, so first
gives you the key, second
the element.
std::map<int, my_vector> m = ....;
for (std::map<int, my_vector>::const_iterator it = m.begin(); it != m.end(); ++it)
{
//it-->first gives you the key (int)
//it->second gives you the mapped element (vector)
}
// C++11 range based for loop
for (const auto& elem : m)
{
//elem.first gives you the key (int)
//elem.second gives you the mapped element (vector)
}

juanchopanza
- 223,364
- 34
- 402
- 480
1
Iterators are the perfect thing for this. Look around http://www.cplusplus.com/reference/map/map/begin/

Thomas Ruiz
- 3,611
- 2
- 20
- 33