开发者

Is there an algorithm for moving ranges?

开发者 https://www.devze.com 2022-12-16 19:43 出处:网络
In C++98, I can copy ranges with the std::copy algorithm. std::copy(source.begin(), source.end(), destination.begin());

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 l开发者_运维百科ast, 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).


Just because this page is a lil confusing right now… There is a std::move( first, last, dest ), OP was just confused by looking at an older C++0x draft. The function is defined by section 25.3.2.

0

精彩评论

暂无评论...
验证码 换一张
取 消