From cppreference.com
Defined in header
template<classInputIt,classOutputIt>OutputItmove(InputItfirst,InputItlast,OutputItd_first); (1)(since C++11)
(constexpr since C++20)template<classExecutionPolicy,classForwardIt1,classForwardIt2>ForwardIt2move(ExecutionPolicy&&policy,ForwardIt1first,ForwardIt1last,ForwardIt2d_first); (2) (since C++17)Moves all elements in the source range [first, last) to the destination range [d_first, std::next(d_first,std::distance(first,last))).
1) Move starts from first and proceeding to last.
If d_first is in the source range, the behavior is undefined.
2) Same as (1), but the move order is determined by policy.
This overload participates in overload resolution only if the value of the following expression is true:
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>
(until C++20)std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>
(since C++20) If the source and destination ranges overlap, the behavior is undefined.
Parameters
first, last - the pair of iterators defining the source
d_first - the beginning of the destination range policy - the
to use Type requirements -InputIt must meet the requirements of
. -OutputIt must meet the requirements of
. -ForwardIt1, ForwardIt2 must meet the requirements of
. Return value
The past-the-end iterator of the destination range.
Complexity
Exactly std::distance(first,last) assignments.
Exceptions
2) During the execution process:
If the temporary memory resources required for parallelization are not available,
is thrown.
If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for
,
is invoked).
Possible implementation
template<classInputIt,classOutputIt>OutputItmove(InputItfirst,InputItlast,OutputItd_first){for(;first!=last;++d_first,++first)*d_first=std::move(*first);returnd_first;}Notes
When moving overlapping ranges, std::move is appropriate when moving to the left (beginning of the destination range is outside the source range) while
is appropriate when moving to the right (end of the destination range is outside the source range).
Example
The following code moves thread objects (which themselves are not copyable) from one container to another.
Run this code
#include<algorithm>#include<chrono>#include<iostream>#include<iterator>#include<list>#include<thread>#include<vector>voidf(intn){std::this_thread::sleep_for(std::chrono::seconds(n));std::cout<<"thread "<<n<<" ended"<<std::endl;}intmain(){std::vector<std::jthread>v;v.emplace_back(f,1);v.emplace_back(f,2);v.emplace_back(f,3);std::list<std::jthread>l;// copy() would not compile, because std::jthread is noncopyablestd::move(v.begin(),v.end(),std::back_inserter(l));}Output:
thread 1 ended thread 2 ended thread 3 ended See also