c++erase-remove-idiom

Using erase-remove_if idiom


Let's say I have std::vector<std::pair<int,Direction>>.

I am trying to use erase-remove_if idiom to remove pairs from the vector.

stopPoints.erase(std::remove_if(stopPoints.begin(),
                                stopPoints.end(),
                                [&](const stopPointPair stopPoint)-> bool { return stopPoint.first == 4; }));

I want to delete all pairs that have .first value set to 4.

In my example I have pairs:

- 4, Up
- 4, Down
- 2, Up
- 6, Up

However, after I execute erase-remove_if, I am left with:

- 2, Up
- 6, Up
- 6, Up

What am I doing wrong here?


Solution

  • The correct code is:

    stopPoints.erase(std::remove_if(stopPoints.begin(),
                                    stopPoints.end(),
                                    [](const stopPointPair stopPoint)-> bool 
                                           { return stopPoint.first == 4; }), 
                     stopPoints.end());
    

    You need to remove the range starting from the iterator returned from std::remove_if to the end of the vector, not only a single element.

    "Why?"


    More information: Erase-remove idiom (Wikipedia).