How to remove elements from a List based on value or External Criterion

In this article we will discuss how to remove an element from a List by matching a value or by matching some criterion.

std::list provides two member functions for removing elements based on value i.e. std::list::remove and std::list::remove_if.

Using std::list::remove to remove element by value

void remove (const value_type& val);

It removes all the elements from a list that matches the passed element val. Lets see an example, in which we will delete all the occurrences of 4 from a list i.e.

#include <iostream>
#include <list>

int main() {
	// Create a list and initialize it with 7 elements
	std::list<int> listOfInts( { 2, 3, 4, 6, 4, 9, 1, 2, 8, 9, 4, 6, 2 });

	// Remove all elements with value 4
	listOfInts.remove(4);

	// Iterate over the list using range based loop
	for (int val : listOfInts)
		std::cout << val << ",";
	std::cout << std::endl;

	return 0;
}

Output:

2,3,6,9,1,2,8,9,6,2,

To compile the above code use following command,

g++ –std=c++11 example.cpp

Using std::list::remove_if to remove elements from a List based on Criterion

In previous example we deleted all the elements from a List that matched the passed values. But this might not be the requirement every time. Many times we need to delete certain elements from a list based on certain criterion Like,

Deleting all elements which are greater than 2 but less than 5 from the list.

To achieve this kind of deletion where every element of list need to verified against a criterion, we will use an another member function of std::list i.e.

template <class Predicate>
void remove_if (Predicate predCallback);

std::list::remove_if accepts a Callback inform of a Predicate as an argument. Then it iterates over all the elements from the list and call our Predicate on each of them. Elements for which Predicate returns true, will get deleted from the list.

[showads ad=inside_post]

 

Here, this Predicate is a Callback, that can be,

  • Function Pointer
  • Function Object
  • Lambda Function

Lets see how to delete all elements from a list whose value is greater than or equal to 2 but less than 5 using remove_if & lambda function,

#include <iostream>
#include <list>
#include <algorithm>

int main() {
	// Create a list and initialize it with 7 elements
	std::list<int> listOfInts( { 2, 3, 4, 6, 4, 9, 1, 2, 8, 9, 4, 6, 2 });

	// Remove only first occurrence of element with value 4

	listOfInts.remove_if([](const int & val) {
		if(val >= 2 && val < 5)
		return true;
		else
		return false;
	});

	// Iterate over the list using range based loop
	for (int val : listOfInts)
		std::cout << val << ",";
	std::cout << std::endl;

	return 0;
}

Output:

6,9,1,8,9,6,

To Compile the above code use following command,

g++ –std=c++11 example.cpp

Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top