iterator_traits Struct

반복기가 있어야 모든 중요 한 유형 정의 지정 하는 데 템플릿 도우미 구조체입니다.

template<class Iterator>
    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;
    };
template<class Type>
    struct iterator_traits<Type*> {
        typedef random_access_iterator_tag iterator_category;
        typedef Type value_type;
        typedef ptrdiff_t difference_type;
        typedef difference_type distance_type;
        typedef Type *pointer;
        typedef Type& reference;
    };
template<class Type>
    struct iterator_traits<const Type*> {
        typedef random_access_iterator_tag iterator_category;
        typedef Type value_type;
        typedef ptrdiff_t difference_type;
        typedef difference_type distance_type;
        typedef const Type *pointer;
        typedef const Type& reference;
    };

설명

템플릿을 구조체 멤버 유형 정의

  • iterator_category: 동의어에 대 한 Iterator::iterator_category.

  • value_type: 동의어에 대 한 Iterator::value_type.

  • difference_type: 동의어에 대 한 Iterator::difference_type.

  • distance_type: 동의어에 대 한 Iterator::difference_type.

  • 포인터: 동의어에 대 한 Iterator::pointer.

  • 참조: 동의어에 대 한 Iterator::reference.

부분 특수화 형식의 개체 포인터와 관련 된 중요 한 종류 결정 형식 * 또는 const 형식 *.

부분 특수화 수 없습니다 몇 가지 템플릿 함수를 사용 하는 수도 있습니다이 구현에서:

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 *);

같은 형식의 여러 직접 결정지 않습니다.함수 호출에 대 한 인수로이 함수를 사용 합니다.자신의 유일한 목적은 유용한 템플릿 클래스 매개 변수가 호출된 되는 함수를 제공할 것입니다.

예제

// 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( ) );
}
  

요구 사항

헤더: <iterator>

네임 스페이스: 국방 표준

참고 항목

참조

<iterator>

표준 C++ 라이브러리에서 스레드로부터의 안전성

표준 템플릿 라이브러리