一、迭代器
1、迭代器是泛型指针
通过重载*,->,++,--等运算符
(1)普通指针可以指向内存中的一个地址
(2)迭代器可以指向容器中的一个位置
2、STL的每一个容器类模版中,都定义了一组对应的迭代器类。使用迭代器,算法函数可以访问容器中指定位置的元素,而无需关心元素的具体类型。
二、迭代器的类型iterator_category
1、输入迭代器(*p , ++)
可以用来从序列中读取数据
2、输出迭代器(*p = 0 , ++)
允许向序列中写入数据
3、前向迭代器(++,但不支持--)
既是输入迭代器又是输出迭代器,并且可以对序列进行单向的遍历
4、双向迭代器(++,支持--)
与前向迭代器相似,但是在两个方向上都可以对数据遍历
5、随机访问迭代器(++,--, 支持+=5, +3)
也是双向迭代器,但能够在序列中的任意两个位置之间进行跳转
6、下图是不同类型的迭代器能够实现的操作:
三、常用的容器成员
下面列举的成员中,有一些是所有容器共有的,有些是特有的,注意区别:
简单的测试程序跟踪调试如下:
#include
#include
#include
using namespace std; int main(void) { vector
v; v.push_back(1); v.push_back(2); v.push_back(3); vector
::iterator it; for (it = v.begin(); it != v.end(); ++it) { cout << *it << ' '; } cout << endl; vector
::reverse_iterator ri; for (ri = v.rbegin(); ri != v.rend(); ++ri) { cout << *ri << ' '; } cout << endl; list
l; l.push_back(1); l.push_back(2); l.push_back(3); list
::iterator it2; for (it2 = l.begin(); it2 != l.end(); ++it2) { cout << *it2 << ' '; } cout << endl; return 0; }
稍微看一下vector
::iterator 和vector
::reverse_iterator 的
源码:
template < class _Ty,
class _Alloc >
class _Vector_val
: public _CONTAINER_BASE_AUX_ALLOC<_Alloc>
{
// base class for vector to hold allocator _Alval
protected:
_Vector_val(_Alloc _Al = _Alloc())
: _CONTAINER_BASE_AUX_ALLOC<_Alloc>(_Al), _Alval(_Al)
{
// construct allocator from _Al
}
typedef typename _Alloc::template
rebind<_Ty>::other _Alty;
_Alty _Alval; // allocator object for values
};
template < class _Ty,
class _Ax >
class vector
: public _Vector_val<_Ty, _Ax>
{
// varying size array of values
public:
.....
typedef _Vector_val<_Ty, _Ax> _Mybase;
typedef typename _Mybase::_Alty _Alloc; //_Alloc 的定义所在
typedef _Vector_iterator<_Ty, _Alloc> iterator;
typedef _Vector_const_iterator<_Ty, _Alloc> const_iterator;
// friend class _Vector_iterator<_Ty, _Alloc>;
friend class _Vector_const_iterator<_Ty, _Alloc>;
typedef std::reverse_iterator
reverse_iterator;
typedef std::reverse_iterator
const_reverse_iterator; ....... }; template < class _Ty, class _Alloc > class _Vector_iterator : public _Vector_const_iterator<_Ty, _Alloc> { // iterator for mutable vector public: typedef _Vector_iterator<_Ty, _Alloc> _Myt; typedef _Vector_const_iterator<_Ty, _Alloc> _Mybase; ....... }; template < class _Ty, class _Alloc > class _Vector_const_iterator : public _Ranit < _Ty, typename _Alloc::difference_type, typename _Alloc::const_pointer, typename _Alloc::const_reference > { // iterator for nonmutable vector public: ..... typedef typename _Alloc::pointer _Tptr; typedef random_access_iterator_tag iterator_category; typedef _Ty value_type; typedef typename _Alloc::difference_type difference_type; typedef typename _Alloc::const_pointer pointer; typedef typename _Alloc::const_reference reference; typedef const value_type _FARQ *const_pointer; typedef const value_type _FARQ& const_reference; .... _Tptr _Myptr; // offset of element in vector }; template
class allocator : public _Allocator_base<_Ty> { // generic allocator for objects of class _Ty public: ...... typedef _Allocator_base<_Ty> _Mybase; typedef typename _Mybase::value_type value_type; typedef value_type _FARQ *pointer; typedef value_type _FARQ& reference; ... }; // TEMPLATE CLASS _Allocator_base template
struct _Allocator_base { // base class for generic allocators typedef _Ty value_type; }; template
class reverse_iterator : public _Revranit<_RanIt, iterator<...> > { // wrap iterator to run it backwards ........ typedef reverse_iterator<_RanIt> _Myt; typedef _RanIt iterator_type; .............. }; // TEMPLATE CLASS _Revranit template < class _RanIt, class _Base > class _Revranit : public _Base { // wrap iterator to run it backwards .... protected: _RanIt current; // the wrapped iterator };
typedef_Vector_iterator<_Ty,_Alloc>iterator; 可知it