Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Remove_if Predicate and Remove Examples

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.

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:

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

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 effect of std.list.remove() function on the c++ list

The complete example and program output is below:

Std.list.remove example program output

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:

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:

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:

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

Remove_If function with predicates explained

The complete code example and its output is below:

Remove_if Example Output

Remove_if Example Output

 

Categories: C++

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.