Remove vs Erase

What is the difference between std::remove() and std::erase() in C++?

The difference between std::remove() and std::erase() is crucial in understanding how to manipulate containers effectively in C++.

std::remove() is an algorithm found in the <algorithm> header. It does not change the size of the container but reorders the elements such that the elements to be "removed" are moved to the end.

It returns an iterator to the new end of the range, beyond which the elements are considered to be removed. However, the container itself still holds those elements.

On the other hand, erase() is a member function of containers like std::vector, std::list, and std::deque. It actually removes elements from the container and adjusts the container's size accordingly.

Here's an example to illustrate the difference:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> Source{1, 2, 3, 4, 5, 3, 6};

  // Using std::remove()
  auto NewEnd = std::remove(Source.begin(),
    Source.end(), 3);

  std::cout << "After std::remove(): ";
  for (auto it = Source.begin();
    it != Source.end(); ++it) {
    std::cout << *it << ", ";
  }
  std::cout << "\nSize after std::remove(): "
    << Source.size() << "\n";

  // Using erase() to actually remove the elements
  Source.erase(NewEnd, Source.end());

  std::cout << "After erase(): ";
  for (auto Num : Source) {
    std::cout << Num << ", ";
  }
  std::cout << "\nSize after erase(): "
    << Source.size() << "\n";
}
After std::remove(): 1, 2, 4, 5, 6, 3, 6,
Size after std::remove(): 7
After erase(): 1, 2, 4, 5, 6,
Size after erase(): 5

In the example above:

  • std::remove() moves the 3s to the end and returns an iterator to the new end, but the size of Source remains unchanged.
  • erase() then actually removes the surplus elements and updates the container size.

Understanding the distinction between these two functions is critical for effective container management in C++.

Removal Algorithms

An overview of the key C++ standard library algorithms for removing objects from containers. We cover remove(), remove_if(), remove_copy(), and remove_copy_if().

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Remove Elements Without Surplus
How can I remove elements from a container without leaving surplus elements at the end?
Memory Management with std::remove()
How do I handle memory management when using std::remove() with dynamic arrays?
Remove and Erase in std::map
How do I remove elements from a std::map using the remove-erase idiom?
Remove with Multiple Conditions
How can I remove elements from a container based on multiple conditions using std::remove_if()?
Remove Copy with Custom Comparator
Is there a way to use remove_copy_if() with a custom comparator function?
Remove Elements from Ordered Containers
How do I remove elements from a std::set while maintaining its sorted property?
Remove and Erase in std::deque
How do I remove elements from a std::deque using the remove-erase idiom?
Efficiently Remove Duplicates
How do I efficiently remove duplicate elements from a container using std::ranges::remove()?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant