C++ - List splice() Function
The C++ list::splice function is used to delete all occurrences of specified element from the list container.
Syntax
//entire list void splice (iterator position, list& x); //single element void splice (iterator position, list& x, iterator i); //range of elements void splice (iterator position, list& x, iterator first, iterator last);
//entire list void splice (const_iterator position, list& x); void splice (const_iterator position, list&& x); //single element void splice (const_iterator position, list& x, const_iterator i); void splice (const_iterator position, list&& x, const_iterator i); //range of elements void splice (const_iterator position, list& x, const_iterator first, const_iterator last); void splice (const_iterator position, list&& x, const_iterator first, const_iterator last);
Parameters
val |
Specify the value of the element which need to be spliced from the list. |
Return Value
None.
Time Complexity
Linear i.e, Θ(n).
Example:
In the below example, the list::splice 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.splice(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