Can I std::move() an element out of a std::vector?

Is it ok to std::move elements out?

Yes, it’s okay.

If I moved the ith element out, then ith slot goes into an undefined
but valid state

It leaves the element in a valid but unspecified state. The difference between those two words is important in C++, but probably not too important to this question. Just thought I should point it out.

but what about the vector?

The vector is in a valid and well-specified state, though one of its elements is in an unspecified state.

Also, can I clear() and then reuse the vector in the next iteration?

Yes. Of course. Though you can do a lot more than that. Unlike a moved-from vector, you can still count on the vector having the same size, the same capacity, and all elements other than the one you just moved from remaining unchanged. As well as all references and iterators to elements (including the one you just moved from) remaining valid.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)