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.
The new container after merging is :
1 1 3 4 5 5 6 7 20 25 30 30
v4 includes v1
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
// C++ code to demonstrate the working of
// inplace_merge()
#include<iostream>
#include<algorithm> // merge operations
#include<vector> // for vector
usingnamespacestd;
intmain()
{
// Initializing 1st vector
vector<int> v1 = {1, 3, 4, 5, 20, 30};
// Initializing 2nd vector
vector<int> v2 = {1, 5, 6, 7, 25, 30};
// Declaring resultant vector
// for inplace_merge()
vector<int> v3(12);
// using copy to copy both vectors into
// one container
autoit = copy(v1.begin(), v1.end(), v3.begin());
copy(v2.begin(), v2.end(), it);
// Using inplace_merge() to sort the container
inplace_merge(v3.begin(),it,v3.end());
// Displaying resultant container
cout << "The newcontainer after inplace_merging is :\n";
for(int&x : v3)
cout << x << " ";
cout << endl;
return0;
}
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
// using set_intersection() to compute intersection
// of 2 containers v1 and v2 and store result in v4
autoit1 = set_intersection(v1.begin(),v1.end(),
v2.begin(), v2.end(), v4.begin());
// resizing new container
v3.resize(it - v3.begin());
// resizing new container
v4.resize(it1 - v4.begin());
// Displaying set union
cout << "Union of two containers is : ";
for(int&x : v3)
cout << x << " ";
cout << endl;
// Displaying set intersection
cout << "Intersection of two containers is : ";
for(int&x : v4)
cout << x << " ";
cout << endl;
return0;
}
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
// C++ code to demonstrate the working of
// set_difference() and set_symmetric_difference()
#include<iostream>
#include<algorithm> // for merge operations
#include<vector> // for vector
usingnamespacestd;
intmain()
{
// Initializing 1st vector
vector<int> v1 = {1, 3, 4, 5, 20, 30};
// Initializing 2nd vector
vector<int> v2 = {1, 5, 6, 7, 25, 30};
// Declaring resultant vector
// for difference
vector<int> v3(10);
// Declaring resultant vector
// for symmetric_difference
vector<int> v4(10);
// using set_difference() to compute difference
// of 2 containers v1 and v2.
autoit = set_difference(v1.begin(), v1.end(),
v2.begin(), v2.end(), v3.begin());
// using set_symmetric_difference() to compute
// symmetric_difference/ of 2 containers
autoit1 = set_symmetric_difference(v1.begin(),
v1.end(), v2.begin(), v2.end(), v4.begin());
// resizing new container
v3.resize(it - v3.begin());
// resizing new container
v4.resize(it1 - v4.begin());
// Displaying set difference
cout << "Difference of two containers is : ";
for(int&x : v3)
cout << x << " ";
cout << endl;
// Displaying set symmetric_difference
cout << "symmetric_difference of two containers is : ";
for(int&x : v4)
cout << x << " ";
cout << endl;
return0;
}
Output:
Difference of two containers is : 3 4 20
Symmetric difference of two containers is : 3 4 6 7 20 25
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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy