In C++98, I can copy ranges with the std::copy
algorithm.
std::copy(source.begin(), source.end(), destination.begin());
Is there an algorithm in C++0x that moves the elements from source to destination? Or is std::copy
somehow overloaded to accept something like rvalue iterators -- is there even such a thing?
The algorithm might look something like this:
#include <utility>
template<class InputIterator, class OutputIterator>
OutputIterator mooove(InputIterator first, InputIterator last, OutputIterator result)
{
for (; first != last; ++first, ++last) *result = std::move(*first);
return result;
}
It seems to be in the latest draft (see section 25.3.2).
I have a hard copy of C++03 which is exactly the same as C++98 (sections 25.2.x) where you can see the same algorithms (without 'move' obviously).
There is also an iterator adaptor, std::move_iterator
, that can be used to adapt any range algorithm that makes copies to move the elements instead. std::move(first, last, dest)
is just a convenience wrapper for the most common use case -- it is semantically equivalent to std::copy(std::move_iterator(first), std::move_iterator(last), dest)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With