Add a vector at the end of another

c++, vector

Solution

You can use `insert`:

vec1.insert(vec1.end(), vec2.begin(), vec2.end());

This will add all the elements in the range [`vec2.begin()`, `vec2.end()`) (that is, all the elements in `vec2`) to `vec1`, starting at position `vec1.end()` (that is, right after all the elements of `vec1`).

Hope this helps!

Problem

Is there a method to add a vector at the end of another vector? For example if my vectors are ``` std::vector<int> v1(3); std::vector<int> v2(3); /* ... initialize vectors ... */ /* ... for example, v1 is 1 2 3 and v2 is 4 5 6 ... */ ``` which is the smartest way to add `v2` at the end of `v1` (i.e. to obtain `v1` = 1 2 3 4 5 6) without using a cycle and a `push_back`?

Original source

Related problems