In C++, given vector<T> src, dst
, both already sorted, is there a more efficient way to merge the contents of src
into dst
than
size_t n = dst.size();
dst.insert(dst.end(), src.begin(), src.end());
std::inplace_merge(dst.begin(), dst.begin() + n, dst.end());
? In the case I care about, T
is a small (12-16 bytes, depending on ABI) POD structure, but each vector contains millions of elements, so the total amount of memory in play is tens to hundreds of megabytes.
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);
Can you combine vectors C++? Using insert() to combine two vectors in C++ vector::insert() is an in-built function in the standard template library of C++. This function can be used to insert elements before the element at a specific position. We can utilize this function to merge two vectors in C++.
Appending to a vector means adding one or more elements at the back of the vector. The C++ vector has member functions. The member functions that can be used for appending are: push_back(), insert() and emplace(). The official function to be used to append is push_back().
It can be done more efficiently if T is heavy to copy and your compiler supports C++0x.
#include <iterator> // for make_move_iterator
size_t n = dst.size();
dst.insert(dst.end(),
std::make_move_iterator(src.begin()),
std::make_move_iterator(src.end()));
std::inplace_merge(dst.begin(), dst.begin() + n, dst.end());
Using make_move_iterator()
will cause insert()
to move the contents of src
into dst
instead of copying them.
Update:
You're dealing with POD types and you're already resizing/copying everything in the dst
vector in the likely case that insert()
overflows the reserve, so it could be faster to just use std::merge()
into a new vector
. This would avoid that initial copy AND have a better worst-case complexity:
inplace_merge()
has a best case of O(n) complexity, but degrades into a worst-case O(n log n) depending on your data.
merge()
has a worst-case O(n) so it is guaranteed to be at least as fast, potentially much faster. It also has move optimization built-in.
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