std::ranges::move_backward, std::ranges::move_backward_result
来自 cppreference.cn
定义于头文件 <algorithm> |
||
调用签名 (Call signature) |
||
template< std::bidirectional_iterator I1, std::sentinel_for<I1> S1, std::bidirectional_iterator I2 > |
(1) | (C++20 起) |
template< ranges::bidirectional_range R, std::bidirectional_iterator I > requires std::indirectly_movable<ranges::iterator_t<R>, I> |
(2) | (C++20 起) |
辅助类型 |
||
template< class I, class O > using move_backward_result = ranges::in_out_result<I, O>; |
(3) | (C++20 起) |
1) 将由
[
first,
last)
定义的范围内的元素移动到另一个范围 [
d_last - N,
d_last)
,其中 N = ranges::distance(first, last)。元素以逆序移动(最后一个元素最先移动),但它们的相对顺序被保留。如果 d_last 在 (first, last]
范围内,则行为未定义。在这种情况下,可以使用 ranges::move 代替。已移动的范围中的元素仍将包含相应类型的有效值,但不一定是移动之前的值,如同对每个整数 n
(其中 0 ≤ n < N)使用 *(d_last - n) = ranges::iter_move(last - 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) |
将参数转换为亡值 (函数模板) |