Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to append one std::vector to the end of another?

Let v1 be the target vector, v2 needs to be appended to the back of it.

I'm now doing:

v1.reserve(v1.size() + v2.size());  copy(v2.begin(), v2.end(), back_inserter(v1)); 

Is this the most efficient way? Or can it maybe be done just via copying a chunk of memory? Thanks!

like image 346
Alex Jenter Avatar asked Feb 05 '10 15:02

Alex Jenter


People also ask

How do you append one vector 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);

What method adds an item to the end of a vector?

push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the current last element and the container size is increased by 1. 1.

Can we append two vectors in C++?

How to join two Vectors using STL in C++? Given two vectors, join these two vectors using STL in C++. Approach: Joining can be done with the help of set_union() function provided in STL.

Is linked list faster than vector?

the results are directly linked to the allocations that have to be performed, allocation being slow. whatever the data size is, push_back to a vector will always be faster than to a list. this is logical because vector allocates more memory than necessary and so does not need to allocate memory for each element.


2 Answers

After a lot of arguing (and a reasonable comment from Matthieu M. and villintehaspam), I'll change my suggestion to

v1.insert( v1.end(), v2.begin(), v2.end() ); 

I'll keep the former suggestion here:

v1.reserve( v1.size() + v2.size() );  v1.insert( v1.end(), v2.begin(), v2.end() ); 

There are some reasons to do it the latter way, although none of them enough strong:

  • there is no guarantee on to what size will the vector be reallocated -- e.g. if the sum size is 1025, it may get reallocated to 2048 -- dependant on implementation. There is no such guarantee for reserve either, but for a specific implementation it might be true. If hunting for a bottleneck it might be rasonable to check that.
  • reserve states our intentions clear -- optimization may be more efficient in this case (reserve could prepare the cache in some top-notch implementation).
  • also, with reserve we have a C++ Standard guarantee that there will be only a single reallocation, while insert might be implemented inefficiently and do several reallocations (also something to test with a particular implementation).
like image 164
Kornel Kisielewicz Avatar answered Oct 06 '22 10:10

Kornel Kisielewicz


Probably better and simpler to use a dedicated method: vector.insert

v1.insert(v1.end(), v2.begin(), v2.end()); 

As Michael mentions, unless the iterators are input iterators, the vector will figure out the required size and copy appended data at one go with linear complexity.

like image 42
UncleBens Avatar answered Oct 06 '22 10:10

UncleBens