Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/move"

From cppreference.com
< cpp‎ | algorithm
m (Example: add <algorithm> for std::move(first, last, out); '\n' -> endl (to see the output w/o a delay);↓moved comment)
(Uses {{mark constexpr since c++20}}.)
 
(5 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{cpp/algorithm/navbar}}
 
{{cpp/algorithm/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | algorithm}}
+
{{dcl header|algorithm}}
{{dcl rev multi | num=1 | since1=c++11 | until1=c++20 |dcl1=
+
{{dcl|num=1|since=c++11|notes={{mark constexpr since c++20}}|
 
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,
|dcl2=
+
              OutputIt d_first );
template< class InputIt, class OutputIt >
+
constexpr OutputIt move( InputIt first, InputIt last, OutputIt d_first );
+
 
}}
 
}}
{{dcl | since=c++17 | until= | num=2 |1=
+
{{dcl|num=2|since=c++17|
 
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
 
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
ForwardIt2 move( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first );
+
ForwardIt2 move( ExecutionPolicy&& policy,
 +
                ForwardIt1 first, ForwardIt1 last,
 +
                ForwardIt2 d_first );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ Moves the elements in the range {{tt|[first, last)}}, to another range beginning at {{tt|d_first}}, starting from first and proceeding to last - 1. 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 {{range|first|last}}, to another range beginning at {{c|d_first}}, starting from first and proceeding to {{c|last}}. 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}}. {{cpp/algorithm/parallel overload precondition}}
+
@2@ Same as {{v|1}}, but executed according to {{c|policy}}.
 +
@@ {{cpp/algorithm/parallel overload precondition}}
 +
 
 +
If {{c|d_first}} is within the range {{range|first|last}}, the behavior is undefined. In this case, {{lc|std::move_backward}} may be used instead.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{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. The behavior is undefined if {{tt|d_first}} is within the range {{tt|[first, last)}}. In this case, {{lc|std::move_backward}} may 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}}
 
{{par exec pol}}
 
{{par exec pol}}
 
{{par hreq}}
 
{{par hreq}}
{{par req named | InputIt | InputIterator}}
+
{{par req named|InputIt|InputIterator}}
{{par req named | OutputIt | OutputIterator}}
+
{{par req named|OutputIt|OutputIterator}}
{{par req named | ForwardIt1, ForwardIt2| ForwardIterator}}
+
{{par req named|ForwardIt1, ForwardIt2|ForwardIterator}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Output iterator to the element past the last element moved ({{c|d_first + (last - first)}})
+
The iterator to the element past the last element moved.
  
 
===Complexity===
 
===Complexity===
Exactly {{tt|last - first}} move assignments.
+
Exactly {{c|std::distance(first, last)}} move assignments.
  
 
===Exceptions===
 
===Exceptions===
{{cpp/algorithm/parallel_exceptions_reporting_behavior|singular=yes}}
+
{{cpp/algorithm/parallel exceptions reporting behavior|singular=yes}}
  
 
===Possible implementation===
 
===Possible implementation===
{{eq fun | 1=
+
{{eq fun|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)
 
{
 
{
     while (first != last) {
+
     for (; first != last; ++d_first, ++first)
         *d_first++ = std::move(*first++);
+
         *d_first = std::move(*first);
     }
+
      
 
     return d_first;
 
     return d_first;
 
}
 
}
Line 53: Line 56:
  
 
===Notes===
 
===Notes===
 
+
When moving overlapping ranges, {{tt|std::move}} is appropriate when moving to the left (beginning of the destination range is outside the source range) while {{lc|std::move_backward}} is appropriate when moving to the right (end of the destination range is outside the source range).
When moving overlapping ranges, {{tt|std::move}} is appropriate when moving to the left (beginning of the destination range is outside the source range) while {{tt|std::move_backward}} is appropriate when moving to the right (end of the destination range is outside the source range).
+
  
 
===Example===
 
===Example===
Line 60: Line 62:
  
 
{{example
 
{{example
|code=
+
|code=
 
#include <algorithm>
 
#include <algorithm>
 +
#include <chrono>
 
#include <iostream>
 
#include <iostream>
#include <vector>
 
#include <list>
 
 
#include <iterator>
 
#include <iterator>
 +
#include <list>
 
#include <thread>
 
#include <thread>
#include <chrono>
+
#include <vector>
  
 
void f(int n)
 
void f(int n)
Line 75: Line 77:
 
}
 
}
  
int main()  
+
int main()
 
{
 
{
     std::vector<std::thread> v;
+
     std::vector<std::jthread> v;
 
     v.emplace_back(f, 1);
 
     v.emplace_back(f, 1);
 
     v.emplace_back(f, 2);
 
     v.emplace_back(f, 2);
 
     v.emplace_back(f, 3);
 
     v.emplace_back(f, 3);
     std::list<std::thread> l;
+
     std::list<std::jthread> l;
 
+
   
     // copy() would not compile, because std::thread is noncopyable
+
     // copy() would not compile, because std::jthread is noncopyable
     std::move(v.begin(), v.end(), std::back_inserter(l));
+
     std::move(v.begin(), v.end(), std::back_inserter(l));
    for (auto& t : l) t.join();
+
 
}
 
}
 
+
|output=
| output=
+
 
thread 1 ended
 
thread 1 ended
 
thread 2 ended
 
thread 2 ended
Line 96: Line 96:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/algorithm/dsc move_backward}}
+
{{dsc inc|cpp/algorithm/dsc move_backward}}
{{dsc inc | cpp/utility/dsc move}}
+
{{dsc inc|cpp/utility/dsc move}}
 +
{{dsc inc|cpp/algorithm/ranges/dsc move}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:30, 25 March 2024

 
 
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)
(constexpr since C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >

ForwardIt2 move( ExecutionPolicy&& policy,
                 ForwardIt1 first, ForwardIt1 last,

                 ForwardIt2 d_first );
(2) (since C++17)
1) Moves the elements in the range [firstlast), to another range beginning at d_first, starting from first and proceeding to last. 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 participates in overload resolution only if

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.

(until C++20)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.

(since C++20)

If d_first is within the range [firstlast), the behavior is undefined. In this case, std::move_backward may be used instead.

Contents

[edit] Parameters

first, last - the range of elements to move
d_first - the beginning of the destination range
policy - the execution policy to use. See execution policy for details.
Type requirements
-
InputIt must meet the requirements of LegacyInputIterator.
-
OutputIt must meet the requirements of LegacyOutputIterator.
-
ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.

[edit] Return value

The iterator to the element past the last element moved.

[edit] Complexity

Exactly std::distance(first, last) move assignments.

[edit] 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.

[edit] Possible implementation

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

[edit] 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).

[edit] Example

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 ended
thread 2 ended
thread 3 ended

[edit] 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]
moves a range of elements to a new location
(niebloid)[edit]