
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
List Remove and List Remove If in C++ STL
Given is the task to show the functionality list remove( ) and list remove_if( ) function in C++ in STL.
What is List in STL?
List are containers that allow constant time insertion and deletion anywhere in sequence. List are implemented as doubly linked lists. List allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.
What is remove( )?
This function is used to remove the given value passed in parameter to function.
Syntax
listname.remove(val);
Parameter
val − It defines the value to be removed.
Example
Input List: 1 2 3 3 4 5 Output New List: 1 2 4 5 In this List element 3 is removed. Input List: 5 6 7 8 8 8 9 Output New List: 5 7 8 8 8 9 In this list element 6 in removed
Approach can be followed
First we declare the List.
Then we print the List.
Then we define remove( ) function.
By using the above approach we can remove the given element.
Example
// C++ code to demonstrate the working of list remove( ) function in STL #include<iostream.h> #include<list.h> Using namespace std; int main ( ){ List<int> list = { 21, 24, 28, 26, 27, 25 }; // print the list cout<< " list: "; for( auto x = list.begin( ); x != list.end( ); ++x) cout<< *x << " "; // defining remove( ) function list.remove(27); cout<< " New List:”; for( x = list.begin( ); x != list.end( ); ++x) cout<<' " " << *x; return 0; }
Output
If we run the above code then it will generate the following output
Input - List: 21 24 28 26 27 25 Output - New List: 21 24 28 26 25 Input – List: 45 46 47 48 49 50 Output – New List: 45 46 48 49 50
What is remove_if( ) function?
This function is used to remove the values which returns true to predicate or return true for condition passed as parameter.
Syntax
listname.remove_if(predicate)
Parameter
predicate − It defines a conditional is passed as the parameter.
Example
Input – List: 5 6 7 8 9 10 Output – New List: 5 7 9 In this list we remove all the even elements. Input – List:5 10 15 20 25 30 Output – New List: 5 15 25 In this List we remove all the elements which is divisible by 10.
Approach can be followed
First we declare the predicate function.
Then we declare the list.
Then we print the list.
then we declare remove_if( ) function.
By using above approach we can remove the element in any given condition. While declaring the remove_if( ) function we pass the predicate as parameter.
Example
// C++ code to demonstrate the working of list remove_if( ) function in STL #include<iostream.h> #include<list.h> Using namespace std; Bool div3( const int& val){ return( val % 3) == 0); } int main( ){ List<int> list = { 2, 3, 4, 15, 9, 7, 21, 24, 13 }; cout<< " List: "; for( auto x = list.begin( ); x != list.end( ); ++x) cout<< *x << " "; // declaring remove_if( ) function list.remove_if(div3); cout<< " New List:”; for( x= list.begin( ); x != end( ); ++x) cout<< " " << *x; return 0; }
Output
If we run the above code then it will generate the following output
Input – List: 2 3 4 15 9 7 21 24 13 Output – New List: 2 4 7 13