how to start investing

Erase-Remove Idiom in C++ – GeeksforGeeks

Click to rate this post!
[Total: 1 Average: 5]

Erase-Remove-Idiom is a C++ STL (Standard Template Library) technique to remove elements from a container. It is used to remove all the elements satisfying certain conditions from the container.

The erase-remove idiom is especially useful in array-based containers like vectors, where each elimination requires all the remaining elements to adjust. In the worst case, this may lead to O ( n2 ) time complexity. This technique avoids this by providing the elimination of the elements in a single parse. That is why, the erase-remove idiom provides O( n ) time complexity.

In this technique, we use the combination of two member functions of the container to remove the elements efficiently.

  • std::erase
  • std::remove or std::remove_if

It is due to the use of this function that this technique is called the erase-remove idiom.


The std::erase function is used to remove elements from the container giving the iterator its position. It can also remove the range of elements when the iterator to starting position and ending position are provided.

Syntax of std::erase

container.erase(position); // to eliminate single element
container.erase(starting_position, ending_position); // to eliminate a range of elements


  • position: It is the iterator to the element to be removed.
  • starting_position: iterator to the starting point of the range to be eliminated.
  • ending_position: It is the iterator to the ending point of the range to be eliminated.


The std::remove function is used to relocate the elements which equate to the given value and return the iterator to the new logical end.

Syntax of std::remove

container.remove(first, last, val);


  • first: It is the iterator to the first element of the range the function has to work.
  • last: It is the iterator to the last element of the range.
  • val: It is the value to be tested against.


The std::remove_if function is the variation of the remove function in which we can pass a comparator function instead of a value to test.

Syntax of std::remove_if

container.remove_if(first, last, val);


  • first: It is the iterator to the first element of the range the function has to work.
  • last: It is the iterator to the last element of the range.
  • predicate: It is the function that specifies the comparator.

Working of Erase Remove Idiom

This is a two-step technique that involves using both std::remove and std::erase functions one after another. The following are the steps:

  • STEP 1: The std::remove function moves all elements to be removed at the end of the container. It returns the new logical end of the range.
  • STEP 2: The std::erase function is then used to erase all the elements after the new logical end at once.

Implementation of Erase Remove Idiom

The following example demonstrates how to use Erase Remove Idiom for removing odd numbers from a vector container.


#include <algorithm>

#include <iostream>

#include <vector>


void printV(std::vector<int>& v)


    for (auto i : v) {

        std::cout << i << " ";



    std::cout << std::endl;



int main()



    std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };



    std::cout << "Original Vector\t\t\t: ";





    auto new_logical_end = std::remove_if(

        v.begin(), v.end(), [](int a) { return a % 2; });



    std::cout << "After using remove_if()\t: ";




    v.erase(new_logical_end, v.end());

    std::cout << "After using erase()\t\t: ";




Original Vector            : 1 2 3 4 5 6 7 8 9 
After using remove_if()    : 2 4 6 8 5 6 7 8 9 
After using erase()        : 2 4 6 8

Advantages of Erase Remove Idiom

  • Highly efficient as all the items can be removed in a single parse.
  • Safe and flexible due to the use of STL predefined function templates.

Limitations of Erase Remove Idiom

  • It only works with certain types of containers like vectors, lists, and deques, and not with others like maps and sets.
  • Disrupts the order of remaining elements.
  • It may be slow if the predicate function is expensive.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top