Concatenation of two std::vector in C++: how to merge vectors?
Hello everyone! I'm new to C++ and encountered a seemingly simple task: I need to merge two std::vector into one. Let's say I have two vectors of integers:
```cpp
#include
std::vector vec1 = {1, 2, 3};
std::vector vec2 = {4, 5, 6};
```
I want to get a third vector `vec3` that would contain elements from both: `{1, 2, 3, 4, 5, 6}`.
In Python I would just write `vec3 = vec1 + vec2`, but in C++ the `+` operator is not overloaded for vectors. I tried doing this:
```cpp
std::vector vec3 = vec1 + vec2; // Compilation error
```
And got an error. Then I thought about using a `for` loop, but that seems too cumbersome and inefficient. I've seen mentions of `std::merge`, `std::copy` and `insert` online, but I'm confused about which one to choose.
No answers yet.
Log in to leave an answer