Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Defined in header <algorithm> | ||
template<class InputIt,class OutputIt> OutputIt move( InputIt first, InputIt last, | (1) | (since C++11) (constexpr since C++20) |
template<class ExecutionPolicy,class ForwardIt1,class ForwardIt2> ForwardIt2 move( ExecutionPolicy&& policy, | (2) | (since C++17) |
[
first,
last)
, to another range beginning atd_first, starting from first and proceeding tolast. 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.std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue. | (until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue. | (since C++20) |
Ifd_first is within the range[
first,
last)
, the behavior is undefined. In this case,std::move_backward may be used instead.
Contents |
first, last | - | the pair of iterators defining the sourcerange of elements to move |
d_first | - | the beginning of the destination range |
policy | - | theexecution policy to use |
Type requirements | ||
-InputIt must meet the requirements ofLegacyInputIterator. | ||
-OutputIt must meet the requirements ofLegacyOutputIterator. | ||
-ForwardIt1, ForwardIt2 must meet the requirements ofLegacyForwardIterator. |
The iterator to the element past the last element moved.
Exactlystd::distance(first, last) move assignments.
The overload with a template parameter namedExecutionPolicy
reports errors as follows:
ExecutionPolicy
is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined.template<class InputIt,class OutputIt>OutputIt move(InputIt first, InputIt last, OutputIt d_first){for(; first!= last;++d_first,++first)*d_first= std::move(*first); return d_first;} |
When moving overlapping ranges,std::move
is appropriate when moving to the left (beginning of the destination range is outside the source range) whilestd::move_backward is appropriate when moving to the right (end of the destination range is outside the source range).
The following code moves thread objects (which themselves are not copyable) from one container to another.
#include <algorithm>#include <chrono>#include <iostream>#include <iterator>#include <list>#include <thread>#include <vector> void f(int n){std::this_thread::sleep_for(std::chrono::seconds(n));std::cout<<"thread "<< n<<" ended"<<std::endl;} int main(){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 noncopyable std::move(v.begin(), v.end(),std::back_inserter(l));}
Output:
thread 1 endedthread 2 endedthread 3 ended
(C++11) | moves a range of elements to a new location in backwards order (function template)[edit] |
(C++11) | converts the argument to an xvalue (function template)[edit] |
(C++20) | moves a range of elements to a new location (algorithm function object)[edit] |