C++ - List merge() Function
The C++ list::merge function is used to delete all occurrences of specified element from the list container.
Syntax
void merge (list& x); template <class Compare> void merge (list& x, Compare comp);
void merge (list& x); void merge (list&& x); template <class Compare> void merge (list& x, Compare comp); template <class Compare> void merge (list&& x, Compare comp);
Parameters
val |
Specify the value of the element which need to be merged from the list. |
Return Value
None.
Time Complexity
Linear i.e, Θ(n).
Example:
In the below example, the list::merge function is used to delete all occurrences of specified element from the list called MyList.
#include <iostream> #include <list> using namespace std; int main (){ list<int> MyList{10, 20, 30, 40, 50, 30, 30, 40}; list<int>::iterator it; cout<<"MyList contains: "; for(it = MyList.begin(); it != MyList.end(); it++) cout<<*it<<" "; //Remove all occurrences of 30 from the list cout<<"\n\nRemove all occurrences of 30 from the MyList.\n"; MyList.merge(30); cout<<"Now, MyList contains: "; for(it = MyList.begin(); it != MyList.end(); it++) cout<<*it<<" "; return 0; }
The output of the above code will be:
MyList contains: 10 20 30 40 50 30 30 40 Remove all occurrences of 30 from the MyList. Now, MyList contains: 10 20 40 50 40
❮ C++ - List