This C++ program merges two sequences using merge() algorithm. This algorithm combines the elements present in the two sorted ranges into a new range given as an argument to the merge() routine.
Here is the source code of the C++ program which merges two sequences using merge() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to merge two sequences using merge() algorithm
*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
cout << '\n';
}
int main () {
vector<int> v1 = {5,10,15,20,25}, v2 = {50,40,30,20,10}, v3(10);
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
cout << "Vector v1 : ";
printVector(v1);
cout << "Vector v2 : ";
printVector(v2);
cout << "Vector v3 : ";
printVector(v3);
}
$ gcc test.cpp $ a.out Vector v1 : 5 10 15 20 25 Vector v2 : 10 20 30 40 50 Vector v3 : 5 10 10 15 20 20 25 30 40 50
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check C++ Books
- Practice Programming MCQs
- Check Programming Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs