Given two vectors, join these two vectors using STL in C++.
Example:
Input:
vec1 = {1, 45, 54, 71, 76, 12},
vec2 = {1, 7, 5, 4, 6, 12}
Output: {1, 4, 5, 6, 7, 12, 45, 54, 71, 76}Input:
vec1 = {1, 7, 5, 4, 6, 12},
vec2 = {10, 12, 11}
Output: {1, 4, 5, 6, 7, 10, 11, 12}
Approach: Joining can be done with the help of set_union() function provided in STL.
Syntax:
set_union (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
// C++ program to join two Vectors // using set_union() in STL #include <bits/stdc++.h> using namespace std; int main() { // Get the vector vector<int> vector1 = { 1, 45, 54, 71, 76, 12 }; vector<int> vector2 = { 1, 7, 5, 4, 6, 12 }; // Sort the vector sort(vector1.begin(), vector1.end()); sort(vector2.begin(), vector2.end()); // Print the vector cout << "First Vector: "; for (int i = 0; i < vector1.size(); i++) cout << vector1[i] << " "; cout << endl; cout << "Second Vector: "; for (int i = 0; i < vector2.size(); i++) cout << vector2[i] << " "; cout << endl; // Initialise a vector // to store the common values // and an iterator // to traverse this vector vector<int> v(vector1.size() + vector2.size()); vector<int>::iterator it, st; it = set_union(vector1.begin(), vector1.end(), vector2.begin(), vector2.end(), v.begin()); cout << "\nAfter joining:\n"; for (st = v.begin(); st != it; ++st) cout << *st << ", "; cout << '\n'; return 0; } |
First Vector: 1 12 45 54 71 76 Second Vector: 1 4 5 6 7 12 After joining: 1, 4, 5, 6, 7, 12, 45, 54, 71, 76,
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to join two Arrays using STL in C++?
- Quickly check if two STL vectors contain same elements or not
- Working with Array and Vectors using STL in C++
- Vector of Vectors in C++ STL with Examples
- Map of Vectors in C++ STL with Examples
- Array of Vectors in C++ STL
- Priority Queue of Vectors in C++ STL with Examples
- Set of Vectors in C++ STL with Examples
- Implementation of file allocation methods using vectors
- Difference between std::remove and vector::erase for vectors
- How to flatten a Vector of Vectors or 2D Vector in C++
- Counts of distinct consecutive sub-string of length two using C++ STL
- Quickly merging two sorted arrays using std::merge() in C++ STL
- How to find common elements between two Arrays using STL in C++?
- How to find common elements between two Vector using STL in C++?
- Count number of unique Triangles using STL | Set 1 (Using set)
- Kruskal's Minimum Spanning Tree using STL in C++
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Prim's algorithm using priority_queue in STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

