Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::copy_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 copy_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last);
      (constexpr since C++20)

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

      The behavior is undefined ifd_last is within(firstlast].std::copy must be used instead ofstd::copy_backward in that case.

      Contents

      [edit]Parameters

      first, last - the pair of iterators defining the sourcerange of elements to copy from
      d_last - the end of the destination range
      Type requirements
      -
      BidirIt must meet the requirements ofLegacyBidirectionalIterator.

      [edit]Return value

      Iterator to the last element copied.

      [edit]Complexity

      Exactlystd::distance(first, last) assignments.

      [edit]Notes

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

      [edit]Possible implementation

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

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <numeric>#include <vector> int main(){std::vector<int> source(4);std::iota(source.begin(), source.end(),1);// fills with 1, 2, 3, 4 std::vector<int> destination(6);     std::copy_backward(source.begin(), source.end(), destination.end()); std::cout<<"destination contains: ";for(auto i: destination)std::cout<< i<<' ';std::cout<<'\n';}

      Output:

      destination contains: 0 0 1 2 3 4

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 1206C++981. the behavior was well-defined ifd_last== last
      2. the behavior was undefined ifd_last== first
      1. made undefined
      2. made well-defined

      [edit]See also

      copies a range of elements to a new location
      (function template)[edit]
      copies a range of elements in backwards order
      (algorithm function object)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/copy_backward&oldid=180519"

      [8]ページ先頭

      ©2009-2025 Movatter.jp