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());
Call
What 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
Parameter
Description
first, last
Required. The range to process.
value
Required. Elements equal to this value are removed.
Return Value
Return Value
Description
Iterator
Points 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;
}
Output
Size: 3
1 3 4
How It Works
remove() slides 1, 3 and 4 to the front and returns the new end.
erase() then deletes everything from that point to the old end.
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;
}
Output
Size still: 5
Kept elements: 3
5 5 7
How It Works
remove() only rearranges elements. It never calls erase().
The size is still 5, but the first three elements are the kept values.
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;
}
Output
abcd
How It Works
std::remove() pushes the letters to the front, past the spaces.
It returns an iterator to the end of the letters.
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.