The insert_iterator
is an iterator adapter that acts as an output iterator, but which converts expressions of the form *itr = value
into calls to insert
on an STL container. The insert_iterator
constructor takes in a reference to another STL container class that should have insert
invoked on it, as well as an iterator indicating where to begin insertion. All STL container class (except for the container adapters and bitset
) support insert
, so this iterator class can be used on both vector
s and set
s.
Because insert
's behavior varies from container to container, this iterator class may or may not be appropriate for the task at hand. For vector
, deque
, and list
, insert
places the new element at the spot specified by the iterator. The insert_iterator
then advances the iterator forward one step. Consequently, if an insert_iterator
is used to add the values (0, 1, 2, 3)
into an STL vector
containing (100, 101, 102, 103
at the spot directly following 101
, the result will be (100, 101, 0, 1, 2, 3, 102, 103)
. For associative containers like map
and set
, insert
will automatically insert the element at the correct location, so an insert_iterator
will correctly place the elements inside the container.
When using an insert_iterator
, be sure that you are aware of the time costs of insertions into the specified container. For vector
, insertions anywhere other than at the end of the vector
are O(n) in the size of the vector
and it may be preferrable to use a back_insert_iterator
instead. Similarly, deque
insert operations anywhere except at the beginning or end of the container are O(n), so you may want to use a back_insert_iterator
or front_insert_iterator
instead. For the list
class and the associative containers like map
and set
, insertions are quite fast and an insert_iterator
may be ideal for the job.
insert_iterator
is a template type whose argument is the type of container to which the insert_iterator
should forward its requests. For example, to invoke insert
on a container of type set<int>
, you would use an insert_iterator<set<int> >
. Remember to include the extra space between the closing angle brackets or you will get a compile-time error!
Since the above syntax can get a bit clunky, the STL provides a utility function, inserter
, which creates insert_iterator
s.
insert_iterator<Container>
Utility FunctionsConstructs a new insert_iterator
that calls insert
on the element specified as the parameter when dereferenced. It is preferable to create insert_iterator
s using inserter
rather than through a constructor call, since the compiler will figure out the correct type of insert_iterator
to create. Since in most cases you will create insert_iterator
s as parameters to functions, this function also lets you avoid writing out the type of the insert_iterator
longhand.
insert_iterator<Container>
Member Functionsinsert_iterator
on the specified element at the specified position.Constructs a new insert_iterator
that calls insert} on the element specified as the parameter when dereferenced. The
start
parameter indicates where to begin inserting; its meaning changes based on the container. See the above section for more information.