In C++, std::list::remove() erases every element that is equal to the value you give. The matching nodes are unlinked and destroyed, and the list gets shorter.
This is different from the std::remove() function in <algorithm>, which only shuffles elements and needs a second erase() call. The list member does everything in one step. 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 with the value to delete. Every element that equals it is erased.
C++
l.remove(2); // erase all 2s
auto n = l.remove(7); // C++20: n = how many were erased
Call
What it does
l.remove(value)
Erases all elements equal to value (compared with ==). size() shrinks by the number of matches.
auto n = l.remove(value)
In C++20 and later, the number of erased elements is returned.
To erase by a rule such as “all even numbers”, use remove_if(). To erase one element by position, use erase().
Library declarations
C++
void remove(const T& value); // before C++20
size_type remove(const T& value); // since C++20
Parameters
Parameter
Description
value
Required. Elements that are equal to this value (*it == value) are erased.
Return Value
Return Value
Description
size_type (C++20)
The number of elements that were erased. Before C++20 the function returns void.
C++ Examples
Example 1 — remove() Erases Every Match
All three 2s are erased in one call, and size() shows the list really got shorter.
The list holds six numbers, and the value 2 appears three times.
remove(2) goes through the list and erases every node that holds 2. No extra erase() is needed.
The size drops from 6 to 3, and the loop prints the survivors 1 3 4 in their old order.
Example 2 — remove() Returns the Count (C++20)
Since C++20, remove() tells you how many elements it erased. Compile with -std=c++20.
C++
#include <iostream>
#include <list>
int main() {
std::list<int> l = {7, 1, 7, 7, 2};
auto removed = l.remove(7);
std::cout << "removed " << removed << '\n';
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
}
Output
removed 3
1 2
How It Works
The value 7 appears three times in the list.
In C++20 and later, remove(7) returns the number of erased elements, so removed is 3. In older standards the return type is void.
The two other numbers, 1 and 2, are left in order.
Example 3 — remove() with Strings
It works for any type that has ==, such as std::string.
C++
#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::string> words = {"cat", "dog", "cat", "bird"};
words.remove("cat");
for (const std::string& w : words) {
std::cout << w << ' ';
}
std::cout << '\n';
return 0;
}
Output
dog bird
How It Works
The text literal "cat" is converted to a std::string and compared with each element using ==.
Both nodes that hold "cat" are unlinked and destroyed.
The list now has two words, dog and bird, still in their original order.
Frequently Asked Questions
It erases all elements of the list that are equal to a value. The list becomes shorter right away.
std::remove() from <algorithm> only moves the kept elements to the front and returns a new end, so you still must call erase(). list::remove() erases the elements itself.
No. It removes every element that is equal to the value. To erase just one element, find it and call erase() with its iterator.
In C++20 and later it returns the number of erased elements. In C++17 and older it returns void.
Use remove_if() with a function or a lambda, for example l.remove_if([](int x) { return x % 2 == 0; });