STL Source code Analysis-iterator and traits programming method

Source: Internet
Author: User
Tags traits

STL Source code Analysis-iterator and traits programming method

The central idea of STL is to separate the algorithms from the containers, design them independently, and finally glue them together, and the generics of the algorithms and containers are not very difficult, C + + class templates and function templates can achieve the goal, but sticking together is the thing of the iterator.

So the iterator is to glue the algorithm and the container, if the iterator is designed separately, then the iterator must know the internal data of a particular container, exposing too much information, so that each STL container is provided with a dedicated iterator.

The type of the object that the iterator refers to, called the value_type of the iterator. The so-called value_type refers to the type of the object that the iterator refers to. Any class that is intended to be perfectly matched with the STL algorithm should define its own value type inline type.

The difference_type is used to represent the distance between two iterators. Can be used to represent the maximum capacity of a container. Types of iterators

Pointer is this type of pointer

Reference is this type of reference

Iterator_category is the category of iterators:

As for the category of iterators, it can be divided into five categories, inputiterator outputiteratorforwarditerator bidirectional iterator random access iterator is to interpret what sort of an iterator is based on iteratorcategory, and each iterator has different functions and characteristics. So how to judge the V iterator, is based on five classes to judge. Why would you want to judge the type of an iterator? Because for different iterators, the same algorithm is handled differently. There will be a difference in efficiency. Therefore, a iterator_category is defined to determine which class of iterators belong to. The corresponding overloaded function is then called.

At this point, the problem is, in all the containers in the STL, define the above five iterators (Value_type iteator_catefory ...), then for the algorithm, There is a way to use the five iterators in the container (because all the algorithms need only know the iterator to be able to do so, depending on the properties of the iterator), then if all the five iterators are implemented in the container, then the algorithm and the container will be combined soon.

At the same time, in order to make it easier to use these five types of iterators in the class, an "extraction" class is also defined.

Template<class t>

structiterator_traits {

Typedeftyepname i::iterator_category iterator_category;

Typedeftyepname I::value_type Value_type;

Typedef tyepname I::d ifference_type difference_type;

Typedef tyepname I::p ointer pointer;

Typedef tyepname i::reference Reference;

};

At the same time, for the original pointer, the above class is also biased. Why do you want to give the original pointer a special bias? Because the original pointer is not a class type. The native pointer is a pointer to the built-in type.

That is to say, for all STL containers have implemented the above five types, externally, using a class (struct iterator_traits) to extract the container of these five definitions, the algorithm according to the need to invoke the extraction class, from the container to extract what they need. So the iterator is the binder of the algorithm and the container, so to speak. The importance of iterators has been known at this time. So, to get here, be sure to understand that the iterator is an important part of the algorithm and the container adhesion, how to glue the content described above.

In order to conform to the specification, any iterator should provide five inline corresponding type, in order to facilitate traits extraction, or the entire STL architecture, may not be able to match with other STL components, and then write code inevitably overlooked, no one can guarantee not careless, STL provides a iterators Class as follows, if each newly designed iterator inherits from him, it is guaranteed to conform to the specifications required by STL:

Template<class Category,

Class T,

Class distance = ptrdiff_t

Class pointer = t*

Class reference = t&>

Structiterator {

Typedef category_iterator_category;

Typedef T Value_type;

Typedef Distance Difference_type;

Typedef pointer pointer;

Typedef reference Reference;

};

Traits programming techniques are widely used in the SLT implementation, it uses "embedded type" programming skills and compiler template parameter derivation function, enhance the C + + failure to provide the ability of the type of authentication.

Finally, through the contents of the Stl_iterator.h file to view the full code of the iterator source code, you can see that there are five types of iterators, and you define the iterator class, which is the above class. There is a struct iterator_traits called extraction, which is specific to the extraction of the native pointer.

Summary: This is the end of the story, the two pieces of space configuration and iterators,

STL Source code Analysis-iterator and traits programming method

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.