Erase-remove idiom is a common technique to eliminate the elements from a C++ STL container which satisfies a particular condition.
We can do the hand written loop to remove the elements from the container. For e.g. if you want to remove an element vector you can do something as below. (note erasing element from a C++ container within a standard for or while loop with simple iterator++ will end up undefined result)
vector::iterator it = vec.begin(); while(it != vec.end()) if( *it == 5 ) it = vec.erase(it); // Remove and take the return of the erase function to safely reassign else ++it;
The above code is simple as it appears, it simply iterate through the elements and find if a match there for the value then remove it from the container. And it takes back the value returned form erase where it returns the iterator of the next element. If no more elements, it will vector::end
Now, the containers will have different implementations for the same function. A map::erase return void. So it’s difficult to give a single and easy strategy for each type of containers.
remove is a handy function defined in algorthm It’s easy to get confused to know what it does and distinguish between erase. remove remove (move) the elements from the given range. Mostly push it towards the end of the container. The function returns an iterator to the location where the moved elements located within the container. It essentially won’t remove the elements as such from the container. Here is the sample demo of using remove
// remove algorithm example #includeusing namespace std; int main () { int data[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20 // bounds of range: int* pbegin = data; // ^ int* pend = data+sizeof(data)/sizeof(int); // ^ ^ pend = remove (pbegin, pend, 20); // 10 30 30 10 10 ? ? ? // ^ ^ return 0; }
Now this handy function has an alternative version to give a comparator function which can accept the object of the type specified for the container. Inside the function, you can make decision to remove or not by specifying in the return value. In simple words, rather giving a comparison hard coded value, we’ll give a function to make decision for us. See the documentation and example here.
Erase-Remove tequenique is accomplished in following ways.
1. Move the elements to be removed to the end with the help of remove function
2. Take the return of remove and pass to the erase function as beginning and containers::end as end.
3. Finally the contianer will contains the elements which are not satisfying the comparator function/value.
#include// the general-purpose vector container #include // remove and remove_if bool is_odd(int i) { // unary predicate returning true if and only if the argument is odd return i % 2; } bool is_even(int i) { // unary predicate returning true if and only if the argument is even return !is_odd(i); } int main() { using namespace std; int elements[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // create a vector that holds the numbers from 0-9. vector v(elements, elements + 10); // use the erase-remove idiom to remove all elements with the value 5 v.erase(remove(v.begin(), v.end(), 5), v.end()); // use the erase-remove idiom to remove all odd numbers v.erase( remove_if(v.begin(), v.end(), is_odd), v.end() ); // use the erase-remove idiom to remove all even numbers v.erase( remove_if(v.begin(), v.end(), is_even), v.end() ); }
Reference:
Erase-remove idiom