std::ranges::move_backward, std::ranges::move_backward_result
来自 cppreference.cn
定义于头文件 <algorithm> |
||
调用签名 |
||
template< std::bidirectional_iterator I1, std::sentinel_for<I1> S1, std::bidirectional_iterator I2 > |
(1) | (since C++20) |
template< ranges::bidirectional_range R, std::bidirectional_iterator I > requires std::indirectly_movable<ranges::iterator_t<R>, I> |
(2) | (since C++20) |
辅助类型 |
||
template< class I, class O > using move_backward_result = ranges::in_out_result<I, O>; |
(3) | (since C++20) |
1) 将范围
[
first,
last)
中的元素移动到另一个范围 [
d_last - N,
d_last)
,其中 N = ranges::distance(first, last)。元素以逆序移动(最后一个元素先移动),但它们的相对顺序被保留。如果 d_last 在 (first, last]
范围内,则行为未定义。在这种情况下,可以改用 ranges::move。移动来源范围中的元素仍将包含适当类型的有效值,但不一定与移动前的值相同,如同对每个整数 n
使用 *(d_last - n) = ranges::iter_move(last - n),其中 0 ≤ n < N。
此页面上描述的类似函数的实体是算法函数对象(非正式地称为 niebloids),即
内容 |
[编辑] 参数
first, last | - | 定义要移动的元素范围的迭代器-哨位对 |
r | - | 要移动的元素的范围 |
d_last | - | 目标范围的末尾 |
[编辑] 返回值
{last, d_last - N}.
[编辑] 复杂度
1) 精确地 N 次移动赋值。
2) 精确地 ranges::distance(r) 次移动赋值。
[编辑] 注解
当移动重叠范围时,如果向左移动(目标范围的开头在源范围之外),则 ranges::move 是合适的;而当向右移动(目标范围的末尾在源范围之外)时,ranges::move_backward 是合适的。
[编辑] 可能的实现
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::end(r), std::move(d_last)); } }; inline constexpr move_backward_fn move_backward {}; |
[编辑] 示例
运行此代码
#include <algorithm> #include <iostream> #include <string> #include <string_view> #include <vector> using Vec = std::vector<std::string>; void print(std::string_view rem, Vec const& 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); }
可能的输出
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]: · · · ▄ ▅ ▁ ▂ ▃
[编辑] 参见
(C++20) |
将元素范围移动到新位置 (算法函数对象) |
(C++20)(C++20) |
将元素范围复制到新位置 (算法函数对象) |
(C++20) |
以相反的顺序复制元素范围 (算法函数对象) |
(C++11) |
将元素范围移动到新位置 (函数模板) |
(C++11) |
将实参转换为 xvalue (函数模板) |