merge() in STL

merge() in STL

INTRODUCTION

merge() in STL

In this tutorial, we are going to learn about the pre-defined function merge() from the standard template library of C++ programming. merge() is a method that is used to sort and merge two containers into a single container. The merge() function merges the two containers by comparing each value in both containers and merges them in sorted order, this comparison should be done explicitly by the user to merge the two containers in sorted order. The merge() function is available from the < algorithm > header.

Let’s look at the syntax of the merge() function.

merge( itr1_begin , itr1_end , itr2_begin , itr2_end , itr3_begin );  
  • Where,
  • itr1_begin: iterator pointing to the first position of the first container.
  • itr1_end: iterator pointing to the last position of the first container.
  • itr2_begin: iterator pointing to the first position of the second container.
  • itr2_end: iterator pointing to the end position of the second container.
  • itr3_begin: iterator pointing to the first position of the container which merges the two containers.

Let’s look at a sample program implementing the merge() function.

#include <algorithm>
#include<vector>
#include<iostream>
using namespace std;
int main() {
  
    vector<int> arr1 = { 1, 2, 3, 4, 5 };
    vector<int> arr2 = { 9, 8, 2, 3, 4, };
    vector<int> arr3(10);
    
    sort(arr1.begin(), arr1.end());
    sort(arr2.begin(), arr2.end());

    merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin());
    cout << "The merged container is : "<< endl;
 
    for (int i = 0; i < arr3.size(); i++)
        cout << arr3[i] << " ";
    return 0;
}

The output of the program is.

See also  UNORDERED MAP in Cpp

In the above program,

  • we have included the <algorithm> header to implement the merge() function on two containers.
  • We have created two containers and another container which stores the merged containers.
  • First, we sorted the two containers using the sort() function.
  • Then we merged the two containers using the merge() function.

This is the implementation of the merge() function.

We can include another parameter in the merge() function which may be useful to directly sort the two containers or to apply any other operation on the two containers.

Let’s see a sample program.

#include <algorithm>
#include<vector>
#include<iostream>
using namespace std;
struct greaters {
    bool operator()(const long& a, const long& b) const
    {
        return a > b;
    }
};
int main() {
    vector<int> arr1 = { 1, 2, 3, 4, 5 };
    vector<int> arr2 = { 9, 8, 2, 3, 4, };
    vector<int> arr3(10);
    
    sort(arr1.rbegin(), arr1.rend());
    sort(arr2.rbegin(), arr2.rend());
    merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin(), greaters());
   
    cout << "The merged container is : \n";
    for (int i = 0; i < arr3.size(); i++)
        cout << arr3[i] << " ";
    return 0;
}

The output of the program is.

In the above program, we have done a slight modification to the previous problem by including an additional function as a parameter that sorts the containers in the reverse sorted order. As a result, we got the merged container which is in reverse sorted order.

CONCLUSION

That’s it from the tutorial. We have learned about the merge() function and it’s implementation. I hope you guys found it interesting. Happy coding!

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh