Stl-traits Programming Techniques

Source: Internet
Author: User
Tags traits

Whether it is generic thinking or the actual use of STL, Iterators (iterators) play an important role. The main idea of an iterator is to separate the data container (container) from the algorithm, design each other independently, and finally glue the two together with an iterator.

I. Template specificity and partial localization

Let's say that there is a class template below:

Template<typename t>class  c{...};     

The definition of partial specialization in the generic thinking is: "A special version of a specification that is designed to further restrict any template parameter."

Thus, the partial version of the above style:

class c<t*>{...}; // This version only applies to "T is the native pointer" case        // "T is a native pointer" is a further constraint on "T can be any type "

Two. corresponding type of iterator

The most commonly used iterators correspond to five types: value type,difference type,pointer,reference,iterator category. " The characteristic extractor "traits" is to extract the characteristics of the iterator.

template<class i>struct  iterator_traits{      typedef typename I::iterator_category Iterator_category;      typedef typename I::value_type Value_type;      typedef typename I::reference Reference;         typedef typename I::p ointer pointer;         typedef typename I::d ifference_type difference_type; };

Value_type: Refers to the type of object that an iterator refers to, and any class that is intended to be perfectly matched with the STL algorithm should define its own Value_type inline type.

Difference_type: Used to represent the distance between two iterators.

Reference_type: The iterator refers to whether the object is allowed to change, iterators are divided into two: not allowed to change "the content of the objects referred to", called constant iterators. Allow to change the "content of the object", known as mutable iterators.

Pointer type: The address of the object represented by P

Iterator_category: Depending on the movement characteristics and execution, iterators are divided into five categories:

Input Iterator: This iterator refers to an object that does not allow external changes, read only

Output Iterator: Writable (write only)

Forward Iterator: Allows the "write-on" algorithm to read and write on the interval formed by this type of iterator.

Bidirectional Iterator: Bidirectional movement possible

Random Access Iterator: The first four iterators refer to providing a subset of the pointer arithmetic capability, which covers all pointer arithmetic capabilities, including P+N,P-N,P[N],P1-P2.

Three. Iterator Source code

template<classI>structiterator_traits{typedef typename I::iterator_category iterator_category;          typedef typename I::VALUE_TYPE Value_type;          typedef typename I::reference Reference;          typedef typename I::p ointer pointer;     typedef typename I::d ifference_type difference_type;  }; Template<classI,classT>TypeName Iterator_traits<I>::d ifference_type count (I first,i last,Constt&value) {TypeName Iterator_traits<i&gt::d ifference_type result=0;  for(; first!=last;first++){               if(*first==value) + +N; }           returnN; }               //Five types of iterators     structinput_iterator_tag{}; structoutput_iterator_tag{}; structForward_iterator_tag: Publicinput_iterator_tag{}; structBidirectional_iterator_tag: Publicforward_iterator_tag{}; structRandom_access_iterator_tag: Publicbidirectional_iterator_tag{}; //to avoid exhaustive writing code, the self-developed iterator is best inherited from the following Std::iteratortemplate<classCategory,classTclassdistance=ptrdiff_t,classpointer=t*,classReference=t&>structiterator{typedef Category iterator_category;         typedef T VALUE_TYPE;         typedef Distance DIFFERENCE_TYPE;         typedef Pointer Pointer;     typedef Reference Reference;          }; Template<classIterator>structiterator_traits{typedef typename Iterator::iterator_category iteator_category;         typedef typename ITERATOR::VALUE_TYPE Value_type;         typedef typename Iterator::d ifference_type difference_type;         typedef typename Iterator::p ointer pointer;     typedef typename Iterator::reference Reference;          }; //traits-biased version designed for native pointers (native pointer)template<classT>structIterator_traits<t*>{typedef random_access_iterator_tag iterator_category;         typedef T VALUE_TYPE;         typedef ptrdiff_t DIFFERENCE_TYPE; typedef T*pointer; typedef T&reference;          }; Template<classT>structiterator_traits<ConstT*>{typedef random_access_iterator_tag iterator_category;         typedef T VALUE_TYPE;         typedef ptrdiff_t DIFFERENCE_TYPE; typedefConstt*pointer; typedefConstt&reference; };
...

Stl-traits Programming Techniques

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.