一. back_insert_iterator
功能:用於在後部插入元素, 迭代器類型: output_iterator_tag. 實現:
template <class _Container>
class back_insert_iterator {
protected:
_Container* container;
public:
typedef _Container 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(_Container& __x) : container(&__x) {}
back_insert_iterator<_Container>&
operator=(const typename _Container::value_type& __value) {
container->push_back(__value);
return *this;
}
back_insert_iterator<_Container>& operator*() { return *this; }
back_insert_iterator<_Container>& operator++() { return *this; }
back_insert_iterator<_Container>& operator++(int) { return *this; }
};
接收一個容器作爲構造的參數。 重載operator=實現插入。重載其他幾個迭代器的必須操作符, 但是不做任何處理, 用以滿足迭代器要求。另有一個全局模板函數來生成此迭代器, 如下:
template <class _Container>
inline back_insert_iterator<_Container> back_inserter(_Container& __x) {
return back_insert_iterator<_Container>(__x);
}
二. front_insert_iterator
除了是從前面而不是後面插入, 同back_insert_iterator基本差不多。生成函數:
template <class _Container>
inline front_insert_iterator<_Container> front_inserter(_Container& __x) {
return front_insert_iterator<_Container>(__x);
}
三. insert_iterator
除了構造需要多傳一個插入位置的參數外,和back_insert_iterator基本差不多。還有就是operator=有點差別, 因爲每次插入之後, 插入位置就失效了, 所以需要修改插入位置:
operator=(const typename _Container::value_type& __value) {
iter = container->insert(iter, __value);
++iter;
return *this;
}
生成函數:
template <class _Container, class _Iterator>
inline
insert_iterator<_Container> inserter(_Container& __x, _Iterator __i)
{
typedef typename _Container::iterator __iter;
return insert_iterator<_Container>(__x, __iter(__i));
}
四. reverse_bidirectional_iterator
功能: 實現反向迭代。 迭代器類型: bidirectional_iterator_tag, 四個模板參數template <class _BidirectionalIterator, class _Tp, class _Reference,
class _Distance>
其中爲什麼只傳入引用類型, 卻不需要傳入指針類型。 我的理解是, 類型爲_Tp, 那麼指針類型就一定是_Tp*, 但是, 引用類型卻不一定, 可能是某個其他的自定義類型,重載了operator*. 可以作爲_Tp的引用類型傳入, 只要對此類型解引用, 仍然可以得到值。從這個意義上講, iterator也應當屬於應用類型吧。實現:
template <class _BidirectionalIterator, class _Tp, class _Reference,
class _Distance>
class reverse_bidirectional_iterator {
typedef reverse_bidirectional_iterator<_BidirectionalIterator, _Tp,
_Reference, _Distance> _Self;
protected:
_BidirectionalIterator current;
public:
typedef bidirectional_iterator_tag iterator_category;
typedef _Tp value_type;
typedef _Distance difference_type;
typedef _Tp* pointer;
typedef _Reference reference;
reverse_bidirectional_iterator() {}
explicit reverse_bidirectional_iterator(_BidirectionalIterator __x)
: current(__x) {}
_BidirectionalIterator base() const { return current; }
_Reference operator*() const {
_BidirectionalIterator __tmp = current;
return *--__tmp;
}
pointer operator->() const { return &(operator*()); }
_Self& operator++() {
--current;
return *this;
}
_Self operator++(int) {
_Self __tmp = *this;
--current;
return __tmp;
}
_Self& operator--() {
++current;
return *this;
}
_Self operator--(int) {
_Self __tmp = *this;
++current;
return __tmp;
}
因爲current指向的位置並不是當前元素的位置,而是當前元素的下一個元素, 所以, operator*中的實現, 返回的是*--__tmp. 由於是雙向迭代器, 所以不僅實現了++, 還實現了--. 然後是一個迭代器的判等。operator->的含義是調用operator*得到當前所指向的值,然後對值取引用, 得到指向那個指的引用。
template <class _BiIter, class _Tp, class _Ref, class _Distance>
inline bool operator==(
const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __x,
const reverse_bidirectional_iterator<_BiIter, _Tp, _Ref, _Distance>& __y)
{
return __x.base() == __y.base();
}
五. reverse_iterator
是一個以迭代器爲模板參數的模版類, 實現了反向的迭代功能。 operator操作同reverse_bidirectional_iterator的實現. reverse_iterator至少是一個雙向迭代器,也有可能是一個隨機迭代器。 所以還實現了+=, -=, -, +, []等operator, 並且實現了更多的比較操作。 reverse_iterator是最新的實現, reverse_bidirectional_iterator和另一個隨機反向迭代器是原來的實現, 爲不能模版特化的版本實現的。
實現:
template <class _Iterator>
class reverse_iterator
{
protected:
_Iterator current;
public:
typedef typename iterator_traits<_Iterator>::iterator_category
iterator_category;
typedef typename iterator_traits<_Iterator>::value_type
value_type;
typedef typename iterator_traits<_Iterator>::difference_type
difference_type;
typedef typename iterator_traits<_Iterator>::pointer
pointer;
typedef typename iterator_traits<_Iterator>::reference
reference;
typedef _Iterator iterator_type;
typedef reverse_iterator<_Iterator> _Self;
public:
reverse_iterator() {}
explicit reverse_iterator(iterator_type __x) : current(__x) {}
reverse_iterator(const _Self& __x) : current(__x.current) {}
#ifdef __STL_MEMBER_TEMPLATES
template <class _Iter>
reverse_iterator(const reverse_iterator<_Iter>& __x)
: current(__x.base()) {}
#endif /* __STL_MEMBER_TEMPLATES */
iterator_type base() const { return current; }
reference operator*() const {
_Iterator __tmp = current;
return *--__tmp;
}
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
_Self& operator++() {
--current;
return *this;
}
_Self operator++(int) {
_Self __tmp = *this;
--current;
return __tmp;
}
_Self& operator--() {
++current;
return *this;
}
_Self operator--(int) {
_Self __tmp = *this;
++current;
return __tmp;
}
_Self operator+(difference_type __n) const {
return _Self(current - __n);
}
_Self& operator+=(difference_type __n) {
current -= __n;
return *this;
}
_Self operator-(difference_type __n) const {
return _Self(current + __n);
}
_Self& operator-=(difference_type __n) {
current += __n;
return *this;
}
reference operator[](difference_type __n) const { return *(*this + __n); }
};
template <class _Iterator>
inline bool operator==(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y) {
return __x.base() == __y.base();
}
template <class _Iterator>
inline bool operator<(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y) {
return __y.base() < __x.base();
}
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
template <class _Iterator>
inline bool operator!=(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y) {
return !(__x == __y);
}
template <class _Iterator>
inline bool operator>(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y) {
return __y < __x;
}
template <class _Iterator>
inline bool operator<=(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y) {
return !(__y < __x);
}
template <class _Iterator>
inline bool operator>=(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y) {
return !(__x < __y);
}
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
template <class _Iterator>
inline typename reverse_iterator<_Iterator>::difference_type
operator-(const reverse_iterator<_Iterator>& __x,
const reverse_iterator<_Iterator>& __y) {
return __y.base() - __x.base();
}
template <class _Iterator>
inline reverse_iterator<_Iterator>
operator+(typename reverse_iterator<_Iterator>::difference_type __n,
const reverse_iterator<_Iterator>& __x) {
return reverse_iterator<_Iterator>(__x.base() - __n);
}