Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::iterator_traits

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      (deprecated in C++17)
      iterator_traits


      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Range access
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++17)(C++20)
      (C++17)
      (C++17)
       
      Defined in header<iterator>
      template<class Iter>
      struct iterator_traits;
      template<class T>
      struct iterator_traits<T*>;
      template<class T>
      struct iterator_traits<const T*>;
      (removed in C++20)

      std::iterator_traits is the type trait class that provides uniform interface to the properties ofLegacyIterator types. This makes it possible to implement algorithms only in terms of iterators.

      The template can be specialized for user-defined iterators so that the information about the iterator can be retrieved even if the type does not provide the usual typedefs.

      User specializations may define the nested typeiterator_concept to one ofiterator category tags, to indicate conformance to the iterator concepts.

      (since C++20)

      Contents

      [edit]Template parameters

      Iter - the iterator type to retrieve properties for

      [edit]Member types

      Nested type Definition
      difference_typeIter::difference_type
      value_typeIter::value_type
      pointerIter::pointer
      referenceIter::reference
      iterator_categoryIter::iterator_category


      IfIter does not have any of the five nested types above, then this template has no members by any of those names (std::iterator_traits is SFINAE-friendly).

      (since C++17)
      (until C++20)

      IfIter does not havepointer, but has all four remaining nested types, then these four nested types are declared as follows:

      Nested type Definition
      difference_typeIter::difference_type
      value_typeIter::value_type
      pointervoid
      referenceIter::reference
      iterator_categoryIter::iterator_category


      Otherwise, ifIter satisfies the exposition-only concept__LegacyInputIterator, the nested types are declared as follows:

      Nested type Definition
      difference_typestd::incrementable_traits<Iter>::difference_type
      value_typestd::indirectly_readable_traits<Iter>::value_type
      pointer
      • Iter::pointer if valid.
      • Otherwisedecltype(std::declval<Iter&>().operator->()) if valid.
      • Otherwisevoid.
      reference
      iterator_category


      Otherwise, ifIter satisfies the exposition-only concept__LegacyIterator, the nested types are declared as follows:

      Nested type Definition
      difference_type
      value_typevoid
      pointervoid
      referencevoid
      iterator_categorystd::output_iterator_tag

      Otherwise, this template has no members by any of those names (std::iterator_traits is SFINAE-friendly).

      (since C++20)

      [edit]Specializations

      This type trait may be specialized for user-provided types that may be used as iterators. The standard library provides partial specializations for pointer typesT*, which makes it possible to use all iterator-based algorithms with raw pointers.

      The standard library also provides partial specializations for some standard iterator adaptors.

      (since C++20)

      [edit]T* specialization nested types

      Only specialized ifstd::is_object_v<T> istrue.

      (since C++20)


      Nested type Definition
      difference_typestd::ptrdiff_t
      value_typeT(until C++20)std::remove_cv_t<T>(since C++20)
      pointerT*
      referenceT&
      iterator_categorystd::random_access_iterator_tag
      iterator_concept(since C++20)std::contiguous_iterator_tag


      const T* specialization nested types

      Nested type Definition
      difference_typestd::ptrdiff_t
      value_typeT
      pointerconst T*
      referenceconst T&
      iterator_categorystd::random_access_iterator_tag
      (until C++20)

      [edit]Specializations for library types

      provides uniform interface to the properties of thestd::common_iterator type
      (class template specialization)[edit]
      provides uniform interface to the properties of thestd::counted_iterator type
      (class template specialization)[edit]

      [edit]Example

      Shows a general-purposestd::reverse() implementation for bidirectional iterators.

      Run this code
      #include <iostream>#include <iterator>#include <list>#include <vector> template<class BidirIt>void my_reverse(BidirIt first, BidirIt last){typename std::iterator_traits<BidirIt>::difference_type n=std::distance(first, last);for(--n; n>0; n-=2){typename std::iterator_traits<BidirIt>::value_type tmp=*first;*first++=*--last;*last= tmp;}} int main(){std::vector<int> v{1,2,3,4,5};    my_reverse(v.begin(), v.end());for(int n: v)std::cout<< n<<' ';std::cout<<'\n'; std::list<int> l{1,2,3,4,5};    my_reverse(l.begin(), l.end());for(int n: l)std::cout<< n<<' ';std::cout<<'\n'; int a[]{1,2,3,4,5};    my_reverse(a, a+std::size(a));for(int n: a)std::cout<< n<<' ';std::cout<<'\n'; //  std::istreambuf_iterator<char> i1(std::cin), i2;//  my_reverse(i1, i2); // compilation error: i1, i2 are input iterators}

      Output:

      5 4 3 2 15 4 3 2 15 4 3 2 1

      [edit]See also

      (deprecated in C++17)
      base class to ease the definition of required types for simple iterators
      (class template)[edit]
      empty class types used to indicate iterator categories
      (class)[edit]
      computes the associated types of an iterator
      (alias template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/iterator_traits&oldid=173508"

      [8]ページ先頭

      ©2009-2025 Movatter.jp