The back_insert_iterator
is an iterator adapter that acts as an output iterator, but which converts expressions of the form *itr = value
into calls to push_back
on an STL container. The back_insert_iterator
constructor takes in a reference to another STL container class that should have push_back
invoked on it. Because the back_insert_iterator
calls push_back
on the element it wraps, the container type must support the push_back
function. Consequently, you cannot use a back_insert_iterator
on an STL map
or set
; for those classes, consider using insert_iterator
.
back_insert_iterator
is a template type whose argument is the type of container to which the back_insert_iterator
should forward its requests. For example, to invoke push_back
on a container of type vector<int>
, you would use a back_insert_iterator<vector<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, back_inserter
, which creates back_insert_iterator
s.
back_insert_iterator<Container>
Utility FunctionsConstructs a new back_insert_iterator
that calls push_back
on the element specified as the parameter when dereferenced. It is preferable to create back_insert_iterator
s using back_inserter
rather than through a constructor call, since the compiler will figure out the correct type of back_insert_iterator
to create. Since in most cases you will create back_insert_iterator
s as parameters to functions, this function also lets you avoid writing out the type of the back_insert_iterator
longhand.
back_insert_iterator<Container>
Member Functionsback_insert_iterator
on the specified element.Constructs a new back_insert_iterator
that calls push_back
on the element specified as the parameter when dereferenced.