std::move
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Defined in header <algorithm>
|
||
template< class InputIt, class OutputIt > OutputIt move( InputIt first, InputIt last, OutputIt d_first ); |
(C++11 起) | |
[first, last)
中的元素,到另一个范围开始于d_first
。此操作后,移离范围中的元素将包含有效的数值适当类型的,但不一定是相同的值前移到.[first, last)
, to another range beginning at d_first
. After this operation the elements in the moved-from range will still contain valid values of the appropriate type, but not necessarily the same values as before the move.目录 |
[编辑] 参数
first, last | - | 的范围内移动的元素
|
d_first | - | 的目标范围的开头。如果 d_first 内[first, last) ,std::move_backward必须使用代替NJ的std ::移动</ span>. 原文: the beginning of the destination range. If d_first is within [first, last) , std::move_backward must be used instead of NJ的std ::移动</ span>. </div></div></div></div></div>
|
Type requirements | ||
-InputIt must meet the requirements of InputIterator .
| ||
-OutputIt must meet the requirements of OutputIterator .
|
[编辑] 返回值
[编辑] 复杂性
last - first
移动作业.last - first
move assignments.[编辑] 可能的实现
template<class InputIt, class OutputIt> OutputIt move(InputIt first, InputIt last, OutputIt d_first) { while (first != last) { *d_first++ = std::move(*first++); } return d_first; } |
[编辑] 为例
下面的代码将线程从一个容器中的对象(它本身并不是拷贝)到另一个地方。</ P>
#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 ::移动</ span>(v.begin(), v.end(), std::back_inserter(l));
for(auto& t : l) t.join();
}
</div>
thread 1 ended thread 2 ended thread 3 ended
[编辑] 另请参阅
(C++11) |
按从后往前的顺序移动某一范围的元素到新的位置 (函数模板) |