Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::shift_left,std::ranges::shift_right

      From cppreference.com
      <cpp‎ |algorithm‎ |ranges
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Constrained algorithms
      All names in this menu belong to namespacestd::ranges
      Non-modifying sequence operations
      Modifying sequence operations
      Partitioning operations
      Sorting operations
      Binary search operations (on sorted ranges)
             
             
      Set operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      Permutation operations
      Fold operations
      Operations on uninitialized storage
      Return types
       
      Defined in header<algorithm>
      Call signature
      template<std::permutable I,std::sentinel_for<I> S>

      constexprranges::subrange<I>

          shift_left( I first, S last,std::iter_difference_t<I> n);
      (1)(since C++23)
      (2)(since C++23)
      template<std::permutable I,std::sentinel_for<I> S>

      constexprranges::subrange<I>

          shift_right( I first, S last,std::iter_difference_t<I> n);
      (3)(since C++23)
      (4)(since C++23)

      Shifts the elements in the range[firstlast) orr byn positions. The behavior is undefined if[firstlast) is not a valid range.

      1) Shifts the elements towards the beginning of the range:
      • Ifn==0|| n>= last- first, there are no effects.
      • Ifn<0, the behavior is undefined.
      • Otherwise, for every integeri in[0last- first- n), moves the element originally at positionfirst+ n+ i to positionfirst+ i. The moves are performed in increasing order ofi starting from0.
      3) Shifts the elements towards the end of the range:
      • Ifn==0|| n>= last- first, there are no effects.
      • Ifn<0, the behavior is undefined.
      • Otherwise, for every integeri in[0last- first- n), moves the element originally at positionfirst+ i to positionfirst+ n+ i. IfI modelsbidirectional_iterator, then the moves are performed in decreasing order ofi starting fromlast- first- n-1.
      2,4) Same as(1) or(3) respectively, but usesr as the range, as if usingranges::begin(r) asfirst andranges::end(r) aslast.

      Elements that are in the original range but not the new range are left in a valid but unspecified state.

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

      Contents

      [edit]Parameters

      first, last - the iterator-sentinel pair defining therange of elements to shift
      r - the range of elements to shift
      n - the number of positions to shift

      [edit]Return value

      1,2){first,/*NEW_LAST*/}, whereNEW_LAST is the end of the resulting range and equivalent to:
      • first+(last- first- n), ifn is less thanlast- first;
      • first otherwise.
      3,4){/*NEW_FIRST*/, last}, whereNEW_FIRST is the beginning of the resulting range and equivalent to:
      • first+ n, ifn is less thanlast- first;
      • last otherwise.

      [edit]Complexity

      1,2) At mostranges::distance(first, last)- n assignments.
      3,4) At mostranges::distance(first, last)- n assignment or swaps.

      [edit]Notes

      ranges::shift_left /ranges::shift_right has better efficiency on common implementations ifI modelsbidirectional_iterator or (better)random_access_iterator.

      Implementations (e.g.MSVC STL) may enable vectorization when the iterator type modelscontiguous_iterator and swapping its value type calls neither non-trivial special member function norADL-foundswap.

      Feature-test macroValueStdFeature
      __cpp_lib_shift202202L(C++23)std::ranges::shift_left andstd::ranges::shift_right

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <string>#include <type_traits>#include <vector> struct S{int value{0};bool specified_state{true};     S(int v=0): value{v}{}    S(Sconst& rhs)=default;    S(S&& rhs){*this= std::move(rhs);}    S& operator=(Sconst& rhs)=default;    S& operator=(S&& rhs){if(this!=&rhs){            value= rhs.value;            specified_state= rhs.specified_state;            rhs.specified_state=false;}return*this;}}; template<typename T>std::ostream& operator<<(std::ostream& os,std::vector<T>const& v){for(constauto& s: v){ifconstexpr(std::is_same_v<T, S>)            s.specified_state? os<< s.value<<' ': os<<". ";elseifconstexpr(std::is_same_v<T,std::string>)            os<<(s.empty()?".": s)<<' ';else            os<< s<<' ';}return os;} int main(){std::cout<<std::left; std::vector<S> a{1,2,3,4,5,6,7};std::vector<int> b{1,2,3,4,5,6,7};std::vector<std::string> c{"α","β","γ","δ","ε","ζ","η"}; std::cout<<"vector<S>\tvector<int>\tvector<string>\n";std::cout<< a<<"  "<< b<<"  "<< c<<'\n';     std::ranges::shift_left(a,3);    std::ranges::shift_left(b,3);    std::ranges::shift_left(c,3);std::cout<< a<<"  "<< b<<"  "<< c<<'\n';     std::ranges::shift_right(a,2);    std::ranges::shift_right(b,2);    std::ranges::shift_right(c,2);std::cout<< a<<"  "<< b<<"  "<< c<<'\n';     std::ranges::shift_left(a,8);// has no effect: n >= last - first    std::ranges::shift_left(b,8);// ditto    std::ranges::shift_left(c,8);// dittostd::cout<< a<<"  "<< b<<"  "<< c<<'\n'; //  std::ranges::shift_left(a, -3); // UB}

      Possible output:

      vector<S>       vector<int>     vector<string>1 2 3 4 5 6 7   1 2 3 4 5 6 7   α β γ δ ε ζ η4 5 6 7 . . .   4 5 6 7 5 6 7   δ ε ζ η . . .. . 4 5 6 7 .   4 5 4 5 6 7 5   . . δ ε ζ η .. . 4 5 6 7 .   4 5 4 5 6 7 5   . . δ ε ζ η .

      [edit]See also

      moves a range of elements to a new location
      (algorithm function object)[edit]
      moves a range of elements to a new location in backwards order
      (algorithm function object)[edit]
      rotates the order of elements in a range
      (algorithm function object)[edit]
      shifts elements in a range
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/shift&oldid=180526"

      [8]ページ先頭

      ©2009-2025 Movatter.jp