STL iterators source and read -traits

Iterator pattern

Provide a method makes it possible to sequentially access each of the elements of the container, but the container without exposing the interior of the presentation of
STL in that the central idea of the design data of the container and the algorithm is separated from the containers and algorithms designed separately, it is two iterators between the tackiness agent who design generally associated with the container iterators details, it is generally to container designers

Corresponding typed iterator

How "within the meaning of the object to obtain iterator typed"? Parameters can make use of derivation, but the return value can not be derived, if the built-typed statement typedef T value_type, then the pointer to the original can not define its embedded types, this when partial template specialization can be done

And traits partial specialization

Generic definition of partial specialization of thinking is "for template parameters further conditions designed a special version of"
We can use

template <class I>
struct iterator_traits {
    typedef typename I::value_type value_type;
};
// 针对原始指针的特化版本
template <class T>
struct iterator_traits<T*> {
    typedef T value_type;
};
// 针对原始常量指针的特化版本
template <class T>
struct iterator_traits<const T*> {
    typedef T value_type;
};

Commonly typed iterator

The common experience typed iterator respective five kinds: value_type, difference_type, pointer, reference, iterator_catagoly

  • As value_type, extracted type of the iterator referent
  • difference_type denotes the distance between two iterators, as the count STL
  • The changes are allowed reference_type "refer to the content object" is divided into two kinds: constant iterator (The const int * p) and mutable iterator (eg int * p)
  • pointer points was referred to in iterator
  • iterator_category

The mobile operator operation and purposes, is divided into five categories iterator

   input iterator             output iterator
        |                           |
        |                           |
        |___________________________|
                      |
                      |
               forward iterator
                      |
                      |
            bidirectional iterator
                      |
                      |
            random access iterator

It is not above the arrow inheritance relationship, but the relationship between concept and refinement in
order to advance an example, we can at runtime, depending on the type of advance call different functions, but this affect the efficiency, choose a different version of the function can be solved in the compiler, just add tag on the line
type marked with the five other

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 {};

These classes only as a marker, and therefore is null, through inheritance, we can write less overloaded function

Each iterator new design should provide a five-cell type do not, or may not work with STL and other components, so STL provides an iterator class as follows, each iteration newly designed inherit from it, you can ensure compliance the specifications required for STL

template <class Category,
          class T,
          class Distance  = ptrdiff_t,
          class Pointer   = T*,
          class Reference = T&>
struct iterator {
    typedef Category iterator_category;
    typedef T value_type;
    typedef Distance difference_type;
    typedef Pointer pointer;
    typedef Reference reference;
};
template <class Item>
struct ListIter : public iterator<forward_iterator_tag, Item> { /*...*/ };

reference

  • STL source code analysis Chapter 3: iterator concepts and programming techniques traits

Guess you like

Origin www.cnblogs.com/qbits/p/11609603.html