简体   繁体   中英

Iterating map of vectors of different sizes in C++

So I declared the following map in C++:

typedef vector<Vector2D> stackPoints; 
typedef map<int, stackPoints> mapPoints; 
mapPoints drawingPoints;

After adding values to it, I want to output all the elements in it, but the vectors at the different key positions are not of the same size:

I am using the following two for-loops that don't work. Sometimes the program crashes at runtime and gives me the out of range vector error.

for (int j = 0; j < drawingPoints.size(); j++)
{
    for (int i = 0; i < drawingPoints[j].size(); i++)
    {
        cout << "(...)" << endl
    }
}

It seems to me that the inner for-loop has the be went through a constant amount of times, as if the following scenario wasn't possible:

1) The first vector has a size of 1, so the inner for-loop will be executed once.

2) Then the second vector of the map has a size of 5 and now I want the for-loop to be went through 5 times, but this seems to not work.

** Edit **

I am using the integer key as a counter, so I increment it by 1 when I add another pair.

for (int j = 0; j < drawingPoints.size(); j++)
{
    // This is wrong.
    // j is not necessarily a valid key in the map.
    for (int i = 0; i < drawingPoints[j].size(); i++)
    {
        cout << "(...)" << endl
    }
}

Use iterators to avoid such issues.

mapPoints::iterator map_begin = drawingPoints.begin();
mapPoints::iterator map_end = drawingPoints.end();
for ( ; map_begin != map_end; ++map_iter )
{
   stackPoints::iterator v_iter = map_iter->second.begin();
   stackPoints::iterator v_end = map_iter->second.end();
   for ( ; v_iter != v_end; ++v_iter )
   {
      cout << "(...)" << endl
   }
}

If you are using a C++11 compiler, you can use:

for ( auto& map_item : mapPoints )
{
   for ( auto& v_item : map_item.second )
   {
      cout << "(...)" << endl
   }
}

For multiple reasons you should be using an iterator.

  1. Your map may not contain every element from 0 to drawingPoints.size() and for each element that it doesnt contain when you loop over it you will create it.
  2. Your highest entry may be greater then drawingPoints.size() in which case you will never reach it.

Lets look at outer most loop, if you want to define an iterator for it you should do the following:

for (mapPoints::Iterator it = drawingPoints.begin(); it != drawingPoints.end(); it++) {

This creates an iterator which you can look at (by doing it-> or *it ).

More information can be found here .

The problem is iterating the map: the fact that you can do drawingPoints[j] is entirely by accident - the type of your map's key just happens to be int , so j works fine as its index. However, not all j s are going to have anything in the map, so the code does not work.

Here is how you iterate over your map:

for(auto& kvp : drawingPoints) {
    cout << "Key " << kvp.first << " has the following values:" << endl;
    for (auto n : kvp.second) {
        cout << n << " ";
    }
    cout << endl;
}

The example above requires C++11. If you use an older compiler, use this Q&A to go over a map using iterators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM