< cpp‎ | iterator
Revision as of 15:40, 30 December 2012 by SuperBoi45 (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <iterator>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class Container >

class back_insert_iterator : public std::iterator< std::output_iterator_tag,

                                                   void, void, void, void >

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

std::back_insert_iterator is an OutputIterator that appends to a container for which it was constructed, using the container's push_back() member function whenever the iterator (whether dereferenced or not) is assigned to. Incrementing the std::back_insert_iterator is a no-op.


Member types

Member type Definition
container_type Container

Member functions

Template:cpp/iterator/inserter/dcl list constructorTemplate:cpp/iterator/inserter/dcl list operator=Template:cpp/iterator/inserter/dcl list operator*Template:cpp/iterator/inserter/dcl list operator++

Member objects

Member name Definition
container (protected) a pointer of type Container*

Member types

Member type Definition
value_type void
difference_type void
pointer void
reference void
iterator_category std::output_iterator_tag

Note: before C++17, these member types are required to be obtained by inheriting from std::iterator<std::output_iterator_tag,void,void,void,void>.


#include <iostream>
#include <iterator>
#include <algorithm>
#include <cstdlib>
int main()
    std::vector<int> v;
    std::generate_n(std::back_insert_iterator<std::vector<int>>(v), // can be simplified
                    10, [](){ return std::rand() % 10; });        // with std::back_inserter
    for (int n : v)
        std::cout << n << ' ';
    std::cout << '\n';


3 6 7 5 3 5 6 2 9 1

See also

Template:cpp/iterator/dcl list back inserterTemplate:cpp/iterator/dcl list front insert iteratorTemplate:cpp/iterator/dcl list insert iterator