Lists are sequence containers that allow for the allocation of non-contiguous memory. List traversal is slower than vector traversal, but once a position is found, insertion and deletion are fast. Normally, when we talk about a List, we mean a doubly linked list. We use a forward list to implement a singly linked list.
Given a list, the task is to remove all the elements from the list which satisfies the given condition
Examples:
Input:
intlist({ 1, 3, 5, 6, 7, 5, 2, 3, 5, 9, 8 }) element=5
Output:
1 3 6 7 2 3 9 8
Remove Elements from a List based on the given Condition
The member functions std::list::remove and std::list::remove_if are used to remove elements based on their value.
Method #1:Using list::remove
void remove (const value_type& value);
It removes all items from a list matching the passed element value.
Let us remove all the occurrences of element ‘5’ from the list.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating a integer list and initialize with some // elements list<int> intlist({ 1, 3, 5, 6, 7, 5, 2, 3, 5, 9, 8 }); // removing '5' element from the given intlist intlist.remove(5); // print the values of the list for (int value : intlist) cout << value << " "; return 0; }
Output:
1 3 6 7 2 3 9 8
Method #2:Using remove_if
We deleted all items from a list that match the passed values in the preceding example. However, this may not always be the requirement. Some elements must be removed from a list many times based on certain criteria Like,
Remove from the list all items greater than 1 but less than 6 .
In order to delete every element in the list with a criterion, we use another members function of std::list
template <class Predicate> void remove_if (Predicate predCallback);
As an argument, std::list::remove if accepts a Callback inform of a Predicate. Then it iterates through the list of elements, calling our Predicate on each one. Elements in the list that have a Predicate that returns true will be removed.
This Predicate is a Callback in this case, which can be,
- Function Object
- Function Lambda
- Function Object
Let’s look at how to use remove if and lambda to delete all elements from a list whose value is greater than or equal to 1 but less than 6.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // creating a integer list and initialize with some // elements list<int> intlist({ 1, 3, 5, 6, 7, 5, 2, 3, 5, 9, 8 }); // removing all the elements which are greater than 1 // and less than 6 intlist.remove_if([](const int& value) { if (value > 1 && value < 6) return true; else return false; }); // print the values of the list for (int value : intlist) cout << value << " "; return 0; }
Output:
1 6 7 9 8
Related Programs:
- how to remove elements from a list while iterating in c
- python how to remove multiple elements from list
- python how to remove duplicates from a list
- how to erase elements from a list in cpp using iterators
- python program to remove the ith occurrence of the given word in a list where words can repeat
- python how to remove characters from a string by index
- python how to create a list of all the keys in the dictionary