Przejdź do głównej zawartości

std::copy_backward() algorithm

// (1)
template< class BidirIt1, class BidirIt2 >
constexpr BidirIt2 copy_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );
  • (1) Copies the elements from the range, defined by [first; last), to another range ending at d_last. The elements are copied in reverse order (the last element is copied first), but their relative order is preserved.

    Undefined Behaviour

    The behavior is undefined if d_first is within [first; last). In this case, std::copy_backward may be used instead.

Parameters

first
last

The range of elements to copy.

d_first

The beginning of the destination range.

Type requirements

BidirItLegacyBidirectionalIterator

Return value

Iterator to the last element copied.

Complexity

Exactly last - first assignments.

Exceptions

(none)

Possible implementation

copy_backward (1)

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

return d_last;
}

Notes

When copying overlapping ranges:

  • std::copy is appropriate when copying to the left (beginning of the destination range is outside the source range).
  • std::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range).

Examples

Main.cpp
#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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::copy_backward() algorithm

// (1)
template< class BidirIt1, class BidirIt2 >
constexpr BidirIt2 copy_backward( BidirIt1 first, BidirIt1 last, BidirIt2 d_last );
  • (1) Copies the elements from the range, defined by [first; last), to another range ending at d_last. The elements are copied in reverse order (the last element is copied first), but their relative order is preserved.

    Undefined Behaviour

    The behavior is undefined if d_first is within [first; last). In this case, std::copy_backward may be used instead.

Parameters

first
last

The range of elements to copy.

d_first

The beginning of the destination range.

Type requirements

BidirItLegacyBidirectionalIterator

Return value

Iterator to the last element copied.

Complexity

Exactly last - first assignments.

Exceptions

(none)

Possible implementation

copy_backward (1)

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

return d_last;
}

Notes

When copying overlapping ranges:

  • std::copy is appropriate when copying to the left (beginning of the destination range is outside the source range).
  • std::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range).

Examples

Main.cpp
#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
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.