Why can't we move iterators?

C ++ 11 introduced the โ€œmoveโ€ algorithm, which behaves like a โ€œcopyโ€ algorithm, except that it ... moves data, not copies it. I am wondering why the commits did not update the copy algorithm to use instead (or perhaps in addition to this).

A vector provides an iterator T & A vector const provides an iterator const T & Is there a reason why the vector && could not provide an iterator T &? This would allow you to move elements from the list of words to the vector using the vector constructor ...

It is a bad idea?

+7
source share
2 answers

We already have. Use std::make_move_iterator to create a move iterator.

+16
source

See std::move_iterator .

 #include <list> #include <vector> #include <iostream> struct A { A() = default; A(A&&) {std::cout << "move\n";} A(const A&) = default; }; int main() { std::list<A> l = {A(), A(), A()}; std::vector<A> v(std::make_move_iterator(l.begin()), std::make_move_iterator(l.end())); } move move move 
+10
source

All Articles