std::ranges:: move_backward, std::ranges:: move_backward_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<algorithm>
|
||
|
调用签名
|
||
|
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 起) |
[
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 } 。
复杂度
注释
当移动重叠范围时,若目标范围的起始位置位于源范围之外(向左移动),适合使用 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)
|
将参数转换为右值引用
(函数模板) |