Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::next

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Iterator operations
      (C++11)  
      (C++11)
      ranges::next
      (C++20)
      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>
      Call signature
      template<std::input_or_output_iterator I>
      constexpr I next( I i);
      (1)(since C++20)
      template<std::input_or_output_iterator I>
      constexpr I next( I i,std::iter_difference_t<I> n);
      (2)(since C++20)
      template<std::input_or_output_iterator I,std::sentinel_for<I> S>
      constexpr I next( I i, S bound);
      (3)(since C++20)
      template<std::input_or_output_iterator I,std::sentinel_for<I> S>
      constexpr I next( I i,std::iter_difference_t<I> n, S bound);
      (4)(since C++20)

      Return thenth successor of iteratori.

      The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:

      Contents

      [edit]Parameters

      i - an iterator
      n - number of elements to advance
      bound - sentinel denoting the end of the rangei points to

      [edit]Return value

      1) The successor of iteratori.
      2) Thenth successor of iteratori.
      3) The first iterator equivalent tobound.
      4) Thenth successor of iteratori, or the first iterator equivalent tobound, whichever is first.

      [edit]Complexity

      1) Constant.
      2) Constant ifI modelsstd::random_access_iterator; otherwise linear.
      3) Constant ifI andS models bothstd::random_access_iterator<I> andstd::sized_sentinel_for<S, I>, or ifI andS modelsstd::assignable_from<I&, S>; otherwise linear.
      4) Constant ifI andS models bothstd::random_access_iterator<I> andstd::sized_sentinel_for<S, I>; otherwise linear.

      [edit]Possible implementation

      struct next_fn{template<std::input_or_output_iterator I>constexpr I operator()(I i)const{++i;return i;} template<std::input_or_output_iterator I>constexpr I operator()(I i,std::iter_difference_t<I> n)const{ranges::advance(i, n);return i;} template<std::input_or_output_iterator I,std::sentinel_for<I> S>constexpr I operator()(I i, S bound)const{ranges::advance(i, bound);return i;} template<std::input_or_output_iterator I,std::sentinel_for<I> S>constexpr I operator()(I i,std::iter_difference_t<I> n, S bound)const{ranges::advance(i, n, bound);return i;}}; inlineconstexprauto next= next_fn();

      [edit]Notes

      Although the expression++x.begin() often compiles, it is not guaranteed to do so:x.begin() is an rvalue expression, and there is no requirement that specifies that increment of an rvalue is guaranteed to work. In particular, when iterators are implemented as pointers or itsoperator++ is lvalue-ref-qualified,++x.begin() does not compile, whileranges::next(x.begin()) does.

      [edit]Example

      Run this code
      #include <cassert>#include <iterator> int main(){auto v={3,1,4};{auto n= std::ranges::next(v.begin());assert(*n==1);}{auto n= std::ranges::next(v.begin(),2);assert(*n==4);}{auto n= std::ranges::next(v.begin(), v.end());assert(n== v.end());}{auto n= std::ranges::next(v.begin(),42, v.end());assert(n== v.end());}}

      [edit]See also

      decrement an iterator by a given distance or to a bound
      (algorithm function object)[edit]
      advances an iterator by given distance or to a given bound
      (algorithm function object)[edit]
      (C++11)
      increment an iterator
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/ranges/next&oldid=168443"

      [8]ページ先頭

      ©2009-2025 Movatter.jp