STL源码 阅读笔记(一) 迭代器头文件stl_iterator_base_types.h

 1 None.gif #ifndef __GLIBCPP_INTERNAL_ITERATOR_BASE_TYPES_H
 2 None.gif #define  __GLIBCPP_INTERNAL_ITERATOR_BASE_TYPES_H
 3 None.gif
 4 None.gif#pragma GCC system_header
 5 None.gif
 6 None.gif namespace  std
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8InBlock.gif
 9InBlock.gif  //定义了5种迭代器类型,但是都不含有任何成员,只用来标记迭代器类型。 
10ExpandedSubBlockStart.gifContractedSubBlock.gif  struct input_iterator_tag dot.gif{};
11ExpandedSubBlockStart.gifContractedSubBlock.gif  struct output_iterator_tag dot.gif{};
12ExpandedSubBlockStart.gifContractedSubBlock.gif  struct forward_iterator_tag : public input_iterator_tag dot.gif{};
13ExpandedSubBlockStart.gifContractedSubBlock.gif  struct bidirectional_iterator_tag : public forward_iterator_tag dot.gif{};
14ExpandedSubBlockStart.gifContractedSubBlock.gif  struct random_access_iterator_tag : public bidirectional_iterator_tag dot.gif{};
15InBlock.gif
16InBlock.gif
17InBlock.gif  //stl提供了一个iteraotr class,供每个新设计的迭代器继承它、。
18InBlock.gif  //在这个迭代器里只是定义了iterator的五种常用型别,不含任何成员,
19InBlock.gif  //所以继承它不会有任何额外负担,而且后三个参数已经有了默认值,故
20InBlock.gif  //只需要提供前两个参数就可以了。
21InBlock.gif  template<typename _Category, typename _Tp, typename _Distance = ptrdiff_t,
22InBlock.gif           typename _Pointer = _Tp*, typename _Reference = _Tp&>
23InBlock.gif    struct iterator
24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
25InBlock.gif      typedef _Category  iterator_category;
26InBlock.gif      typedef _Tp        value_type;
27InBlock.gif      typedef _Distance  difference_type;
28InBlock.gif      typedef _Pointer   pointer;
29InBlock.gif      typedef _Reference reference;
30ExpandedSubBlockEnd.gif    }
;
31InBlock.gif
32InBlock.gif  //针对含有non-trivial constructor,non-trivial destructor,non-trivial copy constructor,
33InBlock.gif  //non-trivial assigment construstor的类型使用的traits版本。定义了5种常用型别
34InBlock.gif  template<typename _Iterator>
35ExpandedSubBlockStart.gifContractedSubBlock.gif    struct iterator_traits dot.gif{
36InBlock.gif      typedef typename _Iterator::iterator_category iterator_category;
37InBlock.gif      typedef typename _Iterator::value_type        value_type;
38InBlock.gif      typedef typename _Iterator::difference_type   difference_type;
39InBlock.gif      typedef typename _Iterator::pointer           pointer;
40InBlock.gif      typedef typename _Iterator::reference         reference;
41ExpandedSubBlockEnd.gif    }
;
42InBlock.gif
43InBlock.gif  //针对原生指针的traits偏特化版本
44InBlock.gif  template<typename _Tp>
45ExpandedSubBlockStart.gifContractedSubBlock.gif    struct iterator_traits<_Tp*> dot.gif{
46InBlock.gif      typedef random_access_iterator_tag iterator_category;
47InBlock.gif      typedef _Tp                         value_type;
48InBlock.gif      typedef ptrdiff_t                   difference_type;
49InBlock.gif      typedef _Tp*                        pointer;
50InBlock.gif      typedef _Tp&                        reference;
51ExpandedSubBlockEnd.gif    }
;
52InBlock.gif  //针对原生指针之pointer-to-const版本的traits偏特化版本
53InBlock.gif  template<typename _Tp>
54ExpandedSubBlockStart.gifContractedSubBlock.gif    struct iterator_traits<const _Tp*> dot.gif{
55InBlock.gif      typedef random_access_iterator_tag iterator_category;
56InBlock.gif      typedef _Tp                         value_type;
57InBlock.gif      typedef ptrdiff_t                   difference_type;
58InBlock.gif      typedef const _Tp*                  pointer;
59InBlock.gif      typedef const _Tp&                  reference;
60ExpandedSubBlockEnd.gif    }
;
61InBlock.gif
62InBlock.gif
63InBlock.gif  //返回迭代器类型 
64InBlock.gif  template<typename _Iter>
65InBlock.gif    inline typename iterator_traits<_Iter>::iterator_category
66InBlock.gif    __iterator_category(const _Iter&)
67ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gifreturn typename iterator_traits<_Iter>::iterator_category(); }
68InBlock.gif
69ExpandedBlockEnd.gif}
  //  namespace std
70 None.gif
71 None.gif #endif  /* __GLIBCPP_INTERNAL_ITERATOR_BASE_TYPES_H */
72 None.gif

转载于:https://www.cnblogs.com/skipper-haobaobao/archive/2007/03/29/692588.html

你可能感兴趣的:(c/c++)