Difference between revisions of "cpp/algorithm/move"
(seealso the utility move) |
m (Shorten template names. Use {{lc}} where appropriate.) |
||
Line 1: | Line 1: | ||
{{cpp/title|move}} | {{cpp/title|move}} | ||
{{cpp/algorithm/navbar}} | {{cpp/algorithm/navbar}} | ||
− | {{ | + | {{dcl begin}} |
− | {{ | + | {{dcl header | algorithm}} |
− | {{ | + | {{dcl | notes={{mark since c++11}} | |
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 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. | 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=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{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 hreq}} |
− | {{ | + | {{par req concept | InputIt | InputIterator}} |
− | {{ | + | {{par req concept | OutputIt | OutputIterator}} |
− | {{ | + | {{par end}} |
===Return value=== | ===Return value=== | ||
Line 80: | Line 80: | ||
===See also=== | ===See also=== | ||
− | {{ | + | {{dsc begin}} |
− | {{ | + | {{dsc inc | cpp/algorithm/dcl list move_backward}} |
− | {{ | + | {{dsc inc | cpp/utility/dcl list move}} |
− | {{ | + | {{dsc end}} |
[[de:cpp/algorithm/move]] | [[de:cpp/algorithm/move]] |
Revision as of 17:48, 31 May 2013
Defined in header <algorithm>
|
||
template< class InputIt, class OutputIt > OutputIt move( InputIt first, InputIt last, OutputIt d_first ); |
(since C++11) | |
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.
|
Type requirements |
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 InputIt, class OutputIt> OutputIt move(InputIt first, InputIt last, OutputIt d_first) { while (first != last) { *d_first++ = std::move(*first++); } return d_first; } |
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
(C++11) |
converts the argument to an xvalue (function template) |