Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::forward_iterator

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      forward_iterator
      (C++20)


      Iterator primitives
      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 I>

          concept forward_iterator=
             std::input_iterator<I>&&
             std::derived_from</*ITER_CONCEPT*/<I>,std::forward_iterator_tag>&&
             std::incrementable<I>&&

             std::sentinel_for<I, I>;
      (since C++20)

      This concept refinesstd::input_iterator by requiring thatI also modelstd::incrementable (thereby making it suitable for multi-pass algorithms), and guaranteeing that two iterators to the same range can be compared against each other.

      Contents

      [edit]Iterator concept determination

      Definition of this concept is specified via an exposition-only alias template/*ITER_CONCEPT*/.

      In order to determine/*ITER_CONCEPT*/<I>, letITER_TRAITS<I> denoteI if the specializationstd::iterator_traits<I> is generated from the primary template, orstd::iterator_traits<I> otherwise:

      • IfITER_TRAITS<I>::iterator_concept is valid and names a type,/*ITER_CONCEPT*/<I> denotes the type.
      • Otherwise, ifITER_TRAITS<I>::iterator_category is valid and names a type,/*ITER_CONCEPT*/<I> denotes the type.
      • Otherwise, ifstd::iterator_traits<I> is generated from the primary template,/*ITER_CONCEPT*/<I> denotesstd::random_access_iterator_tag.
        (That is,std::derived_from</*ITER_CONCEPT*/<I>,std::forward_iterator_tag> is assumed to betrue.)
      • Otherwise,/*ITER_CONCEPT*/<I> does not denote a type and results in a substitution failure.

      [edit]Semantic requirements

      I modelsstd::forward_iterator if, and only ifI models all the concepts it subsumes, and given objectsi andj of typeI:

      • Comparison between iteratorsi andj has a defined result if
      • i andj are iterators to the same underlying sequence, or
      • bothi andj are value-initialized, in which case they compare equal.
      • Pointers and references obtained from a forward iterator into a range remain valid while the range exists.
      • Ifi andj are dereferenceable, they offer themulti-pass guarantee, that is:
      • i== j implies++i==++j, and
      • ((void)[](auto x){++x;}(i),*i) is equivalent to*i.

      [edit]Notes

      Unlike theLegacyForwardIterator requirements, theforward_iterator concept does not require dereference to return a reference.

      [edit]Example

      A minimum forward iterator.

      #include <cstddef>#include <iterator> class SimpleForwardIterator{public:using difference_type=std::ptrdiff_t;using value_type=int;     SimpleForwardIterator();    SimpleForwardIterator(const SimpleForwardIterator&);     SimpleForwardIterator& operator=(const SimpleForwardIterator&); int operator*()const;     SimpleForwardIterator& operator++();     SimpleForwardIterator operator++(int){auto tmp=*this;++*this;return tmp;} bool operator==(const SimpleForwardIterator&)const;}; static_assert(std::forward_iterator<SimpleForwardIterator>);

      [edit]See also

      specifies that a type is an input iterator, that is, its referenced values can be read and it can be both pre- and post-incremented
      (concept)[edit]
      specifies that aforward_iterator is a bidirectional iterator, supporting movement backwards
      (concept)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/forward_iterator&oldid=183123"

      [8]ページ先頭

      ©2009-2025 Movatter.jp