Variations on a theme - std::move and std::move_iterator
As you might guess from the name, or you might have noticed in the preceding implementation, the std::copy
algorithm works by copying elements from the input range to the output. As of C++11, you might wonder: What if instead of copying the elements, we used move semantics to move them from the input to the output?
The STL provides two different approaches to this problem. The first one is the most straightforward: there is a std::move
algorithm (defined in the <algorithm>
header) with the following definition:
template<class InIt, class OutIt> OutIt move(InIt first1, InIt last1, OutIt destination) { while (first1 != last1) { *destination = std::move(*first1); ++first1; ++destination; } return destination; }
It's exactly the same as the std::copy
algorithm except for the addition of a single std::move
on the input element (be careful--this inner std::move
, with one...