Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/move"

From cppreference.com
< cpp‎ | algorithm
(formatting)
(+)
Line 3: Line 3:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl header | algorithm}}
 
{{dcl header | algorithm}}
{{dcl | since=c++11 |
+
{{dcl | since=c++11 | num= 1 |1=
 
template< class InputIt, class OutputIt >
 
template< class InputIt, class OutputIt >
 
OutputIt move( InputIt first, InputIt last, OutputIt d_first );
 
OutputIt move( InputIt first, InputIt last, OutputIt d_first );
 
}}
 
}}
 +
{{dcl | since=c++17 | num= 2 |1=
 +
template< class ExecutionPolicy, class InputIt, class OutputIt >
 +
OutputIt move( ExecutionPolicy&& policy, InputIt first, InputIt last, OutputIt d_first );}}
 
{{dcl end}}
 
{{dcl end}}
  
Moves the elements in the range {{tt|[first, last)}}, to another range beginning at {{tt|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.
+
@1@ Moves the elements in the range {{tt|[first, last)}}, to another range beginning at {{tt|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.
 +
 
 +
@2@ Same as {{v|1}}, but executed according to {{tt|policy}}. This overload does not participate in overload resolution unless {{c|std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>}} is true
  
 
===Parameters===
 
===Parameters===
Line 15: Line 20:
 
{{par | first, last | the range of elements to move}}
 
{{par | first, last | the range of elements to move}}
 
{{par | d_first | the beginning of the destination range. If {{tt|d_first}} is within {{tt|[first, last)}}, {{lc|std::move_backward}} must be used instead of [[cpp/algorithm/move#top | <!--Manually apply syntax highlighting while avoiding automatic hyperlinking to the utility function rather than the algorithm--><span class="mw-geshi cpp source-cpp">std::move</span>]]. }}
 
{{par | d_first | the beginning of the destination range. If {{tt|d_first}} is within {{tt|[first, last)}}, {{lc|std::move_backward}} must be used instead of [[cpp/algorithm/move#top | <!--Manually apply syntax highlighting while avoiding automatic hyperlinking to the utility function rather than the algorithm--><span class="mw-geshi cpp source-cpp">std::move</span>]]. }}
 +
{{par exec pol}}
 
{{par hreq}}
 
{{par hreq}}
 
{{par req concept | InputIt | InputIterator}}
 
{{par req concept | InputIt | InputIterator}}
Line 25: Line 31:
 
===Complexity===
 
===Complexity===
 
Exactly {{tt|last - first}} move assignments.
 
Exactly {{tt|last - first}} move assignments.
 +
 +
===Exceptions===
 +
{{cpp/algorithm/parallel_exceptions_reporting_behavior|singular=yes}}
  
 
===Possible implementation===
 
===Possible implementation===

Revision as of 09:30, 21 March 2016

 
 
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
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
move
(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
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
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 InputIt, class OutputIt >
OutputIt move( ExecutionPolicy&& policy, InputIt first, InputIt last, OutputIt d_first );
(2) (since C++17)
1) Moves the elements in the range [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.
2) Same as (1), but executed according to policy. This overload does not participate in overload resolution unless std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true

Contents

Parameters

first, last - the range of elements to move
d_first - the beginning of the destination range. If d_first is within [first, last), std::move_backward must be used instead of std::move.
policy - the execution policy to use. See execution policy for details.
Type requirements

Template:par req concept Template:par req concept

Return value

Output iterator to the element past the last element moved (d_first + (last - first))

Complexity

Exactly last - first move assignments.

Exceptions

The overload with a template parameter named ExecutionPolicy reports errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

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;
}

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 std::move_backward 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.

#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();
}

Output:

thread 1 ended
thread 2 ended
thread 3 ended

See also

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]