In C++, std::remove_if() removes the elements for which a predicate is true. Like std::remove(), it moves the kept elements to the front and returns the new end.
Combine it with erase() to actually delete the unwanted elements.
Syntax
Add #include <algorithm>, then call std::remove_if(). Start with these two lines.
C++
auto newEnd = std::remove_if(v.begin(), v.end(), pred);
v.erase(newEnd, v.end());
Call
What it does
auto newEnd = std::remove_if(v.begin(), v.end(), pred);
Move the elements to keep to the front.
v.erase(newEnd, v.end());
Delete the leftover tail.
Library declarations
C++
template<class ForwardIt, class UnaryPred>
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPred p);
Parameters
Parameter
Description
first, last
Required. The range to process.
p
Required. A predicate. Elements for which it returns true are removed.
Return Value
Return Value
Description
Iterator
Points just after the last kept element.
C++ Examples
Example 1 — Remove negatives
Delete every negative number.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, -1, 4, -5, 9};
v.erase(std::remove_if(v.begin(), v.end(), [](int n) { return n < 0; }), v.end());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
Output
3 4 9
How It Works
The lambda is true for negative numbers, which are the ones to remove.
remove_if() moves 3, 4 and 9 to the front in their original order.
erase() deletes the leftover tail.
Example 2 — Remove even numbers
Keep only the odd numbers.
C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
auto newEnd = std::remove_if(v.begin(), v.end(), [](int n) { return n % 2 == 0; });
v.erase(newEnd, v.end());
for (int x : v) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
Output
1 3 5 7
How It Works
Even numbers make the predicate true, so they are removed.
The odd numbers keep their relative order.
After erase(), the vector holds four elements.
Example 3 — Remove short words
Delete words with fewer than three letters.
C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = {"go", "code", "is", "fun", "a"};
words.erase(std::remove_if(words.begin(), words.end(),
[](const std::string& w) { return w.size() < 3; }),
words.end());
for (const std::string& x : words) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
Output
code fun
How It Works
go, is and a have fewer than three letters, so they match.
code and fun are kept in their original order.
The vector shrinks from five words to two.
Frequently Asked Questions
In C++, std::remove_if() moves elements that do not satisfy a condition 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. Use erase() to shrink the container.
remove() deletes elements equal to a value. remove_if() deletes elements that make a predicate return true.
Yes. The kept elements stay in their original relative order.