This C++ program merges two sequences using inplace_merge() algorithm. This algorithm combines the elements present in the two consecutive sorted ranges [first,middle) and [middle,last) into a combined sorted range [first,last).
Here is the source code of the C++ program which merges two sequences using inplace_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 inplace_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);
vector<int>::iterator it;
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
it = copy(v1.begin(), v1.end(), v3.begin());
copy(v2.begin(), v2.end(), it);
inplace_merge(v3.begin(), it, v3.end());
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:
- Apply for C++ Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Check Programming Books
- Check C++ Books