Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an algorithm for moving ranges?

Tags:

c++

c++11

stl

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;
}
like image 480
fredoverflow Avatar asked Jan 16 '10 15:01

fredoverflow


2 Answers

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).

like image 89
ziya Avatar answered Oct 07 '22 12:10

ziya


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).

like image 22
Johannes Dahlström Avatar answered Oct 07 '22 12:10

Johannes Dahlström