C++ offers in its STL library a merge() which is quite useful to merge sort two containers into a single container. It is defined in header "algorithm". It is implemented in two ways. Syntax 1 : Using operator "<"
Template :
template
outiter merge (initer1 beg1, initer1 end1,
initer2 beg2, initer2 end2,
outiter res)
Parameters :
beg1 : Input iterator to initial position of first sequence.
end1 : Input iterator to final position of first sequence.
beg2 : Input iterator to initial position of second sequence.
end2 : Input iterator to final position of second sequence.
res : Output Iterator to initial position of resultant container.
Return value :
Iterator to last element of the resulting container.
CPP
// C++ code to demonstrate the working of
// merge() implementation 1
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initializing 1st container
vector<int> arr1 = { 1, 4, 6, 3, 2 };
// initializing 2nd container
vector<int> arr2 = { 6, 2, 5, 7, 1 };
// declaring resultant container
vector<int> arr3(10);
// sorting initial containers
sort(arr1.begin(), arr1.end());
sort(arr2.begin(), arr2.end());
// using merge() to merge the initial containers
merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin());
// printing the resultant merged container
cout << "The container after merging initial containers is : ";
for (int i = 0; i < arr3.size(); i++)
cout << arr3[i] << " ";
return 0;
}
Output:
The container after merging initial containers is : 1 1 2 2 3 4 5 6 6 7
Syntax 2 : Using comparator function
Template :
template
outiter merge (initer1 beg1, initer1 end1,
initer2 beg2, initer2 end2,
outiter res, Compare comp)
Parameters :
beg1 : Input iterator to initial position of first sequence.
end1 : Input iterator to final position of first sequence.
beg2 : Input iterator to initial position of second sequence.
end2 : Input iterator to final position of second sequence.
res : Output Iterator to initial position of resultant container.
comp : The comparator function that returns a boolean
true/false of the each elements compared. This function
accepts two arguments. This can be function pointer or
function object and cannot change values.
Return value :
Iterator to last element of the resulting container.
CPP
// C++ code to demonstrate the working of
// merge() implementation 2
#include <bits/stdc++.h>
using namespace std;
// comparator function to reverse merge sort
struct greaters {
bool operator()(const long& a, const long& b) const
{
return a > b;
}
};
int main()
{
// initializing 1st container
vector<int> arr1 = { 1, 4, 6, 3, 2 };
// initializing 2nd container
vector<int> arr2 = { 6, 2, 5, 7, 1 };
// declaring resultant container
vector<int> arr3(10);
// sorting initial containers
// in descending order
sort(arr1.rbegin(), arr1.rend());
sort(arr2.rbegin(), arr2.rend());
// using merge() to merge the initial containers
// returns descended merged container
merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin(), greaters());
// printing the resultant merged container
cout << "The container after reverse merging initial containers is : ";
for (int i = 0; i < arr3.size(); i++)
cout << arr3[i] << " ";
return 0;
}
Output :
The container after reverse merging initial containers is : 7 6 6 5 4 3 2 2 1 1
Possible application : The merge function can be used to make a single stack of two stacks available in sorted order. These can be stack of books or notes. Let us discuss a simple example that merge orders two stack of notes in ascending order into one on basis of its value.
CPP
// C++ code to demonstrate the application of
// merge() stacking notes
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initializing 1st container
// containing denominations
vector<int> stack1 = { 50, 20, 10, 100, 2000 };
// initializing 2nd container
// containing denominations
vector<int> stack2 = { 500, 2000, 10, 100, 50 };
// declaring resultant stack
vector<int> stack3(10);
cout << "The original 1st stack : ";
for (int i = 0; i < 5; i++)
cout << stack1[i] << " ";
cout << endl;
cout << "The original 2nd stack : ";
for (int i = 0; i < 5; i++)
cout << stack2[i] << " ";
cout << endl;
// sorting initial stacks of notes
// in descending order
sort(stack1.begin(), stack1.end());
sort(stack2.begin(), stack2.end());
// using merge() to merge the initial stacks
// of notes
merge(stack1.begin(), stack1.end(), stack2.begin(), stack2.end(), stack3.begin());
// printing the resultant stack
cout << "The resultant stack of notes is : ";
for (int i = 0; i < stack3.size(); i++)
cout << stack3[i] << " ";
return 0;
}
Output :
The original 1st stack : 50 20 10 100 2000
The original 2nd stack : 500 2000 10 100 50
The resultant stack of notes is : 10 10 20 50 50 100 100 500 2000 2000
Similar Reads
Queue in C++ STL
In C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the dat
4 min read
Map in C++ STL
In C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.Example:C++#include <bits/stdc++.h
8 min read
Set in C++ STL
In C++, sets are associative container which stores unique elements in some sorted order. By default, it is sorted ascending order of the keys, but this can be changed as per requirement. It provides fast insertion, deletion and search operations.Example: C++#include <iostream> #include <se
7 min read
make_pair() in C++ STL
In C++, make_pair() is a standard library function used to construct a key-value pair from the given arguments. The type of the pair constructed is deduced automatically from the type of arguments. In this article, we will learn about make_pair() function in C++.Letâs take a quick look at a simple e
3 min read
Pair in C++ STL
In C++, pair is used to combine together two values that may be of different data types or same data types as a single unit. The first element is stored as a data member with name 'first' and the second element as 'second'.Example:CPP#include <bits/stdc++.h> using namespace std; int main() { /
5 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
map::size() in C++ STL
In C++, the std::map::size() is a built-in method used to find the number of elements in std::map container. It is the member function of std::map defined inside <map> header fie. In this article, we will learn about std::map::size() method in C++.Example:C++// C++ Program to illustrate the us
2 min read
set operator= in C++ STL
The â=â is an operator in C++ STL which copies (or moves) a set to another set and set::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an set as an argument and copies it to an set. Syntax: ums1.operator=(set &set
2 min read
map operator= in C++ STL
The map::operator= is a built function in C++ STL which assigns contents of a container to a different container, replacing its current content. Syntax: map1_name = map2_name Parameters: The map on the left is the container in which the map on the right is to be assigned by destroying the elements o
2 min read
forward_list merge() in C++ STL
forward_list::merge() is an inbuilt function in C++ STL which merges two sorted forward_lists into one. The merge() function can be used in two ways: Merge two forward lists that are sorted in ascending order into one. Merge two forward lists into one using a comparison function. Syntax: forwardlist
2 min read