|
iterator insert ( iterator position, const T& x );
void insert ( iterator position, size_type n, const T& x );
template <class InputIterator>
void insert ( iterator position, InputIterator first, InputIterator last );
Insert elements
The list container is extended by inserting new elements before the element at position.
This effectively increases the container size by the amount of elements inserted.
lists are sequence containers specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence. Compared to the other base sequence containers (vector and deque), lists are the most efficient container doing insertions at some position other than the beginning or the end of the sequence, and, unlike in these, all of the previously obtained iterators and references remain valid after the insertion and refer to the same elements they were referring before.
The parameters determine how many elements are inserted and to which values they are initialized:
Parameters
- position
- Position in the container where the new elements are inserted.
iterator is a member type, defined as a bidirectional iterator type.
- x
- Value to be used to initialize the inserted elements.
T is the first template parameter (the type of the elements stored in the container).
- n
- Number of elements to insert. Each element is initialized to the value specified in x.
Member type size_type is an unsigned integral type.
- first, last
- Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted at position position.
Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
The template type can be any type of input iterator.
Return value
Only the first version returns a value, which is an iterator that points to the newly inserted element.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
// inserting into a list
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main ()
{
list<int> mylist;
list<int>::iterator it;
// set some initial values:
for (int i=1; i<=5; i++) mylist.push_back(i); // 1 2 3 4 5
it = mylist.begin();
++it; // it points now to number 2 ^
mylist.insert (it,10); // 1 10 2 3 4 5
// "it" still points to number 2 ^
mylist.insert (it,2,20); // 1 10 20 20 2 3 4 5
--it; // it points now to the second 20 ^
vector<int> myvector (2,30);
mylist.insert (it,myvector.begin(),myvector.end());
// 1 10 20 30 30 20 2 3 4 5
// ^
cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); it++)
cout << " " << *it;
cout << endl;
return 0;
}
|
Output:
mylist contains: 1 10 20 30 30 20 2 3 4 5
|
Complexity
Linear on the number of elements inserted (copy construction).
See also
list::splice | Move elements from list to list (public member function) |
list::merge | Merge sorted lists (public member function) |
|