I’m trying to understand how back_inserter work, and this is its implementation that I have from SGI-STL:
template<class C>
class back_insert_iterator {
protected:
C* container;
public:
typedef C container_type;
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
explicit back_insert_iterator( C& __x ) :container( &__x ) {
}
back_insert_iterator<C>& operator=( const typename C::value_type& val ) {
container->push_back( val );
return *this;
}
back_insert_iterator<C>& operator*() {
return *this;
}
back_insert_iterator<C>& operator++() {
return *this;
}
back_insert_iterator<C>& operator++( int ) {
return *this;
}
};
I understood most parts, except the last three operator *, ++, ++( int ). My guess for their existence is because they need to support operations when placed inside the STL algorithm. Other than that, I don’t know what are they used for? Could anyone help me clarify this?
Thanks,
Chan
They exist because STL algorithms work on iterators which must be post and pre incrementable and have a dereference operator.
try to think what this does: