C++ std::list remove_if() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

Definition

In C++, std::list::remove_if() erases every element for which a test function (a predicate) returns true. You write the test as a lambda or a normal function.

Like remove(), the list member erases the elements itself, so you do not need a separate erase() call. Include <list> to use it.

This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.

Syntax

Call remove_if with a function that takes one element and returns true for elements that should be erased.

C++
l.remove_if([](int x) { return x % 2 == 0; });   // erase evens
auto n = l.remove_if(pred);                       // C++20: count
CallWhat it does
l.remove_if(pred)Calls pred(element) for each element and erases it when the result is true.
auto n = l.remove_if(pred)In C++20 and later, the number of erased elements is returned.

If you only need to compare with one fixed value, remove(value) is shorter.

Library declarations
C++
template<class UnaryPredicate>
void remove_if(UnaryPredicate p);              // before C++20

template<class UnaryPredicate>
size_type remove_if(UnaryPredicate p);         // since C++20

Parameters

ParameterDescription
pRequired. A function, lambda, or function object. It receives one element and returns true if that element should be erased.

Return Value

Return ValueDescription
size_type (C++20)The number of elements that were erased. Before C++20 the function returns void.

C++ Examples

Example 1 — remove_if() with a Lambda

Erase every even number from a list of eight numbers.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> l = {1, 2, 3, 4, 5, 6, 7, 8};

    l.remove_if([](int x) { return x % 2 == 0; });

    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << "(size " << l.size() << ")\n";
    return 0;
}

How It Works

  1. The lambda [](int x) { return x % 2 == 0; } returns true for even numbers.
  2. remove_if() calls it once for every element and erases the nodes where the answer is true.
  3. Four odd numbers are left, 1 3 5 7, and size() already shows 4.

Example 2 — remove_if() with a Captured Limit

A lambda can use a variable from outside. Here the limit is stored in limit.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> l = {10, 60, 30, 80, 50};
    int limit = 50;

    l.remove_if([limit](int x) { return x > limit; });

    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. [limit] in the lambda brackets copies the variable limit into the lambda, so the test can use it.
  2. The test x > limit is true for 60 and 80, so those two nodes are erased.
  3. The value 50 is kept, because 50 > 50 is false. The result is 10 30 50.

Example 3 — remove_if() to Drop Empty Strings

Clean a list of words by erasing the empty ones.

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

int main() {
    std::list<std::string> l = {"a", "", "b", "", "c"};
    std::cout << "before: " << l.size() << '\n';

    l.remove_if([](const std::string& s) { return s.empty(); });

    std::cout << "after: " << l.size() << '\n';
    for (const std::string& s : l) {
        std::cout << s << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The list has five strings. Two of them are empty strings "".
  2. The lambda takes const std::string& so that no string is copied, and returns s.empty().
  3. The two empty nodes are erased, the size drops to 3, and the words a b c keep their order.

Frequently Asked Questions

It erases every element for which the predicate you give returns true. The list shrinks at once.
remove(value) compares each element with one fixed value using ==. remove_if(pred) lets you write any test as a function or lambda.
No. The list member erases the elements itself. You only need the erase-remove idiom with std::vector and std::deque.
In C++20 and later it returns the number of erased elements. In C++17 and older it returns void.
It should not. The predicate must only look at the element and return true or false, without changing the element or the list.

Next: rend()

Get a reverse iterator one place before the first element with std::list.

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

7 people found this page helpful