Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Containers

The erase-remove idiom and std::erase

How to remove elements from C++ containers: the classic erase-remove idiom, the C++20 std::erase and std::erase_if functions, and why remove alone deletes nothing.

std::remove and std::remove_if do not shrink a container. They are algorithms that only see iterators, so they cannot change the container's size. They shuffle the elements you want to keep to the front and return an iterator to the new logical end. The leftover tail holds valid but unspecified values, and the container keeps its old size.

To really delete the elements you call the container's erase member on that tail. Combining the two calls is the erase-remove idiom. Since C++20, the free functions std::erase(c, value) and std::erase_if(c, pred) do both steps in one call for every standard container.

C++
v.erase(std::remove(v.begin(), v.end(), 3), v.end());   // classic
std::erase(v, 3);                                       // C++20
std::erase_if(v, [](int x) { return x % 2 == 0; });     // C++20

Try it

C++ Classic idiom versus C++20

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Associative and sequence containers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
// <algorithm>
template<class It, class T>   It remove(It first, It last, const T& value);
template<class It, class Pred> It remove_if(It first, It last, Pred pred);

// <vector>, <string>, <list>, <map>, ... (C++20)
template<class T, class A, class U> size_type erase(vector<T, A>& c, const U& value);
template<class T, class A, class Pred> size_type erase_if(vector<T, A>& c, Pred pred);
Name Description
first, last The range to process.
value Elements equal to it are removed.
pred Unary predicate; elements for which it returns true are removed. For associative containers it receives the whole element (a key-value pair for maps).
c The container (erase and erase_if overloads exist for vector, deque, list, forward_list, string, map, set, unordered_* and the multi- variants).

Returns: remove and remove_if return the new logical end. std::erase and std::erase_if return the number of removed elements (C++20).

Undefined behavior: erasing with an iterator range that is not part of the container is undefined. Elements between the new end and the old end after remove are in a moved-from, unspecified state: do not read them.

Usage notes

  • Order is preserved. remove is stable: kept elements keep their relative order. It runs in linear time with at most n predicate applications.
  • Erasing in a loop is a classic bug. Calling v.erase(it) inside a range-for invalidates the iterators. Use the idiom, or it = v.erase(it) in an explicit loop.
  • Vector erase is O(n) per call; erase-remove is O(n) for the whole job, so it is much better than erasing element by element.
  • Associative containers cannot use std::remove (their elements are const keys). Use erase_if, or the explicit loop before C++20.
  • std::list and std::forward_list have member remove and remove_if that really erase (C++20 makes them return the count). Prefer them to the free algorithm.
  • Unique elements. To remove consecutive duplicates use std::unique with the same two-step pattern.
  • Pointers. If the container owns raw pointers, removing does not delete what they point to; use smart pointers.
  • Added in C++20: std::erase and std::erase_if.