Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
Last Updated :
30 Jun, 2023
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
// C++ code to demonstrate the working of
// merge() and include()
#include<iostream>
#include<algorithm> // merge operations
#include<vector> // for vector
using namespace std;
int main()
{
// 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 merging
vector<int> v3(12);
// Using merge() to merge vectors v1 and v2
// and storing result in v3
merge(v1.begin(), v1.end(), v2.begin(),
v2.end(), v3.begin());
// Displaying resultant container
cout << "The new container after merging is :\n";
for (int &x : v3)
cout << x << " ";
cout << endl;
// Initializing new vector
vector<int> v4 = {1, 3, 4, 5, 6, 20, 25, 30};
// Using include() to check if v4 contains v1
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
// C++ code to demonstrate the working of
// inplace_merge()
#include<iostream>
#include<algorithm> // merge operations
#include<vector> // for vector
using namespace std;
int main()
{
// 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
auto it = 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 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
// C++ code to demonstrate the working of
// set_union() and set_intersection()
#include<iostream>
#include<algorithm> // for merge operations
#include<vector> // for vector
using namespace std;
int main()
{
// 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 union
vector<int> v3(10);
// Declaring resultant vector
// for intersection
vector<int> v4(10);
// using set_union() to compute union of 2
// containers v1 and v2 and store result in v3
auto it = set_union(v1.begin(), v1.end(), v2.begin(),
v2.end(), v3.begin());
// using set_intersection() to compute intersection
// of 2 containers v1 and v2 and store result in v4
auto it1 = 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;
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
// C++ code to demonstrate the working of
// set_difference() and set_symmetric_difference()
#include<iostream>
#include<algorithm> // for merge operations
#include<vector> // for vector
using namespace std;
int main()
{
// 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.
auto it = set_difference(v1.begin(), v1.end(),
v2.begin(), v2.end(), v3.begin());
// using set_symmetric_difference() to compute
// symmetric_difference/ of 2 containers
auto it1 = 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;
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 [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Similar Reads
C++ STL Algorithm Library
Standard Template Library (STL) offers a rich collection of algorithms designed to operate on STL containers and beyond. It provides commonly used algorithms such as sorting, searching, copying, etc. These well tested algorithms are optimized for performance and provide a way to write cleaner, faste
3 min read
sort() in C++ STL
In C++, sort() is a built-in function used to sort the given range in desired order. It provides a simple and efficient way to sort the data in C++, but it only works on data structures that provide random access to its elements such as vectors and arrays.Let's take a look at an example:C++#include
4 min read
Type Inference in C++ (auto and decltype)
Type Inference is a feature in C++, using which the compiler automatically deduces the data type of an expression, function, or variable. Type inference was introduced with C++11 through the use of the auto and decltype.Before C++ 11, each data type had to be explicitly declared, which limited the v
5 min read
transform() in C++ STL
In C++, transform() is a built-in STL function used to apply the given operation to a range of elements and store the result in another range. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { vector<i
4 min read
Variadic function templates in C++
Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue. Douglas Gregor
3 min read
Template Specialization in C++
Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type. What if we want a different code for a p
5 min read
Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
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
7 min read
std::partition in C++ STL
C++ has a class in its STL algorithms library which allows us easy partition algorithms using certain inbuilt functions. Partition refers to act of dividing elements of containers depending upon a given condition. Partition operations :1. partition(beg, end, condition) :- This function is used to pa
5 min read
accumulate() and partial_sum() in C++ STL
accumulate() and partial_sum() functions are used to find the sum or any other accumulated value that is obtained by doing the addition or any other binary operation on the elements in the given range. Both of these functions are the part of STL Numeric Library and defined inside <numeric> hea
4 min read
numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc.
4 min read