Udostępnij za pośrednictwem


iterator_traits — Struktura

Struktura pomocnika szablonu używana do określania wszystkich definicji typów krytycznych, które powinny mieć iterator.

Składnia

struct iterator_traits {
   typedef typename Iterator::iterator_category iterator_category;
   typedef typename Iterator::value_type value_type;
   typedef typename Iterator::difference_type difference_type;
   typedef difference_type distance_type;
   typedef typename Iterator::pointer pointer;
   typedef typename Iterator::reference reference;
   };

Uwagi

Struktura szablonu definiuje typy składowych

  • iterator_category: synonim dla elementu Iterator::iterator_category.

  • value_type: synonim dla elementu Iterator::value_type.

  • difference_type: synonim dla elementu Iterator::difference_type.

  • distance_type: synonim dla elementu Iterator::difference_type.

  • pointer: synonim dla elementu Iterator::pointer.

  • reference: synonim dla elementu Iterator::reference.

Częściowe specjalizacje określają typy krytyczne skojarzone ze wskaźnikiem obiektu typu Typ* lub Typ* const.

W tej implementacji można również użyć kilku funkcji szablonu, które nie korzystają z częściowej specjalizacji:

template <class Category, class Type, class Diff>
C _Iter_cat(const iterator<Category, Ty, Diff>&);

template <class Ty>
random_access_iterator_tag _Iter_cat(const Ty *);

template <class Category, class Ty, class Diff>
Ty *val_type(const iterator<Category, Ty, Diff>&);

template <class Ty>
Ty *val_type(const Ty *);

template <class Category, class Ty, class Diff>
Diff *_Dist_type(const iterator<Category, Ty, Diff>&);

template <class Ty>
ptrdiff_t *_Dist_type(const Ty *);

które określają kilka tych samych typów pośrednio. Te funkcje są używane jako argumenty wywołania funkcji. Ich jedynym celem jest podanie przydatnego parametru szablonu klasy do wywoływanej funkcji.

Przykład

// iterator_traits.cpp
// compile with: /EHsc
#include <iostream>
#include <iterator>
#include <vector>
#include <list>

using namespace std;

template< class it >
void
function( it i1, it i2 )
{
   iterator_traits<it>::iterator_category cat;
   cout << typeid( cat ).name( ) << endl;
   while ( i1 != i2 )
   {
      iterator_traits<it>::value_type x;
      x = *i1;
      cout << x << " ";
      i1++;
   };
   cout << endl;
};

int main( )
{
   vector<char> vc( 10,'a' );
   list<int> li( 10 );
   function( vc.begin( ), vc.end( ) );
   function( li.begin( ), li.end( ) );
}
/* Output:
struct std::random_access_iterator_tag
a a a a a a a a a a
struct std::bidirectional_iterator_tag
0 0 0 0 0 0 0 0 0 0
*/

Wymagania

Nagłówek:<iterator>

Przestrzeń nazw: std

Zobacz też

<Sterująca>
Bezpieczeństwo wątku w standardowej bibliotece C++
Dokumentacja standardowej biblioteki C++