Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Algorithms

The std::remove and std::remove_if algorithms

Reference for C++ std::remove, remove_if and remove_copy: why they do not shrink the container, the erase-remove idiom and std::erase in C++20.

std::remove moves the elements you want to keep to the front of a range and returns an iterator to the new logical end. It does not change the size of the container: algorithms only see iterators, and iterators cannot remove anything from a std::vector. The elements after the returned iterator are still there, but they are in a valid-but-unspecified state, so you should not read them.

std::remove_if does the same with a predicate. To actually shrink the container, pass the returned iterator to the container's erase member (the erase-remove idiom). Since C++20 the free functions std::erase and std::erase_if do both steps in one call.

C++
v.erase(std::remove(v.begin(), v.end(), 0), v.end());
// C++20: std::erase(v, 0);

Try it

C++ The erase-remove idiom

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++ C++20 std::erase_if and remove_copy_if

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++
template<class ForwardIt, class T>
ForwardIt remove(ForwardIt first, ForwardIt last, const T& value);

template<class ForwardIt, class Pred>
ForwardIt remove_if(ForwardIt first, ForwardIt last, Pred pred);

template<class InputIt, class OutputIt, class T>
OutputIt remove_copy(InputIt first, InputIt last, OutputIt out, const T& value);

template<class InputIt, class OutputIt, class Pred>
OutputIt remove_copy_if(InputIt first, InputIt last, OutputIt out, Pred pred);

// C++20, in <vector>, <string>, <deque>, <list>, ...
template<class Container, class T> size_type erase(Container& c, const T& value);
template<class Container, class Pred> size_type erase_if(Container& c, Pred pred);
Name Description
first, last The half-open range [first, last) to process.
value Elements equal to it (by ==) are removed.
pred Elements for which it returns true are removed.
out Output iterator that receives the kept elements (remove_copy versions).
c The container for std::erase and std::erase_if.

Returns: remove and remove_if return the new logical end. remove_copy and remove_copy_if return the output iterator one past the last element written. std::erase and std::erase_if return the number of removed elements.

Errors / undefined behavior: none for valid ranges. Reading the elements in [new_end, last) gives unspecified (but valid) values, and for remove_copy the output must not overlap the input.

Usage notes

  • The container keeps its size. Forgetting the erase call is the classic mistake: the vector still has the same number of elements, with leftovers at the end.
  • Order is preserved. The kept elements stay in their original relative order, which makes remove stable.
  • Complexity: exactly last - first comparisons or predicate calls, one pass. Elements are moved, never copied, when possible.
  • Associative containers: you cannot use remove on std::set or std::map (their elements are const). Use the container's erase or std::erase_if (C++20).
  • std::list and std::forward_list have member remove and remove_if that really erase nodes. Before C++20 they were the only ones that did.
  • Raw pointers: if the elements are owning raw pointers, remove just overwrites them and leaks the removed ones. Use smart pointers.
  • Ranges versions: std::ranges::remove and remove_if (C++20) return a subrange [new_end, last), which you can pass to erase as two iterators.
  • Many removals from the middle of a large vector are O(n) in total with remove, whereas calling erase in a loop is O(nยฒ).