Given
std::list<std::vector<float>> foo;
std::vector<float> bar;
How should I move bar
to the end of foo
without copying data?
Is this ok?
foo.emplace_back(std::move(bar));
Is this ok?
foo.emplace_back(std::move(bar));
Yes, because:
std::move(bar)
casts bar
to an rvalue reference.
std::list::emplace_back
takes any number of forwarding references and uses them to construct an element at the end.
std::vector::vector
has an overload (6) that takes an rvalue reference, which moves the contents of the rhs vector without performing any copy.
Yes, the code in the Q is certainly OK. Even using push_back
would be OK:
foo.push_back(std::move(bar));
How should I move
bar
to the end offoo
without copying data?
Using the move constructor of std::vector
:
foo.push_back(std::move(bar));
Is this ok?
foo.emplace_back(std::move(bar));
That is OK as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With