std::copy_backward(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 if d_last is within (first, last]. std::copy must be used instead of std::copy_backward in 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 πŸŽ€

  • BidirIt must meet the requirements of LegacyBidirectionalIterator. 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 if d_last == first.
    • Correct behavior: 1. made undefined; 2. made well-defined.

πŸ™ˆ 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