C++ std::remove_if() Function

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

Definition

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());
CallWhat 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

ParameterDescription
first, lastRequired. The range to process.
pRequired. A predicate. Elements for which it returns true are removed.

Return Value

Return ValueDescription
IteratorPoints 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;
}

How It Works

  1. The lambda is true for negative numbers, which are the ones to remove.
  2. remove_if() moves 3, 4 and 9 to the front in their original order.
  3. 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;
}

How It Works

  1. Even numbers make the predicate true, so they are removed.
  2. The odd numbers keep their relative order.
  3. 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;
}

How It Works

  1. go, is and a have fewer than three letters, so they match.
  2. code and fun are kept in their original order.
  3. 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.

Next: replace()

Replaces every element equal to one value with another value.

replace() 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.

20 people found this page helpful