I want to do something like the following (a
and b
are both vector<my_moveable_type>
):
a.insert(a.end(), b.begin(), b.end());
But I want the operation to move b
's elements into a
instead of copying them. I have found std::vector::emplace
but that is just for a single element, not a range.
Can this be done?
Appending a vector elements to another vector To insert/append a vector's elements to another vector, we use vector::insert() function. Syntax: //inserting elements from other containers vector::insert(iterator position, iterator start_position, iterator end_position);
The immediate answer to your question as to fetching access to the last element in a vector can be accomplished using the back() member. Such as: int var = vec. back().
Yes, you can move vectors.
Two vectors can be added together to determine the result (or resultant).
You can use std::make_move_iterator
, so that accesses to the iterator returns rvalue references instead of lvalue references:
a.insert(a.end(), std::make_move_iterator(b.begin()), std::make_move_iterator(b.end()));
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