| inserter |
function template |
template <class Container, class Inserter>
insert_iterator<Container> inserter (Container& x, Inserter i); |
<iterator> |
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
// 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) |