std::copy_backward(3)
π
std::copy_backward(3) β the backwards-copying icon π
okay so bestie, this is std::copy_backward(3), straight outta the C++ Standard Libary (thatβs the manual name, periodt). Itβs giving βcopy a range, but start from the back so the order stays cute.β π¦
β¨ NAME β¨
std::copy_backward - std::copy_backward. Thatβs the name, no cap. π
π Synopsis π
Defined in header <algorithm>:
template< class BidirIt1, class BidirIt2 >
BidirIt2 copy_backward( BidirIt1 first, BidirIt1 last,
(constexpr since C++20)
BidirIt2 d_last );
So what does it do? It copies the elements from the range [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. Yes bestie, itβs backwards but not chaotic. β¨
β οΈ The behavior is undefined ifd_lastis within(first, last].std::copymust be used instead ofstd::copy_backwardin that case. Do not play with this one, fr fr. π
πΈ Parameters πΈ
first,lastβ the range of the elements to copy from.d_lastβ the end of the destination range.
π Type requirements π
BidirItmust meet the requirements ofLegacyBidirectionalIterator. Thatβs the vibe check for your iterator, and it has to pass. π«Ά
π₯ Return value π₯
Iterator to the last element copied. Thatβs the returned glow-up. β
β Complexity β
Exactly std::distance(first, last) assignments. Not more, not less β the algorithm is literally counting its steps and staying in its lane. π
π§ Notes π§
When copying overlapping ranges, std::copy is appropriate when copying to the left (beginning of the destination range is outside the source range), while std::copy_backward is appropriate when copying to the right (end of the destination range is outside the source range). This is the overlapping-ranges lore, iykyk. π
π 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;
}
π Example π
Run this code, bestie β the page literally says // 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
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. History really said βwe fixed that,β and honestly? Slay.
- DR
LWG 1206, applied to C++98:- Behavior as published: 1. the behavior was well-defined if
d_last == last; 2. the behavior was undefined ifd_last == first. - Correct behavior: 1. made undefined; 2. made well-defined.
- Behavior as published: 1. the behavior was well-defined if
π See also π
copyβ copies a range of elements to a new location. πcopy_ifβ(function template),(C++11). πranges::copy_backwardβ copies a range of elements in backwards order;(C++20),(niebloid). π¦
made with π by your Unix bestie β source: http://cppreference.com, version/date: 2024.06.10, page: std::copy_backward(3). Bye bestie, go copy backwards responsibly! π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms