std::ranges::move_backward, std::ranges::move_backward_result
Defined in header <algorithm> | ||
|---|---|---|
| Call signature | ||
template< std::bidirectional_iterator I1, std::sentinel_for<I1> S1,
std::bidirectional_iterator I2 >
requires std::indirectly_movable<I1, I2>
constexpr move_backward_result<I1, I2>
move_backward( I1 first, S1 last, I2 d_last );
| (1) | (since C++20) |
template< ranges::bidirectional_range R, std::bidirectional_iterator I >
requires std::indirectly_movable<ranges::iterator_t<R>, I>
constexpr move_backward_result<ranges::borrowed_iterator_t<R>, I>
move_backward( R&& r, I d_last );
| (2) | (since C++20) |
| Helper types | ||
template< class I, class O > using move_backward_result = ranges::in_out_result<I, O>; | (3) | (since C++20) |
Moves all elements from the source range to the destination range ending at d_last, starting from the end and proceeding to the beginning (reverse order).
[first, last).r.If d_last is in (first, last], the behavior is undefined.
The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
Parameters
| first, last | - | the iterator-sentinel pair defining the source range |
| r | - | the source range |
| d_last | - | the past-the-end iterator of the destination range |
Return value
A ranges::move_backward_result object where:
- The data member
inholds the past-the-end iterator of the source range. - The data member
outholds and an iterator to the last move-assigned element in the destination range, ord_lastif no element is moved.
Complexity
ranges::distance(first, last) assignments.ranges::distance(r) assignments.Notes
When moving overlapping ranges, ranges::move is appropriate when moving to the left (beginning of the destination range is outside the source range) while ranges::move_backward is appropriate when moving to the right (end of the destination range is outside the source range).
Possible implementation
struct move_backward_fn
{
template<std::bidirectional_iterator I1, std::sentinel_for<I1> S1,
std::bidirectional_iterator I2>
requires std::indirectly_movable<I1, I2>
constexpr ranges::move_backward_result<I1, I2>
operator()(I1 first, S1 last, I2 d_last) const
{
auto i {last};
for (; i != first; *--d_last = ranges::iter_move(--i))
{}
return {std::move(last), std::move(d_last)};
}
template<ranges::bidirectional_range R, std::bidirectional_iterator I>
requires std::indirectly_movable<ranges::iterator_t<R>, I>
constexpr ranges::move_backward_result<ranges::borrowed_iterator_t<R>, I>
operator()(R&& r, I d_last) const
{
return (*this)(ranges::begin(r),
ranges::next(ranges::begin(r), ranges::end(r)),
std::move(d_last));
}
};
inline constexpr move_backward_fn move_backward{};
|
Example
#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
using Vec = std::vector<std::string>;
void print(std::string_view rem, const Vec& vec)
{
std::cout << rem << "[" << vec.size() << "]: ";
for (const std::string& s : vec)
std::cout << (s.size() ? s : std::string{"·"}) << ' ';
std::cout << '\n';
}
int main()
{
Vec a{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};
Vec b(a.size());
print("Before move:\n" "a", a);
print("b", b);
std::ranges::move_backward(a, b.end());
print("\n" "Move a >> b:\n" "a", a);
print("b", b);
std::ranges::move_backward(b.begin(), b.end(), a.end());
print("\n" "Move b >> a:\n" "a", a);
print("b", b);
std::ranges::move_backward(a.begin(), a.begin()+3, a.end());
print("\n" "Overlapping move a[0, 3) >> a[5, 8):\n" "a", a);
}
Possible output:
Before move: a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ b[8]: · · · · · · · · Move a >> b: a[8]: · · · · · · · · b[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ Move b >> a: a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ b[8]: · · · · · · · · Overlapping move a[0, 3) >> a[5, 8): a[8]: · · · ▄ ▅ ▁ ▂ ▃
See also
|
(C++11) | moves a range of elements to a new location in backwards order (function template) |
|
(C++20) | moves a range of elements to a new location (algorithm function object) |
|
(C++20)(C++20) | copies a range of elements to a new location (algorithm function object) |
|
(C++20) | copies a range of elements in backwards order (algorithm function object) |
|
(C++11) | converts the argument to an xvalue (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/algorithm/ranges/move_backward