Introduction: Why concatenate vectors and why is it tricky?
\n\nHello, colleague! If you've ever written C++ code that processes lists of data, you've likely faced the task: "I have two std::vectors, how do I make one out of them?" This question is a real hit on Stack Overflow (thousands of views!), and for good reason. At first glance, it seems simple: vector1 + vector2, like in Python? But no. In C++, the + operator is not defined for vectors. You have to get creative.
Today, we'll break down concatenating two std::vectors from start to finish. I'll show you 3 proven methods, explain when to use each one, and warn you about pitfalls that even experienced developers stumble into. Let's go!
\n\nMethod #1: insert() — The Classic
\n\nThe most obvious and frequently used method is insert(). It allows you to insert all elements of one vector at the end of another. The syntax is simple and elegant:
#include <iostream>\n#include <vector>\n\nint main() {\n std::vector<int> vec1 = {1, 2, 3};\n std::vector<int> vec2 = {4, 5, 6};\n\n // Concatenate: insert vec2 at the end of vec1\n vec1.insert(vec1.end(), vec2.begin(), vec2.end());\n\n // Output: 1 2 3 4 5 6\n for (int v : vec1) std::cout << v << " ";\n return 0;\n}\n\nHow it works: We tell the compiler: "Starting from position vec1.end() (i.e., after the last element of the first vector), copy everything between vec2.begin() and vec2.end()." Voilà — merging vectors in C++ is done.
Pros and Cons of insert()
\n- \n
- ✅ Pros: Simplicity, readability, works with any type. \n
- ❌ Cons: If
vec1is small andvec2is huge, multiple memory reallocations may occur during insertion. This reduces performance when concatenating vectors of large sizes. \n
Tip: If you know the final size, call vec1.reserve(vec1.size() + vec2.size()) in advance. This avoids unnecessary allocations.
Method #2: std::merge() — For Sorted Data
\n\nWhat if your vectors are already sorted? Or you need not just to concatenate, but to obtain a single sorted array? That's where the std::merge algorithm from the <algorithm> header comes in.
#include <iostream>\n#include <vector>\n#include <algorithm> // for std::merge\n\nint main() {\n std::vector<int> vec1 = {1, 3, 5};\n std::vector<int> vec2 = {2, 4, 6};\n std::vector<int> result;\n\n // Reserve space to avoid unnecessary copying\n result.reserve(vec1.size() + vec2.size());\n\n // Merge two sorted ranges\n std::merge(vec1.begin(), vec1.end(),\n vec2.begin(), vec2.end(),\n std::back_inserter(result));\n\n // Output: 1 2 3 4 5 6\n for (int v : result) std::cout << v << " ";\n return 0;\n}\n\nNote: std::merge requires both input ranges to be sorted in ascending order. If not, the result will be unpredictable. But if the data is ordered, this method is the king of performance: complexity O(n + m).
When to use std::merge?
\n- \n
- If you're merging the results of two SQL queries or logs sorted by time. \n
- When you need not just concatenating two std::vectors, but specifically a sorted merge. \n
- If the original vectors cannot be modified (we create a third vector
result). \n
Method #3: Move Semantics — Speed for C++11 and Newer
\n\nSuppose you don't need the original vectors after