1、基本概念
迭代器,是一种抽象的设计模式,主要是提供一种方法,使之能够依序巡防某个聚合物所含的各个元素,而又无需暴露该聚合物的内部表述方式。STL想要将容器和算法分开,达到泛化的目的,就需要迭代器作为其中的胶着剂。
2、智能指针
迭代器是一种行为类似指针的对象,其最常见也最重要的行为是内容提领和成员访问,因为需要对operation*和operation->进行重载。但是,迭代器在实现这些重载时,不可避免会暴露容器本身的实现细节,因而,容器所需的迭代器直接有容器本身去实现,从而达到封装实现细节的目的。
3、迭代器相应型别
相应型别,即指对象的类型,由于c++只支持sizeof(),不支持typeof(),想要解决这一问题,可利用函数模板的参数推导机制:
template<typename I,typename T>
void func_impl(I iter, T t)
{
T temp = 0;
cout<<temp<<endl;
}
template<typename I>
inline void func(I iter)
{
func_impl(iter, *iter);
}
函数模板的参数推倒机制虽然可以获取对象类型,但却不能满足所有情况,若是要推导返回值的数据类型就不可用了。这里,就需要直接在迭代器内部声明出来。同时,在用做返回值时需要在前面加上关键字typename,用于告诉编译器这是一个型别,如此才能通过编译。示例如下:
template<typename T>
struct MyIter
{
typedef T value_type;
T* ptr;
MyIter(T* p=0):ptr(p){}
T& operator*() const{return *ptr;}
};
template<typename I>
typename I::value_type func(I iter)
{
return *iter;
}
目前看起来很不错,但却有一个陷阱:并不是所有的迭代器都是class type。如原生指针,其并没有value_type。为了解决这一问题,STL采用的方法是在获取迭代器的value_type的基础上包装一层,使用特性萃取机来萃取,若迭代器有value_type,则直接访问;若没有,则可以通过定义偏特化来进行获取。偏特化,是指对模板参数添加更进一步的限制条件,这里就是对原生指针进行偏特化,从而实现不管什么类型的迭代器,都可获取到正确的value_type,达到迭代器泛化的目的。实现源码如下:
template <class _Iterator>
struct iterator_traits {
typedef typename _Iterator::iterator_category iterator_category;
typedef typename _Iterator::value_type value_type;
typedef typename _Iterator::difference_type difference_type;
typedef typename _Iterator::pointer pointer;
typedef typename _Iterator::reference reference;
};
template <class _Tp>
struct iterator_traits<_Tp*> {
typedef random_access_iterator_tag iterator_category;
typedef _Tp value_type;
typedef ptrdiff_t difference_type;
typedef _Tp* pointer;
typedef _Tp& reference;
};
目前迭代器有以下五种常用相应型别:
1. value_type,是指迭代器所指对象的类型;
2. difference_type,是指两个迭代器之间的距离,也可使用容器头尾的距离来表示容器最大容量;
3. pointer,是指迭代器所指对象类型的指针;
4. reference,是指迭代器所指对象类型的引用;
5. iterator_category,是指迭代器本身的类型。
为了到达统一,每个迭代器都必须实现这些相应型别,从而才能融入整个STL体系当中。
4、迭代器类型
不同的迭代器,具有不同的操作功能,目前被分为以下五类:
1. input iterator,这种迭代器所指对象只能读,不可更改;
2. output iterator,这种迭代器所指对象只能写;
3. forward iterator,这种迭代器可提供对指定区间内的对象单向顺序操作;
4. bidirectional iterator, 这种迭代器可提供对指定区间内的对象双向顺序操作;
5. random access iterator,这种迭代器可提供所有指针所具有的算术操作。
五种迭代器之间并没有继承的关系,是所谓概念与强化的关系。任何一种迭代器,其特性可能同时满足多种情况,但其类型永远是定义为特性最强的那个。如int*,同时满足random access iterator、bidirectional iterator、forward iterator和input iterator,这其中random access iterator是特性最强的,因而int*的类型就属于random access iterator。
在设计算法时,尽量对某种迭代器提供一个明确的定义,并针对更强化的某种迭代器提供另一种定义,以达到在不同情况下提供最大的效率。因而在设计算法接口时,需要设定参数迭代器的类型为所能接受的最低的类型。
虽然迭代器之间没有继承关系,但是STL在定义迭代器类型值时却是有继承关系的,这样做不仅可以促成重载机制的成功运作,也可消除单纯传递调用的函数,由编译器来进行参数匹配。源码如下:
struct input_iterator_tag {};
struct output_iterator_tag {};
struct forward_iterator_tag : public input_iterator_tag {};
struct bidirectional_iterator_tag : public forward_iterator_tag {};
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
template <class _InputIterator>
inline typename iterator_traits<_InputIterator>::difference_type
__distance(_InputIterator __first, _InputIterator __last, input_iterator_tag)
{
typename iterator_traits<_InputIterator>::difference_type __n = 0;
while (__first != __last) {
++__first; ++__n;
}
return __n;
}
template <class _RandomAccessIterator>
inline typename iterator_traits<_RandomAccessIterator>::difference_type
__distance(_RandomAccessIterator __first, _RandomAccessIterator __last,
random_access_iterator_tag) {
__STL_REQUIRES(_RandomAccessIterator, _RandomAccessIterator);
return __last - __first;
}
template <class _InputIterator>
inline typename iterator_traits<_InputIterator>::difference_type
distance(_InputIterator __first, _InputIterator __last) {
typedef typename iterator_traits<_InputIterator>::iterator_category
_Category;
__STL_REQUIRES(_InputIterator, _InputIterator);
return __distance(__first, __last, _Category());
}
通过上面的源码我们可以看到,distance能够支持所有类型的迭代器,但实际却只实现了input_iterator_tag和random_access_iterator_tag这两种版本的,若参数为forward_iterator_tag或bidirectional_iterator_tag时,虽然没有明确的实现接口,但编译器根据继承关系可推导出需要使用input_iterator_tag的版本。
5、容器迭代器
设计适当的相应型别,是迭代器的责任,而设计适当的迭代器,则是容器的责任。只有容器本身,才知道该设计出适合本身操作的迭代器。这里,我们看下各个容器是如何设计其对应的迭代器的。
5.1、vector
vector迭代器就是原生指针,本身是没有迭代器所需要的相应型别的,这里就是通过前面所介绍的萃取器的偏特化实现的。原生指针天生就支持随机存取,因而vector的迭代器属于随机迭代器。源码如下:
typedef _Tp value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
5.2、list
list不再像vector那样以原生指针作为迭代器,因为其节点并不保证在存储空间中连续存在。list在结构上属于双向链表,因而其迭代器必须满足向前和向后访问的能力,因而属于双向迭代器。源码如下:
struct _List_iterator_base {
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef bidirectional_iterator_tag iterator_category;
_List_node_base* _M_node;
_List_iterator_base(_List_node_base* __x) : _M_node(__x) {}
_List_iterator_base() {}
void _M_incr() { _M_node = _M_node->_M_next; }
void _M_decr() { _M_node = _M_node->_M_prev; }
bool operator==(const _List_iterator_base& __x) const {
return _M_node == __x._M_node;
}
bool operator!=(const _List_iterator_base& __x) const {
return _M_node != __x._M_node;
}
};
template<class _Tp, class _Ref, class _Ptr>
struct _List_iterator : public _List_iterator_base {
typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator;
typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
typedef _List_iterator<_Tp,_Ref,_Ptr> _Self;
typedef _Tp value_type;
typedef _Ptr pointer;
typedef _Ref reference;
typedef _List_node<_Tp> _Node;
_List_iterator(_Node* __x) : _List_iterator_base(__x) {}
_List_iterator() {}
_List_iterator(const iterator& __x) : _List_iterator_base(__x._M_node) {}
reference operator*() const { return ((_Node*) _M_node)->_M_data; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
_Self& operator++() {
this->_M_incr();
return *this;
}
_Self operator++(int) {
_Self __tmp = *this;
this->_M_incr();
return __tmp;
}
_Self& operator--() {
this->_M_decr();
return *this;
}
_Self operator--(int) {
_Self __tmp = *this;
this->_M_decr();
return __tmp;
}
};
5.3、deque
deque是分段连续空间,因而其迭代器首先能够知道分段缓冲区在哪里,其次在能够知道自己处于某个缓冲区的边缘并正确跳转到下一个缓冲区。deque的迭代器使用cur、first、last、node四个指针,其中node指向当前缓冲区在管控中心里的位置,cur指向缓冲器的当前位置,first指向缓冲区的第一个节点,last指向缓冲区的最后一个节点的下一个位置,遵循STL前开后闭的标准。dequez迭代器支持基本的算法运算,属于随机迭代器,实现源码如下:
inline size_t __deque_buf_size(size_t __size) {
return __size < 512 ? size_t(512 / __size) : size_t(1);
}
template <class _Tp, class _Ref, class _Ptr>
struct _Deque_iterator {
typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
static size_t _S_buffer_size() { return __deque_buf_size(sizeof(_Tp)); }
typedef random_access_iterator_tag iterator_category;
typedef _Tp value_type;
typedef _Ptr pointer;
typedef _Ref reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _Tp** _Map_pointer;
typedef _Deque_iterator _Self;
_Tp* _M_cur;
_Tp* _M_first;
_Tp* _M_last;
_Map_pointer _M_node;
_Deque_iterator(_Tp* __x, _Map_pointer __y)
: _M_cur(__x), _M_first(*__y),
_M_last(*__y + _S_buffer_size()), _M_node(__y) {}
_Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
_Deque_iterator(const iterator& __x)
: _M_cur(__x._M_cur), _M_first(__x._M_first),
_M_last(__x._M_last), _M_node(__x._M_node) {}
reference operator*() const { return *_M_cur; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return _M_cur; }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
difference_type operator-(const _Self& __x) const {
return difference_type(_S_buffer_size()) * (_M_node - __x._M_node - 1) +
(_M_cur - _M_first) + (__x._M_last - __x._M_cur);
}
_Self& operator++() {
++_M_cur;
if (_M_cur == _M_last) {
_M_set_node(_M_node + 1);
_M_cur = _M_first;
}
return *this;
}
_Self operator++(int) {
_Self __tmp = *this;
++*this;
return __tmp;
}
_Self& operator--() {
if (_M_cur == _M_first) {
_M_set_node(_M_node - 1);
_M_cur = _M_last;
}
--_M_cur;
return *this;
}
_Self operator--(int) {
_Self __tmp = *this;
--*this;
return __tmp;
}
_Self& operator+=(difference_type __n)
{
difference_type __offset = __n + (_M_cur - _M_first);
if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
_M_cur += __n;
else {
difference_type __node_offset =
__offset > 0 ? __offset / difference_type(_S_buffer_size())
: -difference_type((-__offset - 1) / _S_buffer_size()) - 1;
_M_set_node(_M_node + __node_offset);
_M_cur = _M_first +
(__offset - __node_offset * difference_type(_S_buffer_size()));
}
return *this;
}
_Self operator+(difference_type __n) const
{
_Self __tmp = *this;
return __tmp += __n;
}
_Self& operator-=(difference_type __n) { return *this += -__n; }
_Self operator-(difference_type __n) const {
_Self __tmp = *this;
return __tmp -= __n;
}
reference operator[](difference_type __n) const { return *(*this + __n); }
bool operator==(const _Self& __x) const { return _M_cur == __x._M_cur; }
bool operator!=(const _Self& __x) const { return !(*this == __x); }
bool operator<(const _Self& __x) const {
return (_M_node == __x._M_node) ?
(_M_cur < __x._M_cur) : (_M_node < __x._M_node);
}
bool operator>(const _Self& __x) const { return __x < *this; }
bool operator<=(const _Self& __x) const { return !(__x < *this); }
bool operator>=(const _Self& __x) const { return !(*this < __x); }
void _M_set_node(_Map_pointer __new_node) {
_M_node = __new_node;
_M_first = *__new_node;
_M_last = _M_first + difference_type(_S_buffer_size());
}
};
5.4、slist
slist属于单向列表,因而其迭代器只需提供单向访问功能即可,属于单向迭代器,实现源码如下:
struct _Slist_iterator_base
{
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef forward_iterator_tag iterator_category;
_Slist_node_base* _M_node;
_Slist_iterator_base(_Slist_node_base* __x) : _M_node(__x) {}
void _M_incr() { _M_node = _M_node->_M_next; }
bool operator==(const _Slist_iterator_base& __x) const {
return _M_node == __x._M_node;
}
bool operator!=(const _Slist_iterator_base& __x) const {
return _M_node != __x._M_node;
}
};
template <class _Tp, class _Ref, class _Ptr>
struct _Slist_iterator : public _Slist_iterator_base
{
typedef _Slist_iterator<_Tp, _Tp&, _Tp*> iterator;
typedef _Slist_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
typedef _Slist_iterator<_Tp, _Ref, _Ptr> _Self;
typedef _Tp value_type;
typedef _Ptr pointer;
typedef _Ref reference;
typedef _Slist_node<_Tp> _Node;
_Slist_iterator(_Node* __x) : _Slist_iterator_base(__x) {}
_Slist_iterator() : _Slist_iterator_base(0) {}
_Slist_iterator(const iterator& __x) : _Slist_iterator_base(__x._M_node) {}
reference operator*() const { return ((_Node*) _M_node)->_M_data; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
_Self& operator++()
{
_M_incr();
return *this;
}
_Self operator++(int)
{
_Self __tmp = *this;
_M_incr();
return __tmp;
}
};
5.5、RB-tree
RB-tree的迭代器属于双向迭代器,不具备随机定位的能力,其操作方式与list的迭代器比较相似。实现源码如下:
struct _Rb_tree_base_iterator
{
typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
typedef bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
_Base_ptr _M_node;
void _M_increment()
{
if (_M_node->_M_right != 0) {
_M_node = _M_node->_M_right;
while (_M_node->_M_left != 0)
_M_node = _M_node->_M_left;
}
else {
_Base_ptr __y = _M_node->_M_parent;
while (_M_node == __y->_M_right) {
_M_node = __y;
__y = __y->_M_parent;
}
if (_M_node->_M_right != __y)
_M_node = __y;
}
}
void _M_decrement()
{
if (_M_node->_M_color == _S_rb_tree_red &&
_M_node->_M_parent->_M_parent == _M_node)
_M_node = _M_node->_M_right;
else if (_M_node->_M_left != 0) {
_Base_ptr __y = _M_node->_M_left;
while (__y->_M_right != 0)
__y = __y->_M_right;
_M_node = __y;
}
else {
_Base_ptr __y = _M_node->_M_parent;
while (_M_node == __y->_M_left) {
_M_node = __y;
__y = __y->_M_parent;
}
_M_node = __y;
}
}
};
template <class _Value, class _Ref, class _Ptr>
struct _Rb_tree_iterator : public _Rb_tree_base_iterator
{
typedef _Value value_type;
typedef _Ref reference;
typedef _Ptr pointer;
typedef _Rb_tree_iterator<_Value, _Value&, _Value*>
iterator;
typedef _Rb_tree_iterator<_Value, const _Value&, const _Value*>
const_iterator;
typedef _Rb_tree_iterator<_Value, _Ref, _Ptr>
_Self;
typedef _Rb_tree_node<_Value>* _Link_type;
_Rb_tree_iterator() {}
_Rb_tree_iterator(_Link_type __x) { _M_node = __x; }
_Rb_tree_iterator(const iterator& __it) { _M_node = __it._M_node; }
reference operator*() const { return _Link_type(_M_node)->_M_value_field; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
_Self& operator++() { _M_increment(); return *this; }
_Self operator++(int) {
_Self __tmp = *this;
_M_increment();
return __tmp;
}
_Self& operator--() { _M_decrement(); return *this; }
_Self operator--(int) {
_Self __tmp = *this;
_M_decrement();
return __tmp;
}
};
5.6、hashtable
hashtable迭代器属于单向迭代器,当迭代器处于list的尾端,就跳转至下一个bucket身上,实现源码如下:
template <class _Val, class _Key, class _HashFcn,
class _ExtractKey, class _EqualKey, class _Alloc>
struct _Hashtable_iterator {
typedef hashtable<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>
_Hashtable;
typedef _Hashtable_iterator<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>
iterator;
typedef _Hashtable_const_iterator<_Val, _Key, _HashFcn,
_ExtractKey, _EqualKey, _Alloc>
const_iterator;
typedef _Hashtable_node<_Val> _Node;
typedef forward_iterator_tag iterator_category;
typedef _Val value_type;
typedef ptrdiff_t difference_type;
typedef size_t size_type;
typedef _Val& reference;
typedef _Val* pointer;
_Node* _M_cur;
_Hashtable* _M_ht;
_Hashtable_iterator(_Node* __n, _Hashtable* __tab)
: _M_cur(__n), _M_ht(__tab) {}
_Hashtable_iterator() {}
reference operator*() const { return _M_cur->_M_val; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
iterator& operator++();
iterator operator++(int);
bool operator==(const iterator& __it) const
{ return _M_cur == __it._M_cur; }
bool operator!=(const iterator& __it) const
{ return _M_cur != __it._M_cur; }
};
6、SGI类型萃取器__type_traits
c++本身不支持获取对象类型,STL为了解决这一问题,只对迭代器进行了规范,SGI将其进行了扩展。SGI的__type_traits,提供一种机制,允许针对不同的对象类型,在编译时期就完成函数派送决定。SGI在定义__type_traits时,采用最保守的值,然后通过定义特化版本对具体的对象类型进行实现,源码如下:
struct __true_type {
};
struct __false_type {
};
template <class _Tp>
struct __type_traits {
typedef __true_type this_dummy_member_must_be_first;
/* Do not remove this member. It informs a compiler which
automatically specializes __type_traits that this
__type_traits template is special. It just makes sure that
things work if an implementation is using a template
called __type_traits for something unrelated. */
/* The following restrictions should be observed for the sake of
compilers which automatically produce type specific specializations
of this class:
- You may reorder the members below if you wish
- You may remove any of the members below if you wish
- You must not rename members without making the corresponding
name change in the compiler
- Members you add will be treated like regular members unless
you add the appropriate support in the compiler. */
typedef __false_type has_trivial_default_constructor;
typedef __false_type has_trivial_copy_constructor;
typedef __false_type has_trivial_assignment_operator;
typedef __false_type has_trivial_destructor;
typedef __false_type is_POD_type;
};
__STL_TEMPLATE_NULL struct __type_traits<char> {
typedef __true_type has_trivial_default_constructor;
typedef __true_type has_trivial_copy_constructor;
typedef __true_type has_trivial_assignment_operator;
typedef __true_type has_trivial_destructor;
typedef __true_type is_POD_type;
};
7、综合结果
迭代器的特性与容器是息息相关的,因而由容器提供自己的迭代器,从而使算法能够独立于容器和迭代器之外自行发展,只要设计时以迭代器对外接口就行。
traits编程技法利用内嵌型别的编程技巧和编译器的模板参数推导功能,增强c++未能提供的关于型别认证方面的能力,弥补c++不为强型别的语言的遗憾。