C++ std::merge() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::merge() takes two sorted ranges and writes all their elements into a destination in sorted order. Nothing is removed, so duplicates are kept.

This is the same idea as the merge step in merge sort. It is fast because each source is read only once.

Syntax

Add #include <algorithm>, then call std::merge(). Start with these two lines.

C++
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));
std::merge(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp);
CallWhat it does
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));Merge two sorted vectors into a new one.
std::merge(a.begin(), a.end(), b.begin(), b.end(), out.begin(), comp);Merge with your own ordering.
Library declarations
C++
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt merge(InputIt1 first1, InputIt1 last1,
               InputIt2 first2, InputIt2 last2,
               OutputIt d_first);

Parameters

ParameterDescription
first1, last1Required. The first sorted range.
first2, last2Required. The second sorted range.
d_firstRequired. Where the merged result is written.
compOptional. The comparison that both ranges were sorted with.

Return Value

Return ValueDescription
IteratorPoints just after the last element written in the destination.

C++ Examples

Example 1 — Merge two vectors

Combine two sorted lists.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> a = {1, 3, 5};
    std::vector<int> b = {2, 4, 6};
    std::vector<int> out;

    std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));

    for (int x : out) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. At each step merge() compares the front value of each list and writes the smaller one.
  2. The values alternate between a and b here: 1, 2, 3, 4, 5, 6.
  3. The result is sorted without calling sort().

Example 2 — Duplicates are kept

Equal values from both lists all appear in the result.

C++
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> a = {1, 2, 2};
    std::vector<int> b = {2, 3};
    std::vector<int> out;

    std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out));

    for (int x : out) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. a holds two 2s and b holds one.
  2. merge() never drops values, so all three 2s are written.
  3. Use a set operation like set_union() if you want to remove the repeats.

Example 3 — Descending order

Pass std::greater for lists sorted from large to small.

C++
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> a = {9, 5, 1};
    std::vector<int> b = {8, 4};
    std::vector<int> out;

    std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(out),
               std::greater<int>());

    for (int x : out) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. Both lists are sorted from big to small.
  2. std::greater<int>() tells merge() to use that order.
  3. The merged list also goes from big to small.

Frequently Asked Questions

In C++, std::merge() merges two sorted ranges into one sorted range. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns an iterator to the position after the last element written in the destination.
The result is not sorted either. Sort both inputs first.
merge() writes to a separate destination from two ranges. inplace_merge() merges two sorted halves inside one range.

Next: min()

Returns the smaller of two values, or the smallest in a list.

min() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful