Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::move_backward,std::ranges::move_backward_result

      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::bidirectional_iterator I1,std::sentinel_for<I1> S1,

               std::bidirectional_iterator I2>
      requiresstd::indirectly_movable<I1, I2>
      constexpr move_backward_result<I1, I2>

          move_backward( I1 first, S1 last, I2 d_last);
      (1)(since C++20)
      template<ranges::bidirectional_range R,std::bidirectional_iterator I>

      requiresstd::indirectly_movable<ranges::iterator_t<R>, I>
      constexpr move_backward_result<ranges::borrowed_iterator_t<R>, I>

          move_backward( R&& r, I d_last);
      (2)(since C++20)
      Helper types
      template<class I,class O>
      using move_backward_result=ranges::in_out_result<I, O>;
      (3)(since C++20)
      1) Moves the elements in the range, defined by[firstlast), to another range[d_last- Nd_last), whereN=ranges::distance(first, last). The elements are moved in reverse order (the last element is moved first), but their relative order is preserved. The behavior is undefined ifd_last is within(first, last]. In such a case,ranges::move may be used instead.
      2) Same as(1), but usesr as the source range, as if usingranges::begin(r) asfirst, andranges::end(r) aslast.

      The elements in themoved-from range will still contain valid values of the appropriate type, but not necessarily the same values as before the move, as if using*(d_last- n)=ranges::iter_move(last- n) for each integern, where0 ≤ n< N.

      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 move
      r - the range of the elements to move
      d_last - the end of the destination range

      [edit]Return value

      {last, d_last- N}.

      [edit]Complexity

      1) ExactlyN move assignments.
      2) Exactlyranges::distance(r) move assignments.

      [edit]Notes

      When moving overlapping ranges,ranges::move is appropriate when moving to the left (beginning of the destination range is outside the source range) whileranges::move_backward is appropriate when moving to the right (end of the destination range is outside the source range).

      [edit]Possible implementation

      struct move_backward_fn{template<std::bidirectional_iterator I1,std::sentinel_for<I1> S1,std::bidirectional_iterator I2>    requiresstd::indirectly_movable<I1, I2>constexpr ranges::move_backward_result<I1, I2>        operator()(I1 first, S1 last, I2 d_last)const{auto i{last};for(; i!= first;*--d_last=ranges::iter_move(--i)){}return{std::move(last), std::move(d_last)};} template<ranges::bidirectional_range R,std::bidirectional_iterator I>    requiresstd::indirectly_movable<ranges::iterator_t<R>, I>constexpr ranges::move_backward_result<ranges::borrowed_iterator_t<R>, I>        operator()(R&& r, I d_last)const{return(*this)(ranges::begin(r),ranges::end(r), std::move(d_last));}}; inlineconstexpr move_backward_fn move_backward{};

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <string>#include <string_view>#include <vector> using Vec=std::vector<std::string>; void print(std::string_view rem, Vecconst& vec){std::cout<< rem<<"["<< vec.size()<<"]: ";for(conststd::string& s: vec)std::cout<<(s.size()? s:std::string{"·"})<<' ';std::cout<<'\n';} int main(){    Vec a{"▁","▂","▃","▄","▅","▆","▇","█"};    Vec b(a.size());     print("Before move:\n""a", a);    print("b", b);     std::ranges::move_backward(a, b.end());     print("\n""Move a >> b:\n""a", a);    print("b", b);     std::ranges::move_backward(b.begin(), b.end(), a.end());    print("\n""Move b >> a:\n""a", a);    print("b", b);     std::ranges::move_backward(a.begin(), a.begin()+3, a.end());    print("\n""Overlapping move a[0, 3) >> a[5, 8):\n""a", a);}

      Possible output:

      Before move:a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █b[8]: · · · · · · · · Move a >> b:a[8]: · · · · · · · ·b[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ Move b >> a:a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █b[8]: · · · · · · · · Overlapping move a[0, 3) >> a[5, 8):a[8]: · · · ▄ ▅ ▁ ▂ ▃

      [edit]See also

      moves a range of elements to a new location
      (algorithm function object)[edit]
      copies a range of elements to a new location
      (algorithm function object)[edit]
      copies a range of elements in backwards order
      (algorithm function object)[edit]
      (C++11)
      moves a range of elements to a new location
      (function template)[edit]
      (C++11)
      converts the argument to an xvalue
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/move_backward&oldid=180479"

      [8]ページ先頭

      ©2009-2025 Movatter.jp