Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::move_backward

      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 BidirIt1,class BidirIt2>
      BidirIt2 move_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last);
      (since C++11)
      (constexpr since C++20)

      Moves the elements from the range[firstlast), to another range ending atd_last. The elements are moved in reverse order (the last element is moved first), but their relative order is preserved.

      Ifd_last is within(firstlast], the behavior is undefined. In this case,std::move may be used instead.

      Contents

      [edit]Parameters

      first, last - the pair of iterators defining the sourcerange of elements to move
      d_last - end of the destination range
      Type requirements
      -
      BidirIt1, BidirIt2 must meet the requirements ofLegacyBidirectionalIterator.

      [edit]Return value

      Iterator in the destination range, pointing at the last element moved.

      [edit]Complexity

      Exactlystd::distance(first, last) move assignments.

      [edit]Possible implementation

      template<class BidirIt1,class BidirIt2>BidirIt2 move_backward(BidirIt1 first, BidirIt1 last, BidirIt2 d_last){while(first!= last)*(--d_last)= std::move(*(--last)); return d_last;}

      [edit]Notes

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

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <iterator>#include <string>#include <string_view>#include <vector> using container=std::vector<std::string>; void print(std::string_view comment,const container& src,const container& dst={}){auto prn=[](std::string_view name,const container& cont){std::cout<< name;for(constauto&s: cont)std::cout<<(s.empty()?"∙": s.data())<<' ';std::cout<<'\n';};std::cout<< comment<<'\n';    prn("src: ", src);if(dst.empty())return;    prn("dst: ", dst);} int main(){    container src{"foo","bar","baz"};    container dst{"qux","quux","quuz","corge"};    print("Non-overlapping case; before move_backward:", src, dst);    std::move_backward(src.begin(), src.end(), dst.end());    print("After:", src, dst);     src={"snap","crackle","pop","lock","drop"};    print("Overlapping case; before move_backward:", src);    std::move_backward(src.begin(),std::next(src.begin(),3), src.end());    print("After:", src);}

      Output:

      Non-overlapping case; before move_backward:src: foo bar bazdst: qux quux quuz corgeAfter:src: ∙ ∙ ∙dst: qux foo bar bazOverlapping case; before move_backward:src: snap crackle pop lock dropAfter:src: ∙ ∙ snap crackle pop

      [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
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/move_backward&oldid=180517"

      [8]ページ先頭

      ©2009-2025 Movatter.jp