Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge vector into existing vector

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.

like image 241
zwol Avatar asked Jul 06 '11 16:07

zwol


People also ask

How do you add vectors to another vector?

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++?

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++.

What is append vector?

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().


1 Answers

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.

like image 108
Cory Nelson Avatar answered Oct 09 '22 09:10

Cory Nelson