Some of the merge operation classes are provided in C++ STL under the header file “algorithm”, which facilitates several merge operations in a easy manner.
Some of them are mentioned below.
- merge(beg1, end1, beg2, end2, beg3) :- This function merges two sorted containers and stores in new container in sorted order (merge sort). It takes 5 arguments, first and last iterator of 1st container, first and last iterator of 2nd container and 1st iterator of resultant container.
- includes(beg1, end1, beg2, end2) :- This function is used to check whether one sorted container elements are including other sorted container elements or not. Returns true if 1st container includes 2nd container else returns false.
CPP
#include<iostream>
#include<algorithm> // merge operations
#include<vector> // for vector
using namespace std;
int main()
{
vector< int > v1 = {1, 3, 4, 5, 20, 30};
vector< int > v2 = {1, 5, 6, 7, 25, 30};
vector< int > v3(12);
merge(v1.begin(), v1.end(), v2.begin(),
v2.end(), v3.begin());
cout << "The new container after merging is :\n";
for ( int &x : v3)
cout << x << " ";
cout << endl;
vector< int > v4 = {1, 3, 4, 5, 6, 20, 25, 30};
includes(v4.begin(), v4.end(), v1.begin(), v1.end())?
cout << "v4 includes v1":
cout << "v4 does'nt include v1";
return 0;
}
|
Output
The new container after merging is :
1 1 3 4 5 5 6 7 20 25 30 30
v4 includes v1
Time complexity:
The time complexity of the merge() and include() functions is O(n1 + n2) where n1 and n2 are the sizes of the two containers being merged or checked, respectively.
Space complexity:
The space complexity of the merge() and include() functions is O(n1 + n2) where n1 and n2 are the sizes of the two containers being merged or checked, respectively. This is due to the fact that both functions require an additional container of a size equal to the sum of the sizes of the two containers being merged or checked.
inplace_merge(beg1, beg2, end) :- This function is used to sort two consecutively placed sorted ranges in a single container. It takes 3 arguments, iterator to beginning of 1st sorted range, iterator to beginning of 2nd sorted range, and iterator to last position.
CPP
#include<iostream>
#include<algorithm> // merge operations
#include<vector> // for vector
using namespace std;
int main()
{
vector< int > v1 = {1, 3, 4, 5, 20, 30};
vector< int > v2 = {1, 5, 6, 7, 25, 30};
vector< int > v3(12);
auto it = copy(v1.begin(), v1.end(), v3.begin());
copy(v2.begin(), v2.end(), it);
inplace_merge(v3.begin(),it,v3.end());
cout << "The new container after inplace_merging is :\n";
for ( int &x : v3)
cout << x << " ";
cout << endl;
return 0;
}
|
Output:
The new container after inplace_merging is :
1 1 3 4 5 5 6 7 20 25 30 30
set_union(beg1, end1, beg2, end2, beg3) :- This function computes the set union of two containers and stores in new container .It returns the iterator to the last element of resultant container. It takes 5 arguments, first and last iterator of 1st container, first and last iterator of 2nd container and 1st iterator of resultant container . The containers should be sorted and it is necessary that new container is resized to suitable size.
set_intersection(beg1, end1, beg2, end2, beg3) :- This function computes the set intersection of two containers and stores in new container .It returns the iterator to the last element of resultant container. It takes 5 arguments, first and last iterator of 1st container, first and last iterator of 2nd container and 1st iterator of resultant container . The containers should be sorted and it is necessary that new container is resized to suitable size.
One way to implement set-union and set-intersection in sorted ranges can be found here
CPP
#include<iostream>
#include<algorithm> // for merge operations
#include<vector> // for vector
using namespace std;
int main()
{
vector< int > v1 = {1, 3, 4, 5, 20, 30};
vector< int > v2 = {1, 5, 6, 7, 25, 30};
vector< int > v3(10);
vector< int > v4(10);
auto it = set_union(v1.begin(), v1.end(), v2.begin(),
v2.end(), v3.begin());
auto it1 = set_intersection(v1.begin(),v1.end(),
v2.begin(), v2.end(), v4.begin());
v3.resize(it - v3.begin());
v4.resize(it1 - v4.begin());
cout << "Union of two containers is : ";
for ( int &x : v3)
cout << x << " ";
cout << endl;
cout << "Intersection of two containers is : ";
for ( int &x : v4)
cout << x << " ";
cout << endl;
return 0;
}
|
Output:
Union of two containers is : 1 3 4 5 6 7 20 25 30
Intersection of two containers is : 1 5 30
set_difference(beg1, end1, beg2, end2, beg3) :- This function computes the set difference of two containers and stores in new container .It returns the iterator to the last element of resultant container. It takes 5 arguments, first and last iterator of 1st container, first and last iterator of 2nd container and 1st iterator of resultant container . The containers should be sorted and it is necessary that new container is resized to suitable size.
set_symmetric_difference(beg1, end1, beg2, end2, beg3) :- This function computes the set symmetric difference of two containers and stores in new container .It returns the iterator to the last element of resultant container. It takes 5 arguments, first and last iterator of 1st container, first and last iterator of 2nd container and 1st iterator of resultant container . The containers should be sorted and it is necessary that new container is resized to suitable size.
CPP
#include<iostream>
#include<algorithm> // for merge operations
#include<vector> // for vector
using namespace std;
int main()
{
vector< int > v1 = {1, 3, 4, 5, 20, 30};
vector< int > v2 = {1, 5, 6, 7, 25, 30};
vector< int > v3(10);
vector< int > v4(10);
auto it = set_difference(v1.begin(), v1.end(),
v2.begin(), v2.end(), v3.begin());
auto it1 = set_symmetric_difference(v1.begin(),
v1.end(), v2.begin(), v2.end(), v4.begin());
v3.resize(it - v3.begin());
v4.resize(it1 - v4.begin());
cout << "Difference of two containers is : ";
for ( int &x : v3)
cout << x << " ";
cout << endl;
cout << "symmetric_difference of two containers is : ";
for ( int &x : v4)
cout << x << " ";
cout << endl;
return 0;
}
|
Output:
Difference of two containers is : 3 4 20
Symmetric difference of two containers is : 3 4 6 7 20 25
The time complexity of set_difference() and set_symmetric_difference() is O(m+n) where m and n are the size of the two input containers.
The space complexity of both the functions is O(m+n) as the resulting vector would take the same size as the combined size of the two input vectors.
This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Jun, 2023
Like Article
Save Article