Namespaces
Variants

std::ranges:: move, std::ranges:: move_result

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace 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:: input_iterator I, std:: sentinel_for < I > S, std:: weakly_incrementable O >

requires std:: indirectly_movable < I, O >
constexpr move_result < I, O >

move ( I first, S last, O result ) ;
(1) (C++20 起)
template < ranges:: input_range R, std:: weakly_incrementable O >

requires std:: indirectly_movable < ranges:: iterator_t < R > , O >
constexpr move_result < ranges:: borrowed_iterator_t < R > , O >

move ( R && r, O result ) ;
(2) (C++20 起)
辅助类型
template < class I, class O >
using move_result = ranges:: in_out_result < I, O > ;
(3) (C++20 起)
1) 将定义在范围 [ first , last ) 内的元素移动到以 result 开始的另一范围。 若 result 位于范围 [ first , last ) 内,则行为未定义。在此情况下,可改用 ranges::move_backward
2) (1) 相同,但使用 r 作为源范围,如同使用 ranges:: begin ( r ) 作为 first ,并使用 ranges:: end ( r ) 作为 last

被移动范围中的元素仍将包含适当类型的有效值,但不一定与移动前的值相同。

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待移动元素 范围 的迭代器-哨位对
r - 待移动元素的范围
result - 目标范围的起始位置

返回值

{ last, result + N } ,其中

1) N = ranges:: distance ( first, last ) .
2) N = ranges:: distance ( r ) .

复杂度

恰好 N 次移动赋值操作。

注释

当移动重叠范围时,若目标范围的起始位置位于源范围之外(向左移动),适合使用 ranges::move ;若目标范围的结束位置位于源范围之外(向右移动),则适合使用 ranges::move_backward

可能的实现

struct move_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O>
    requires std::indirectly_movable<I, O>
    constexpr ranges::move_result<I, O>
        operator()(I first, S last, O result) const
    {
        for (; first != last; ++first, ++result)
            *result = ranges::iter_move(first);
        return {std::move(first), std::move(result)};
    }
    template<ranges::input_range R, std::weakly_incrementable O>
    requires std::indirectly_movable<ranges::iterator_t<R>, O>
    constexpr ranges::move_result<ranges::borrowed_iterator_t<R>, O>
        operator()(R&& r, O result) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(result));
    }
};
inline constexpr move_fn move {};

示例

以下代码将线程对象(其本身是 不可复制的 )从一个容器移动到另一个容器。

#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <list>
#include <thread>
#include <vector>
using namespace std::literals::chrono_literals;
void f(std::chrono::milliseconds n)
{
    std::this_thread::sleep_for(n);
    std::cout << "thread with n=" << n.count() << "ms ended" << std::endl;
}
int main()
{
    std::vector<std::jthread> v;
    v.emplace_back(f, 400ms);
    v.emplace_back(f, 600ms);
    v.emplace_back(f, 800ms);
    std::list<std::jthread> l;
    // std::ranges::copy() would not compile, because std::jthread is non-copyable
    std::ranges::move(v, std::back_inserter(l));
}

输出:

thread with n=400ms ended
thread with n=600ms ended
thread with n=800ms ended

参见

将元素范围按逆序移动到新位置
(算法函数对象)
将元素范围复制到新位置
(算法函数对象)
按逆序复制元素范围
(算法函数对象)
(C++11)
将元素范围移动到新位置
(函数模板)
(C++11)
将实参转换为亡值
(函数模板)