STL的iterator

Posted cccv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL的iterator相关的知识,希望对你有一定的参考价值。

  1 #include <cstddef>
  2 
  3 struct input_iterator_tag {};
  4 struct output_iterator_tag {};
  5 struct forward_iterator_tag : public input_iterator_tag {};
  6 struct bidirectional_iterator_tag : public forward_iterator_tag {};
  7 struct random_access_iterator_tag : public bidirectional_iterator_tag {};
  8 
  9 template<typename Category, typename T, typename Distance = ptrdiff_t,
 10          typename Pointer = T*, typename Reference = T&>
 11 struct iterator
 12 {
 13     typedef Category    iterator_category;
 14     typedef T           value_type;
 15     typedef Distance    difference_type;
 16     typedef Pointer     pointer;
 17     typedef Reference   reference;
 18 };
 19 
 20 // traits
 21 template<typename Iterator>
 22 struct iterator_traits
 23 {
 24     typedef typename Iterator::iterator_category    iterator_category;
 25     typedef typename Iterator::value_type           value_type;
 26     typedef typename Iterator::difference_type      difference_type;
 27     typedef typename Iterator::pointer              pointer;
 28     typedef typename Iterator::reference            reference;
 29 };
 30 
 31 // 针对原生指针(native pointer)而设计的traits偏特化版
 32 template<typename T>
 33 struct iterator_traits<T*>
 34 {
 35     typedef random_access_iterator_tag  iterator_category;
 36     typedef T                           value_type;
 37     typedef ptrdiff_t                   difference_type;
 38     typedef T*                          pointer;
 39     typedef T&                          reference;
 40 };
 41 
 42 // 针对原生之 pointer-to-const 而设计的 traits 偏特化版
 43 template<typename T>
 44 struct iterator_traits<const T*>
 45 {
 46     typedef random_access_iterator_tag  iterator_category;
 47     typedef T                           value_type;
 48     typedef ptrdiff_t                   difference_type;
 49     typedef const T*                    pointer;
 50     typedef const T&                    reference;
 51 };
 52 
 53 // 判定某个迭代器的类型(Category)
 54 template<typename Iterator>
 55 inline typename iterator_traits<Iterator>::iterator_category 
 56 iterator_category(const Iterator&)
 57 {
 58     typedef typename iterator_traits<Iterator>::iterator_category 
 59         category;
 60     return category();
 61 }
 62 
 63 // 判定某个迭代器的 distance type
 64 template<typename Iterator>
 65 inline typename iterator_traits<Iterator>::difference_type*
 66 distance_type(const Iterator&)
 67 {
 68     return static_cast<typename iterator_traits<Iterator>::difference_type*>(0);
 69 }
 70 
 71 // 判定某个迭代器的 value type
 72 template<typename Iterator>
 73 inline typename iterator_traits<Iterator>::value_type*
 74 value_type(const Iterator&)
 75 {
 76     return static_cast<typename iterator_traits<Iterator>::value_type*>(0);
 77 }
 78 
 79 
 80 // 以下是整组的 distance 函数
 81 template<typename InputIterator>
 82 inline typename iterator_traits<InputIterator>::difference_type
 83 __distance(InputIterator first, InputIterator last, input_iterator_tag)
 84 {
 85     typename iterator_traits<InputIterator>::difference_type n = 0;
 86     while(first != last)
 87     {
 88         first++;
 89         n++;
 90     }
 91     return n;
 92 }
 93 
 94 
 95 template<typename RandomAccessIterator>
 96 inline typename iterator_traits<RandomAccessIterator>::difference_type
 97 __distance(RandomAccessIterator first, RandomAccessIterator last,
 98         random_access_iterator_tag)
 99 {
100     return last - first;
101 }
102 
103 template<typename InputIterator>
104 inline typename iterator_traits<InputIterator>::difference_type
105 distance(InputIterator first, InputIterator last)
106 {
107     typedef typename iterator_traits<InputIterator>::iterator_category
108         category;
109     return __distance(first, last, category());
110 }
111 
112 // 以下是整组的 advance 函数
113 template<typename InputIterator, typename Distance>
114 inline void __advance(InputIterator& i, Distance n, input_iterator_tag)
115 {
116     while(n--)
117         i++;
118 }
119 
120 template<typename BidirectionalIterator, typename Distance>
121 inline void __advance(BidirectionalIterator& i, Distance n, 
122         bidirectional_iterator_tag)
123 {
124     if(n >= 0)
125         while(n--) i++;
126     else
127         while(n++) i--;
128 }
129 
130 template<typename RandomAccessIterator, typename Distance>
131 inline void __advance(RandomAccessIterator& i, Distance n,
132         random_access_iterator_tag)
133 {
134     i += n;
135 }
136 
137 template<typename InputIterator, typename Distance>
138 inline void advance(InputIterator& i, Distance n)
139 {
140     __advance(i, n, iterator_category(i));
141 }

 

以上是关于STL的iterator的主要内容,如果未能解决你的问题,请参考以下文章

STL.vector.iterator的序号

对于set iterator和const_iterator的输入,不能重载成员函数(但可以用于其他stl迭代器)

STL的iterator

STL的六大容器之iterator----自定义范式

stl之iterator_category

STL源码剖析——Iterators与Traits编程#5 __type_traits