我正在努力了解如何back_inserter工作,这是我从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;
}
};
Run Code Online (Sandbox Code Playgroud)
我理解大多数部分,除了最后三个运算符*,++,++(int).我对它们存在的猜测是因为它们需要在置于STL算法内时支持操作.除此之外,我不知道他们用的是什么?任何人都可以帮我澄清一下吗?
谢谢,
陈
它们存在是因为STL算法在迭代器上工作,迭代器必须是post和pre incrementable并且有一个dereference运算符.
试着想一想这是做什么的:
(*back_inserter) = value;
++back_inserter;
Run Code Online (Sandbox Code Playgroud)