Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the typical use cases of an iterator_trait

I am new to C++ so please bear with me. I am trying to understand STL iterator_traits. In the book "The C++ Standard Library" the structure iterator_traits is defined as follows:

template <class T> struct iterator_traits {   typedef typename T::value_type value_type;   typedef typename T::difference_type difference_type;   typedef typename T::iterator_category iterator_category;   typedef typename T::pointer pointer;   typedef typename T::reference reference; }; 

So it seems to me that it is re-exposing the subtypes that T already exposes. Moving ahead further, the book gives an example of how to use it, which is something like the following

template <class MyIterator> void do_something(MyIterator start, MyIterator end) {     typedef typename iterator_traits<MyIterator>::value_type value_type;      value_type v = *start;     ..... } 

My question is why do I need this iterator_traits structure here, if the idea was to obtain the value_type, couldn't I have obtained it from MyIterator directly ? My confusion seems to arise from my (surely incorrect) understanding that the information of the subtypes have to be sourced from the template <class T> used to instantiate the iterator_trait. So if you could explain, and preferably with an example why and where would I need iterator_traits that would be very helpful.

like image 805
san Avatar asked Jul 19 '11 03:07

san


People also ask

What does iterator_ traits?

iterator_traits are used within algorithms to create local variables of either the type pointed to by the iterator or of the iterator's distance type. The traits also improve the efficiency of algorithms by making use of knowledge about basic iterator categories provided by the iterator_category member.

What is Difference_type C++?

difference_type - a type that can be used to identify distance between iterators. value_type - the type of the values that can be obtained by dereferencing the iterator. This type is void for output iterators. pointer - defines a pointer to the type iterated over ( value_type )

Is std :: iterator deprecated?

`std::iterator` Is Deprecated iterator_category - the type of the iterator. value_type - type stored in the iterator. difference_type - the type that is the result of subtracting two iterators. pointer - pointer type of the stored type.


1 Answers

Pointers into an array can be used as random access iterators.

There needs to be some consistent way to get these types both for pointers (which obviously can't have the types declared as nested types, since only classes can have nested types) and for class-type iterators. The traits class template provides this consistent way.

The iterator_traits class template is specialized for pointers like so:

template <typename T> struct iterator_traits<T*> {     typedef std::random_access_iterator_tag iterator_category;     typedef T                               value_type;     typedef T*                              pointer;     typedef T&                              reference;     typedef std::ptrdiff_t                  difference_type; }; 
like image 70
James McNellis Avatar answered Oct 08 '22 19:10

James McNellis