Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::shift_left,std::shift_right

      From cppreference.com
      <cpp‎ |algorithm
       
       
      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
       
      Defined in header<algorithm>
      template<class ForwardIt>

      constexpr ForwardIt shift_left( ForwardIt first, ForwardIt last,
                                     typenamestd::iterator_traits<ForwardIt>::

                                         difference_type n);
      (1)(since C++20)
      template<class ExecutionPolicy,class ForwardIt>

      ForwardIt shift_left( ExecutionPolicy&& policy,
                            ForwardIt first, ForwardIt last,
                           typenamestd::iterator_traits<ForwardIt>::

                               difference_type n);
      (2)(since C++20)
      template<class ForwardIt>

      constexpr ForwardIt shift_right( ForwardIt first, ForwardIt last,
                                       typenamestd::iterator_traits<ForwardIt>::

                                           difference_type n);
      (3)(since C++20)
      template<class ExecutionPolicy,class ForwardIt>

      ForwardIt shift_right( ExecutionPolicy&& policy,
                             ForwardIt first, ForwardIt last,
                             typenamestd::iterator_traits<ForwardIt>::

                                 difference_type n);
      (4)(since C++20)

      Shifts the elements in the range[firstlast) byn positions.

      1) Shifts the elements towards the beginning of the range.
      • Ifn==0|| n>= last- first, there are no effects.
      • 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.
      • Otherwise, for every integeri in[0last- first- n), moves the element originally at positionfirst+ i to positionfirst+ n+ i.
      IfForwardIt meets theLegacyBidirectionalIterator requirements, then the moves are performed in decreasing order ofi starting fromlast- first- n-1.
      2,4) Same as(1) and(3), respectively, but executed according topolicy and the moves may be performed in any order.
      These overloads participate in overload resolution only ifstd::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue.

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

      If any of the following conditions is satisfied, the behavior is undefined:

      Contents

      [edit]Parameters

      first, last - the pair of iterators defining therange of elements to shift
      n - the number of positions to shift
      policy - theexecution policy to use
      Type requirements
      -
      ForwardIt must meet the requirements ofLegacyForwardIterator.

      [edit]Return value

      1,2) The end of the resulting range.
      3,4) The beginning of the resulting range.
      • Ifn is less thanstd::distance(first, last), returns an iterator equal tostd::next(first, n).
      • Otherwise, returnslast.

      [edit]Complexity

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

      [edit]Exceptions

      The overloads with a template parameter namedExecutionPolicy report errors as follows:

      • If execution of a function invoked as part of the algorithm throws an exception andExecutionPolicy is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy, the behavior is implementation-defined.
      • If the algorithm fails to allocate memory,std::bad_alloc is thrown.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_shift201806L(C++20)std::shift_left andstd::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::shift_left(begin(a), end(a),3);    std::shift_left(begin(b), end(b),3);    std::shift_left(begin(c), end(c),3);std::cout<< a<<"  "<< b<<"  "<< c<<'\n';     std::shift_right(begin(a), end(a),2);    std::shift_right(begin(b), end(b),2);    std::shift_right(begin(c), end(c),2);std::cout<< a<<"  "<< b<<"  "<< c<<'\n';     std::shift_left(begin(a), end(a),8);// has no effect: n >= last - first    std::shift_left(begin(b), end(b),8);// ditto    std::shift_left(begin(c), end(c),8);// dittostd::cout<< a<<"  "<< b<<"  "<< c<<'\n'; //  std::shift_left(begin(a), end(a), -3); // UB, e.g. segfault}

      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

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp