merge() in C++ STL

Last Updated : 20 Jan, 2026

C++ STL provides a merge() function to merge two sorted containers into a single sorted container. It is defined in the <algorithm> header and has two main implementations.

Syntax 1: Using default operator

template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator res);

Parameters:

  • beg1: Input iterator pointing to the first element of the first sequence.
  • end1: Input iterator pointing one past the last element of the first sequence.
  • beg2: Input iterator pointing to the first element of the second sequence.
  • end2: Input iterator pointing one past the last element of the second sequence.
  • res: Output iterator pointing to the first position of the resultant container where the merged elements will be stored.

Return value: Returns an iterator pointing to the element past the last element of the resulting merged container.

Code:

CPP
#include <bits/stdc++.h>
using namespace std;

int main(){
   
    vector<int> arr1 = { 1, 4, 6, 3, 2 };
    vector<int> arr2 = { 6, 2, 5, 7, 1 };
    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 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 <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator merge(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator res, Compare comp);

Parameters:

  • beg1 : Input iterator pointing to the first element of the first sequence.
  • end1 : Input iterator pointing one past the last element of the first sequence.
  • beg2 : Input iterator pointing to the first element of the second sequence.
  • end2 : Input iterator pointing one past the last element of the second sequence.
  • res : Output iterator pointing to the first position of the resultant container.
  • comp : Comparator function or function object that returns true/false when comparing two elements. It accepts two arguments and does not modify them.

Return value: Returns an iterator pointing to the element past the last element of the merged container.

Code:

CPP
#include <bits/stdc++.h>
using namespace std;

struct greaters {
    bool operator()(const long& a, const long& b) const
    {
        return a > b;
    }
};

int main()
{
    vector<int> arr1 = { 1, 4, 6, 3, 2 };
    vector<int> arr2 = { 6, 2, 5, 7, 1 };
    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 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
#include <bits/stdc++.h>
using namespace std;

int main(){
    vector<int> stack1 = { 50, 20, 10, 100, 2000 };
    vector<int> stack2 = { 500, 2000, 10, 100, 50 };
    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;

    sort(stack1.begin(), stack1.end());
    sort(stack2.begin(), stack2.end());
    merge(stack1.begin(), stack1.end(), stack2.begin(), stack2.end(), stack3.begin());

    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 

Comment