Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/move"

From cppreference.com
< cpp‎ | algorithm
m (Text replace - "{{eq fun cpp" to "{{eq fun")
Line 9: Line 9:
 
{{ddcl list end}}
 
{{ddcl list end}}
  
Moves the elements in the range {{tt|[first, last)}}, to another range beginning at {{tt|d_first}}.
+
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.
  
 
===Parameters===
 
===Parameters===

Revision as of 15:47, 16 July 2012

 
 
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
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <algorithm>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class InputIterator, class OutputIterator >
OutputIterator move( InputIterator first, InputIterator last, OutputIterator d_first );
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

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.

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.

Return value

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

Complexity

Exactly last - first move assignments.

Possible implementation

template<class InputIterator, class OutputIterator>
OutputIterator move(InputIterator first, InputIterator last, 
                    OutputIterator d_first)
{
    while (first != last) {
        *d_first++ = std::move(*first++);
    }
    return d_first;
}

Example

The following code moves threads handles (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 the thread`s parent 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

Template:cpp/algorithm/dcl list move backward