STL source code analysis: iterator

Ready knowledge

What is an iterator?
  • Iterator is a bridge linking containers and algorithms, all algorithms are iterative data through the operation of the vessel

  • Iterator is a smart pointer, the most important is the operator overloading operator*,operator->

  • Implement iterators need to know the specific details of the container, so that each container corresponds to its own unique iterators, but the iterator interface is consistent

Traits programming skills
  • Objective: extracting the data type stored in the container

  • The core technology is the use of templates, you need to call to confirm the function at compile time

  • Nested subordinate Name:

Template <typename T>
 class the Data 
{ 
    typedef the value_type T; 
}; 

Template <typename T>
 class the Type 
{ 
    // typename T :: the value_type is a nested slave name, which can not fall typename
     @ appreciated that template types using a template type 
    the value_type the value_type typename T :: typedef;   
}; 

int main () 
{ 
    the type <the Data < int >> the value_type :: a (); // a () represents a variable of type int 
}
Placeholder parameters to achieve function overloading
  • Placeholder parameter is a function parameter, but this parameter only parameter type, no parameters name

  • Footprint parameter is also part of the list of parameters, can be achieved overloaded

// int parameter is a placeholder 
void Print ( int ) 
{ 
    COUT << " void Print (int) " << endl; 
} 

void Print () 
{ 
    COUT << " void Print () " << endl; 
} 

int main ( ) 
{ 
    Print ( . 1 ); // call Print void (int) 
    Print (); // call Print void () 
}

Source

Common types corresponding iterator
  • Commonly used in the iterator total 5 kinds of container type

Template < class the I>
 struct iterator_traits 
{ 
    typedef typename iterator_category iterator_category the I ::; 
    typedef typename the value_type the value_type the I ::; 
    typedef typename difference_type difference_type the I ::; 
    typedef typename the I :: pointer pointer; 
    typedef typename the I :: Reference Reference 
}; 

/ * 
the value_type: iterator data type referred 
difference_type: indicates the type of the distance between two iterators of 
reference: the type of data content itself 
pointer: corresponding to the reference pointer type 
iterator_category: 
    the Input the iterator: Read only 
    Output iterator: write only 
    Forward iterator: read and write, can only go forward, go forward every time a unit of 
    bidirectional Iterator: read and write, two-way mobile, every time a mobile unit
    Random Access Iterator: readable and writable, two-way mobile, every time a plurality of mobile units 
* /
  • Since the pointer type does not provide native types defined in 5, and therefore processing performed Laid
// Type achieve extraction of native pointer 
Template < class T>
 struct iterator_traits <T *>  
{ 
    ... 
    typedef T * pointer; 
    typedef T & Reference; 
}; 

// implementation type of the extracted pointer const, const attribute removed, as it will operation data container via the iterator 
Template < class T>
 struct iterator_traits < const T *>  
{ 
    ... 
    typedef const T * pointer; 
    typedef const T & Reference; 
};
  • 5 kinds of type definition iterator_category, just type definition, no members
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 { };
iterator complete code
  • Iterator type the relevant code

// five kinds iterator type 
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 {}; 

// define a base class that provides iteration type definition requires compiler to achieve succession, to prevent the omission 
Template < class the Category, class T, class Distance = ptrdiff_t, class the 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 Iterator>
struct iterator_traits {
    typedef typename Iterator::iterator_category iterator_category;
    typedef typename Iterator::value_type
    typedef typename Iterator::difference_type
    typedef typename Iterator::pointer
    typedef typename Iterator::reference
};

// 针对原生指针的特化处理
template <class T>
struct iterator_traits<T*> {
    typedef random_access_iterator_tag        iterator_category;
    typedef T                                value_type;
    typedef ptrdiff_t                        difference_type;
    typedef T*                                pointer;
    typedef T&                                reference;
};

// 针对const指针的特化处理
template <class T>
struct iterator_traits<const T*> {
    typedef random_access_iterator_tag            iterator_category;
    typedef T                                    value_type;
    typedef ptrdiff_t                            difference_type;
    typedef const T*                            pointer;
    typedef const T&                            reference;
};

// 该函数非常方便的提取某个迭代器的类型iterator_category
template <class Iterator>
inline typename iterator_traits<Iterator>::iterator_category iterator_category(const Iterator&) 
{
    typedef typename iterator_traits<Iterator>::iterator_category category;
    return category();
}

//This function is very convenient to extract a type of iterator difference_type 
Template < class the Iterator> 
inline typename iterator_traits <the Iterator> :: difference_type DISTANCE_TYPE * ( const the Iterator & ) 
{ 
    return static_cast <typename iterator_traits <the Iterator> :: difference_type *> ( 0 ) ; 
} 

// this function is very convenient to extract a type of iterator the value_type 
Template < class the iterator> 
inline typename iterator_traits <the iterator> :: * the value_type the value_type ( const the iterator & ) 
{ 
    return static_cast <typename iterator_traits <the iterator> :: * the value_type > ( 0 );
}
  • advance function
// 迭代器前进
template <class InputIterator, class Distance>
inline void advance(InputIterator& i, Distance n) 
{
    __advance(i, n, iterator_category(i));
}

// radom迭代器前进
template <class RandomAccessIterator, class Distance>
inline void __advance(RandomAccessIterator& i, Distance n, random_access_iterator_tag)
{
    i += n;
}

// bidirectional迭代器前进
template <class BidirectionalIterator, class Distance>
inline void __advance(BidirectionalIterator& i, Distance n, bidirectional_iterator_tag) 
{
    if (n >= 0)
        while (n--) ++i;
    else
        while (n++) --i;
}

// input迭代器前进
template <class InputIterator, class Distance>
inline void __advance(InputIterator& i, Distance n, input_iterator_tag) 
{
    while (n--) ++i;
}
  • distance function
// 计算迭代器之间的距离
template <class InputIterator>
inline iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last) 
{
    typedef typename iterator_traits<InputIterator>::iterator_category category;
    return __distance(first, last, category());
}

// 计算random迭代器之间的距离
template <class RandomAccessIterator>
inline iterator_traits<RandomAccessIterator>::difference_type __distance(RandomAccessIterator first, RandomAccessIterator last,
random_access_iterator_tag) 
{
    return last - first;
}

// 计算Input迭代器的距离
template <class InputIterator>
inline iterator_traits<InputIterator>::difference_type __distance(InputIterator first, InputIterator last, input_iterator_tag)
{
    iterator_traits<InputIterator>::difference_type n = 0;
    while (first != last) 
    {
        ++first; 
        ++n;
    }
    return n;
}
  • __type_traits technology
    • Not part of the standard STL, belonging to the SGI STL's proprietary code

    • Iterator_traits same techniques and technologies, but the definition of different types, different occasions action

    • The POD is a type defined __type_traits

    • Part of the code as follows:

struct __true_type { };
struct __false_type { };

template <class type>
struct __type_traits
{
    typedef __true_type this_dummy_member_must_be_first;
    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; // 是否是POD类型
};

__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;
};
__STL_TEMPLATE_NULL struct __type_traits<signed 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;
};
__STL_TEMPLATE_NULL struct __type_traits<unsigned 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;
};
__STL_TEMPLATE_NULL struct __type_traits<short> 
{
    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;
    __true_type is_POD_type typedef; 
}; 

// ... is defined for each base type 

// pointer native type defined 
Template < class T>
 struct __type_traits <T *>  
{ 
    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; 
};

 

Guess you like

Origin www.cnblogs.com/chusiyong/p/11574026.html