C++ std::remove() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::remove() shifts the elements you want to keep to the front of the range. It returns an iterator to the new logical end.

It does not shrink the container. To really delete the leftover elements, follow it with erase(). This pair is called the erase-remove idiom.

Syntax

Add #include <algorithm>, then call std::remove(). Start with these two lines.

C++
auto newEnd = std::remove(v.begin(), v.end(), 2);
v.erase(newEnd, v.end());
CallWhat it does
auto newEnd = std::remove(v.begin(), v.end(), 2);Move the unwanted 2s out of the way.
v.erase(newEnd, v.end());Delete the leftover tail so the size shrinks.
Library declarations
C++
template<class ForwardIt, class T>
ForwardIt remove(ForwardIt first, ForwardIt last, const T& value);

Parameters

ParameterDescription
first, lastRequired. The range to process.
valueRequired. Elements equal to this value are removed.

Return Value

Return ValueDescription
IteratorPoints just after the last element that was kept. The elements from there to last have unspecified values.

C++ Examples

Example 1 — Erase-remove idiom

Delete every 2 from a vector.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {1, 2, 3, 2, 4, 2};

    auto newEnd = std::remove(v.begin(), v.end(), 2);
    v.erase(newEnd, v.end());

    std::cout << "Size: " << v.size() << '\n';
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. remove() slides 1, 3 and 4 to the front and returns the new end.
  2. erase() then deletes everything from that point to the old end.
  3. The vector now has three elements.

Example 2 — Without erase

See why the size does not change on its own.

C++
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {5, 0, 5, 0, 7};

    auto it = std::remove(v.begin(), v.end(), 0);

    std::cout << "Size still: " << v.size() << '\n';
    std::cout << "Kept elements: " << (it - v.begin()) << '\n';
    for (auto p = v.begin(); p != it; ++p) {
        std::cout << *p << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. remove() only rearranges elements. It never calls erase().
  2. The size is still 5, but the first three elements are the kept values.
  3. We print only up to it, because the rest is unspecified.

Example 3 — Strip spaces from text

Use the same idiom on a string.

C++
#include <algorithm>
#include <iostream>
#include <string>

int main() {
    std::string s = "a b c d";

    s.erase(std::remove(s.begin(), s.end(), ' '), s.end());

    std::cout << s << '\n';
    return 0;
}

How It Works

  1. std::remove() pushes the letters to the front, past the spaces.
  2. It returns an iterator to the end of the letters.
  3. erase() cuts off the leftover tail, so abcd is all that remains.

Frequently Asked Questions

In C++, std::remove() moves all elements not equal to a value to the front and returns the new end. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the new end of the kept elements. The container size does not change until you call erase().
Algorithms work with iterators and cannot change a container's size. Call erase() with the returned iterator to delete the leftover elements.
Yes. std::erase(v, value) removes the elements and shrinks the vector in one call.

Next: remove_copy()

Copies a range while leaving out elements equal to a value.

remove_copy() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

17 people found this page helpful