std::move_backward(3)
okay bestie, you clicked into std::move_backward(3) and honestly? taste. this one lives in the C++ Standard Libary (manual name and all β yes, the header spells it Libary, and we are not fixing her π
).
β¨ NAME β¨
std::move_backward β std::move_backward (the name section said what it said, and honestly? slay).
πΈ SYNOPSIS πΈ
template< class BidirIt1, class BidirIt2 >
BidirIt2 move_backward( BidirIt1 first, BidirIt1 last,
BidirIt2 d_last );
timeline check: (since C++11), and (constexpr since C++20). both details are important, bestie, don't skip them.
here's the vibe: moves the elements from the range [first, last), to another range ending at d_last. The elements are moved in reverse order (the last element is moved first), but their relative order is preserved. cute, right? π
π¨ okay but for real: ifd_lastis within(first, last], the behavior is undefined. In this case,std::movemay be used instead. undefined behavior is not cute, do not do it.
π PARAMETERS π
first,lastβ the range of the elements to move.d_lastβ end of the destination range.
π§ TYPE REQUIREMENTS π§
BidirIt1,BidirIt2must meet the requirements ofLegacyBidirectionalIterator.
π RETURN VALUE π
Iterator in the destination range, pointing at the last element moved.
β±οΈ COMPLEXITY β±οΈ
Exactly std::distance(first, last) move assignments.
π οΈ 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;
}
π NOTES π
When moving overlapping ranges, std::move is appropriate when moving to the left (beginning of the destination range is outside the source range) while std::move_backward is appropriate when moving to the right (end of the destination range is outside the source range). that's the directional tea β
π 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 (const auto &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 baz
dst: qux quux quuz corge
After:
src: β β β
dst: qux foo bar baz
Overlapping case; before move_backward:
src: snap crackle pop lock drop
After:
src: β β snap crackle pop
π SEE ALSO π
moveβ moves a range of elements to a new location (C++11) (function template).ranges::move_backwardβ moves a range of elements to a new location in backwards order (C++20) (niebloid).
source: http://cppreference.com β’ version/date: 2024.06.10 β’ bye bestie, go move some ranges π
π¦ 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