inserter
function template
<iterator>
template <class Container, class Inserter>
insert_iterator<Container> inserter (Container& x, Inserter i);
Construct an insert iterator
This function generates an insert iterator for a container.
An insert iterator is a special type of output iterator specifically designed to allow algorithms that usually overwrite elements (such as copy) to instead insert new elements in the container.
Parameters
- x
- Container for which the insert iterator is constructed.
- i
- Iterator to the location in x where the element are to be inserted.
Return value
An insert_iterator that inserts elements in the container x at the location pointed by i.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// inserter example
#include <iostream>
#include <iterator>
#include <list>
using namespace std;
int main () {
list<int> firstlist, secondlist;
for (int i=1; i<=5; i++)
{ firstlist.push_back(i); secondlist.push_back(i*10); }
list<int>::iterator it;
it = firstlist.begin(); advance (it,3);
copy (secondlist.begin(),secondlist.end(),inserter(firstlist,it));
for ( it = firstlist.begin(); it!= firstlist.end(); ++it )
cout << *it << " ";
cout << endl;
return 0;
}
|
Output:
See also
back_inserter | Construct a back insert iterator (function template) |
|