1. Introduction to Remove and Remove_if
The Remove_if predicate is a user function. It tells when an item in the C++ list can be removed. The Standard Library function Remove_if will accepts the Predicate function and removes the list items as instructed by the predicate. The remove function removes an item by its value. In this example, we will learn both Remove and Remove_if functions.
2. Supporting Functions For This Example
2.1 Add Default Elements to C++ List
The below function
Add_ListElements
adds default items to the list. First, it clears the list and then adds six integers (from 1 to 6) to the list. This function takes the list as a reference argument so that the changes reflected to the caller. Have a look at the Listing 2.1 at the end of this section.
1 2 3 4 5 6 7 8 9 10 11 |
//Example 01: Function to Push some values to List void Add_ListElements(list<int>& listParam) { listParam.clear(); listParam.push_back(1); listParam.push_back(2); listParam.push_back(3); listParam.push_back(4); listParam.push_back(5); listParam.push_back(6); } |
2.2 Iterate the C++ List
The function
Print_List
takes the standard list as a argument and iterates through its each element through an iterator. On each iteration, it prints the content of the list element to console output window. The code listing is below:
1 2 3 4 5 6 7 8 9 10 |
//Example 02: Function to Print the Values in List void Print_List(list<int> listParam) { list<int>::iterator listItr; printf("\nThe contents of the Lists are:\n"); for (listItr = listParam.begin(); listItr != listParam.end(); listItr++) printf("[%d] ", *listItr); } |
3. The Std::list::remove Example
In the main() function of this sample application, we create a standard C++ List called theList. Then, we add six items to it by calling the function
Add_ListElements
. After filling the list with default items, we hand over it to the function Print_List to display list content in the console output window. The list at this moment is shown below:

Std.List Before calling remove function
The
remove()
function of the C++ Standard Library iterates the List and removes the element(s) by matching it with the value passed in. For example, the function call
theList.Remove(2)
searches the list and removes all the elements which has the value of 2. In our example, we are removing the values 2,4,6 from the list by calling the remove function three times. The below illustration explains this:

The effect of std.list.remove() function on the c++ list
The complete example and program output is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <stdio.h> #include <conio.h> #include <list> using namespace std; //Example 01: Function to Push some values to List void Add_ListElements(list<int>& listParam) { listParam.clear(); listParam.push_back(1); listParam.push_back(2); listParam.push_back(3); listParam.push_back(4); listParam.push_back(5); listParam.push_back(6); } //Example 02: Function to Print the Values in List void Print_List(list<int> listParam) { list<int>::iterator listItr; printf("\nThe contents of the Lists are:\n"); for (listItr = listParam.begin(); listItr != listParam.end(); listItr++) printf("[%d] ", *listItr); } //Example 01: Clear void main() { //1.0 Create list and iterator list<int> theList; //2.0 Let us push some elements to the List Add_ListElements(theList); //3.0 Print Content of the List Print_List(theList); //4.0 Remove Values 2,4,6 theList.remove(2); theList.remove(4); theList.remove(6); Print_List(theList); _getch(); } |

Std.list.remove example program output
4. The remove_if & Its Predicate
The
remove_if
function of function C++ Standard Library List takes user defined function as an Argument. The user-defined function tells the
remeove_if
function under what condition an item can be removed. In C++ world the user-defined function is termed as ‘Predicate Function‘. The Predicate Function takes only one argument which must match with the type of items kept in the List. For example, in our case we store integers and hence predicate function should take integer as a argument and should return a Boolean type.
4.1 Remove_if Predicate to Remove Even numbers
Our First Predicate function
even_number
will check the passed in value and returns true when the number is an even number. Below is the first remove_if Predicate Function:
1 2 3 4 5 6 7 8 |
//Example 03a: Predicate to Remove even numbers bool even_number(int param) { if (param % 2 == 0) return true; else return false; } |
4.2 Remove_if Predicate to Remove Number Divisible by 5
Our second Predicate Function
Divby5
return true when a number is divisible by 5. Below the second Predicate Function:
1 2 3 4 5 6 7 8 |
//Example 03b: Predicate to Remove divisible by 5 bool Divby5(int param) { if (param % 5 == 0) return true; else return false; } |
Note that both the example takes an integer as a argument.
4.3 Using the C++ Remove_If Function
This is because our list is created to store integer values. Now when we pass these Predicate Functions to the “std::list::Remove_if()”, it iterates through the List and calls the predicate function for each item in the list. In Addition, it removes the items whenever the predicate function returns true. Now have a look at the example code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//Example 01: Clear List through Remove_if and Predicates void main() { //1.0 Create list and iterator list<int> theList; //2.0 Let us push some elements to the List Add_ListElements(theList); //3.0 Print Content of the List Print_List(theList); //4.0 Remove even numbers printf("\nCalling Remove_if with even_number predicate"); <strong> theList.remove_if(even_number);</strong> Print_List(theList); //5.0 Remove all numbers divisible by 5 printf("\nCalling Divby5 predicate"); <strong> theList.remove_if(Divby5);</strong> Print_List(theList); _getch(); } |
In the example code above, at line 8 we are populating the list by calling the
Add_ListElements()
. Now list will have the values 1,2,3,4,5,6. At line 15, we are making the call to the
remove_if()
function and we supply the even_number predicate to it. The standard library function remove_if will iterate through each item in the list and calls the predicate function in each iteration. The
remove_if
will pass the list item found in each iteration to the predicate function. When the predicate function returns true, it removes the element in the current iteration and advances to the next element in the list. At line 19, we are calling the
remove_if
function one more time. This time, we supply the predicate function
Divby5
. After calling
remove_if
twice, the list will have 1,3 and this is shown in the below picture:

Remove_If function with predicates explained
The complete code example and its output is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include <stdio.h> #include <conio.h> #include <list> using namespace std; //Example 01: Function to Push some values to List void Add_ListElements(list<int>& listParam) { listParam.clear(); listParam.push_back(1); listParam.push_back(2); listParam.push_back(3); listParam.push_back(4); listParam.push_back(5); listParam.push_back(6); } //Example 02: Function to Print the Values in List void Print_List(list<int> listParam) { list<int>::iterator listItr; printf("\nThe contents of the Lists are:\n"); for (listItr = listParam.begin(); listItr != listParam.end(); listItr++) printf("[%d] ", *listItr); } //Example 03a: Predicate to Remove even numbers bool even_number(int param) { if (param % 2 == 0) return true; else return false; } //Example 03b: Predicate to Remove divisible by 5 bool Divby5(int param) { if (param % 5 == 0) return true; else return false; } //Example 01: Clear List through Remove_if and Predicates void main() { //1.0 Create list and iterator list<int> theList; //2.0 Let us push some elements to the List Add_ListElements(theList); //3.0 Print Content of the List Print_List(theList); //4.0 Remove even numbers printf("\nCalling Remove_if with even_number predicate"); theList.remove_if(even_number); Print_List(theList); //5.0 Remove all numbers divisible by 5 printf("\nCalling Divby5 predicate"); theList.remove_if(Divby5); Print_List(theList); _getch(); } |

Remove_if Example Output
Categories: C++
Tags: list::remove, list::remove_if, remove_if predicate