首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::move

Defined in header <algorithm>

?

?

template< class InputIt, class OutputIt > OutputIt move( InputIt first, InputIt last, OutputIt d_first );

(1)

(since C++11)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > ForwardIt2 move( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );

(2)

(since C++17)

1%29移动范围内的元素[first, last),到另一个范围,从d_first在此操作之后,Move-From范围中的元素仍将包含适当类型的有效值,但不一定包含与移动前相同的值。

2%29与%281%29相同,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

参数

first, last

-

the range of elements to move

d_first

-

the beginning of the destination range. The behavior is undefined if d_first is within the range [first, last). In this case, std::move_backward may be used instead of std::move.

policy

-

the execution policy to use. See execution policy for details.

类型要求

-输入必须符合输入器的要求。

-输出必须符合输出器的要求。

-前进1,前进2必须符合先行者的要求。

返回值

输出迭代器到上一个元素之后的元素移动%28d_first + (last - first)29%。

复杂性

一点儿没错last - first转移任务。

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

模板<class InputIt,class OutputIt>OutputIt移动%28 InputIt First,InputIt Lest,OutputIt d[医]第一%29{而%281%21=最后%29{%2A丁[医]第一++=std::移动%28%2A第一++%29;}返回d[医]第一;}

*。

注记

当移动重叠范围时,std::move当移动到左侧%28时,目标范围的开头位于源范围%29之外std::move_backward当移动到目标区域的右%28端在源范围%29之外时,则是适当的。

下面的代码将线程对象%28从一个容器移动到另一个容器,线程对象本身是不可复制的%29。

二次

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <list>
#include <iterator>
#include <thread>
#include <chrono>
 
void f(int n)
{
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "thread " << n << " ended" << '\n';
}
 
int main() 
{
    std::vector<std::thread> v;
    v.emplace_back(f, 1);
    v.emplace_back(f, 2);
    v.emplace_back(f, 3);
    std::list<std::thread> l;
    // copy() would not compile, because std::thread is noncopyable
 
    std::move(v.begin(), v.end(), std::back_inserter(l)); 
    for (auto& t : l) t.join();
}

二次

产出:

二次

代码语言:javascript
复制
thread 1 ended
thread 2 ended
thread 3 ended

二次

另见

move_backward (C++11)

moves a range of elements to a new location in backwards order (function template)

move (C++11)

obtains an rvalue reference (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com