How to Concatenate Two Vectors in C++? Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Concatenation of two vectors refers to the process of adding the elements of one vector at the end of another vector. In this article, we will learn how to concatenate two vectors in C++.The simplest method to concatenate the two vectors is by using the vector insert() method. Let’s take a look at an example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 4, 7}; vector<int> v2 = {6, 5, 9}; // Concatenate v2 to v1 v1.insert(v1.end(), v2.begin(), v2.end()); for (int i : v1) cout << i << " "; return 0; } Output1 4 7 6 5 9 Explanation: The vector insert() method allows us to insert a range of elements at the given position in the vector. To concatenate, we define the range as the entire second vector and set the position to the end of the first vector.Apart from the method shown above, C++ also provides a few other methods to concatenate the two vector. Some of them are as follows:Using copy()The copy() function can be used to copy one vector at the end of another vector. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 4, 7}; vector<int> v2 = {6, 5, 9}; // Concatenate v2 to v1 copy(v2.begin(), v2.end(), back_inserter(v1)); for (int i : v1) cout << i << " "; return 0; } Output1 4 7 6 5 9 Explanation: We have to use back_insertor() function as the v1 does not have enough space to store the new element. This function creates the space and append the incoming elements at the end.Using Loop with vector push_back()Iterate through the second vector and insert all the elements one by one at the end of first vector using vector push_back() method. C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v1 = {1, 4, 7}; vector<int> v2 = {6, 5, 9}; // Insert all elements of second vector // into first vector for (auto i : v2) v1.push_back(i); for (int i : v1) cout << i << " "; return 0; } Output1 4 7 6 5 9 Comment More infoAdvertise with us Next Article How to Concatenate Two Vectors in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-vector CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Concatenate Two Sets in C++? In C++, sets are the data containers that store the unique elements in some specified order. Concatenating two sets means merging the elements of two sets into the first set. In this article, we will learn how to concatenate two sets in C++ STL. Example Input: mySet1 = {1, 2, 3, 4, 5, 8, 9} mySet2 = 2 min read How to Concatenate Two Arrays in C++? In C++, arrays store a fixed number of elements of the same type in contiguous memory locations. In this article, we will learn how to concatenate two arrays in C++. Example: Input: myArray1={10, 30, 40} myArray2 ={20, 50, 60}Output: Concatenated Array: 10 30 40 20 50 60 Concatenate Two Arrays in On 2 min read How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in 2 min read How to Create a Stack of Vectors in C++? In C++, a stack of vectors can be created using the Standard Template Library (STL). The stack is a container adapter that provides a Last-In-First-Out (LIFO) type of data structure, and a vector is a dynamic array that can grow and shrink in size. In this article, we will learn how to create a stac 2 min read How to Create a Multimap of Vectors in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is not required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of vectors in C++. For Example, Input:myPa 2 min read Like