First, insert method

function function
push_front() Adds a new element to the list before the first element
push_back() Adds a new element to the list after the last element
emplace_front() Generates a new element directly before the first element in the container
emplace_back() A new element is generated directly after the last element in the container
emplace() Generates a new element directly at the specified location of the container
insert() Inserts a new element at the specified location
splice() Adds elements from other List containers to the current List at the specified location

1. Insert method

format instructions
iterator insert(pos,elem) Inserts a new element elem before the position specified by iterator pos and returns an iterator representing the position of the newly inserted element
iterator insert(pos,n,elem) Inserts n elements elem before the position specified by iterator pos and returns an iterator representing the position of the first newly inserted element
iterator insert(pos,first,last) Inserts all elements in the [first,last] region of other containers (such as Array, Vector, deque, etc.) before the position specified by iterator pos, and returns an iterator representing the position of the first newly inserted element
iterator insert(pos,initlist) Before the position specified by iterator pos, insert all elements in the initializer list (multiple elements enclosed in braces {}, separated by commas) and return an iterator representing the position of the first newly inserted element

2. Splice method

format instructions
void splice (iterator position, list& x); Unit 2
Unit 3 Position is an iterator that indicates the insertion position; X is another list container. The splice() method in this format moves all elements stored in the X container to the position specified in the current List container
void splice (iterator position, list& x, iterator i); Position is an iterator that indicates the insertion position; X is another list; I is also an iterator that refers to an element in the X container. The splice() method in this format moves the element pointed to by I in the X container to the position specified in the current container.
void splice (iterator position, list& x, iterator first, iterator last); Position is an iterator that indicates the insertion position; X is another list; First and last are iterators. [fist,last] is used to specify an area of X. The splice() method in this format moves all elements in the scope of X [first, last] to the position specified in the current container.

How the splice() member method moves elements

Remove the node that stores the element from the list at the bottom of the list and link it to the list at the bottom of the current list. When an element from the list1 container is added to the list2 container using the splice() member method, the element is removed from the list1 container.

Second, the instance

This is mainly an instance of the Splice method

1. Method 1

STD ::list<int> listInt1{16,72,100}, listInt2{9,201,94}; std::list<int>::iterator listIter = ++listInt1.begin(); **listInt1.splice(listIter, listInt2); ** int i = 0; std::list<int>::iterator listIter1 = listInt1.begin(); std::list<int>::iterator listIter2 = listInt2.begin(); for (; listIter1 ! = listInt1.end(); listIter1++) { std::cout << " splice1 listInt1[" << i++ << "]=" << *listIter1 << std::endl; } for (; listIter2 ! = listInt2.end(); listIter2++) { std::cout << " splice1 listInt2[" << i++ << "]=" << *listIter2 << std::endl; }

The results are as follows:

2. Method 2

STD ::list<int> listInt1{16,72,100}, listInt2{9,201,94}; std::list<int>::iterator listIter = ++listInt1.begin(); **listInt2.splice(listInt2.begin(), listInt1, listIter); ** int i = 0; std::list<int>::iterator listIter1 = listInt1.begin(); std::list<int>::iterator listIter2 = listInt2.begin(); for (; listIter1 ! = listInt1.end(); listIter1++) { std::cout << " splice1 listInt1[" << i++ << "]=" << *listIter1 << std::endl; } for (; listIter2 ! = listInt2.end(); listIter2++) { std::cout << " splice1 listInt2[" << i++ << "]=" << *listIter2 << std::endl; }

The results are as follows:

3. Method 3

STD ::list<int> listInt1{16,72,100}, listInt2{9,201,94}; std::list<int>::iterator listIter = ++listInt1.begin(); ** listInt2.splice(listInt2.begin(), listInt1, listInt1.begin(), listInt1.end()); ** int i = 0; std::list<int>::iterator listIter1 = listInt1.begin(); std::list<int>::iterator listIter2 = listInt2.begin(); for (; listIter1 ! = listInt1.end(); listIter1++) { std::cout << " splice1 listInt1[" << i++ << "]=" << *listIter1 << std::endl; } for (; listIter2 ! = listInt2.end(); listIter2++) { std::cout << " splice1 listInt2[" << i++ << "]=" << *listIter2 << std::endl; }

The results are as follows: