Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::subrange<I,S,K>::advance

      From cppreference.com
      <cpp‎ |ranges‎ |subrange
       
       
      Ranges library
      Range adaptors
       
       
      constexpr subrange& advance(std::iter_difference_t<I> n);
      (since C++20)

      Increments or decrementsbegin_ :

      Equivalent to:ranges::advance(begin_ , n);
      ifconstexpr(StoreSize )
          size_ += to-unsigned-like (-n);
      return*this;
      .
      • Otherwise, incrementsbegin_ byn elements, or untilend_ is reached.
      Equivalent to:auto d= n-ranges::advance(begin_ , n, end_ );
      ifconstexpr(StoreSize )
          size_ -= to-unsigned-like (d);
      return*this;
      .


      According to the preconditions ofranges::advance, ifn<0 istrue andbegin_ cannot be decremented by-n elements, the behavior is undefined.

      Contents

      [edit]Parameters

      n - number of maximal increments of the iterator

      [edit]Return value

      *this

      [edit]Example

      Run this code
      #include <algorithm>#include <array>#include <iostream>#include <iterator>#include <ranges> void print(auto name,autoconst sub){std::cout<< name<<".size() == "<< sub.size()<<"; { ";    std::ranges::for_each(sub,[](int x){std::cout<< x<<' ';});std::cout<<"}\n";}; int main(){std::array arr{1,2,3,4,5,6,7};    std::ranges::subrange sub{std::next(arr.begin()),std::prev(arr.end())};    print("1) sub", sub);    print("2) sub", sub.advance(3));    print("3) sub", sub.advance(-2));}

      Output:

      1) sub.size() == 5; { 2 3 4 5 6 }2) sub.size() == 2; { 5 6 }3) sub.size() == 4; { 3 4 5 6 }

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3433C++20the behavior was undefined ifn<0made well-defined ifbegin_ can be decremented

      [edit]See also

      obtains a copy of thesubrange with its iterator advanced by a given distance
      (public member function)[edit]
      obtains a copy of thesubrange with its iterator decremented by a given distance
      (public member function)[edit]
      advances an iterator by given distance
      (function template)[edit]
      advances an iterator by given distance or to a given bound
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/subrange/advance&oldid=179877"

      [8]ページ先頭

      ©2009-2025 Movatter.jp