Skip to main content

std::merge() algorithm

// (1)
template< class InputIt1, class InputIt2, class OutputIt >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first );

// (2)
template< class InputIt1, class InputIt2, class OutputIt, class Compare >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first, Compare comp );

// (3)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3 >
ForwardIt3 merge( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first );

// (4)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3, class Compare >
ForwardIt3 merge( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first, Compare comp );

Merges two sorted ranges [first1; last1) and [first2; last2) into one sorted range beginning at d_first.

A sequence is said to be sorted with respect to a comparator comp if for any iterator it pointing to the sequence and any non-negative integer n such that it + n is a valid iterator pointing to an element of the sequence, comp(*(it + n), *it) evaluates to false.

  • (1) Elements are compared using operator<.

  • (2) Elements are compared using the given binary comparison function comp.

  • (3 - 4) Same as (1) and (2), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.  (until C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.  (since C++20)

This merge function is stable, which means that for equivalent elements in the original two ranges, the elements from the first range precede the elements from the second range, preserving their original order.

Undefined Behaviour

The behavior is undefined

if the destination range overlaps either of the input ranges (the input ranges may overlap each other).

Parameters

first1
last2

The first range of elements to merge.

first2
last3

The second range of elements to merge.

d_first

The beginning of the destination range.

policy

The execution policy to use. See execution policy for details.

comp

Comparison function object (i.e. an object that satisfies the requirements of Compare). The signature of the comparison function should be equivalent to the following:

bool cmp(const Type1 &a, const Type2 &b);
  • The signature does not need to have const&, but must not modify arguments.
  • Must accept all values of type (possibly const) Type and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (since C++11))
  • The types Type1 and Type2 must be such that an object of type RandomIt can be implicitly converted to both of them.

Type requirements

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
ForwardIt3
LegacyForwardIterator

Return value

An output iterator to element past the last element copied.

Complexity

Given N as std::distance(first1, last1) + std::distance(first2, last2):

  • (1) At most N - 1 comparisons using operator<.
  • (2) O(N) comparisons using operator<.
  • (3) At most N - 1 comparisons using comp.
  • (4) O(N) comparisons using comp.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

merge(1)
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt merge(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first)
{
for (; first1 != last1; ++d_first)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);

if (*first2 < *first1)
{
*d_first = *first2;
++first2;
}
else
{
*d_first = *first1;
++first1;
}
}
return std::copy(first2, last2, d_first);
}
merge(2)
template<class InputIt1, class InputIt2,
class OutputIt, class Compare>
OutputIt merge(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first, Compare comp)
{
for (; first1 != last1; ++d_first)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);

if (comp(*first2, *first1))
{
*d_first = *first2;
++first2;
}
else
{
*d_first = *first1;
++first1;
}
}
return std::copy(first2, last2, d_first);
}

Notes

This algorithm performs a similar task as std::set_union does. Both consume two sorted input ranges and produce a sorted output with elements from both inputs.

The difference between these two algorithms is with handling values from both input ranges which compare equivalent (see notes on LessThanComparable.

If any equivalent values appeared n times in the first range and m times in the second, std::merge would output all n + m occurrences whereas std::set_union would output std::max(n, m) ones only.

So std::merge outputs exactly std::distance(first1, last1) + std::distance(first2, last2) values and std::set_union may produce fewer.

Examples

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

auto print = [](auto const rem, auto const& v)
{
std::cout << rem;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
};

int main()
{
// fill the vectors with random numbers
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0, 9);

std::vector<int> v1(10), v2(10);
std::generate(v1.begin(), v1.end(), std::bind(dis, std::ref(mt)));
std::generate(v2.begin(), v2.end(), std::bind(dis, std::ref(mt)));

print("Originally:\nv1: ", v1);
print("v2: ", v2);

std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());

print("After sorting:\nv1: ", v1);
print("v2: ", v2);

// merge
std::vector<int> dst;
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));

print("After merging:\ndst: ", dst);
}
Possible Output
Originally:
v1: 2 6 5 7 4 2 2 6 7 0
v2: 8 3 2 5 0 1 9 6 5 0
After sorting:
v1: 0 2 2 2 4 5 6 6 7 7
v2: 0 0 1 2 3 5 5 6 8 9
After merging:
dst: 0 0 0 1 2 2 2 2 3 4 5 5 5 6 6 6 7 7 8 9
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::merge() algorithm

// (1)
template< class InputIt1, class InputIt2, class OutputIt >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first );

// (2)
template< class InputIt1, class InputIt2, class OutputIt, class Compare >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first, Compare comp );

// (3)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3 >
ForwardIt3 merge( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first );

// (4)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,
class ForwardIt3, class Compare >
ForwardIt3 merge( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,
ForwardIt3 d_first, Compare comp );

Merges two sorted ranges [first1; last1) and [first2; last2) into one sorted range beginning at d_first.

A sequence is said to be sorted with respect to a comparator comp if for any iterator it pointing to the sequence and any non-negative integer n such that it + n is a valid iterator pointing to an element of the sequence, comp(*(it + n), *it) evaluates to false.

  • (1) Elements are compared using operator<.

  • (2) Elements are compared using the given binary comparison function comp.

  • (3 - 4) Same as (1) and (2), but executed according to policy.

    Overload Resolution

    These overloads participate in overload resolution only if std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true.  (until C++20) std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.  (since C++20)

This merge function is stable, which means that for equivalent elements in the original two ranges, the elements from the first range precede the elements from the second range, preserving their original order.

Undefined Behaviour

The behavior is undefined

if the destination range overlaps either of the input ranges (the input ranges may overlap each other).

Parameters

first1
last2

The first range of elements to merge.

first2
last3

The second range of elements to merge.

d_first

The beginning of the destination range.

policy

The execution policy to use. See execution policy for details.

comp

Comparison function object (i.e. an object that satisfies the requirements of Compare). The signature of the comparison function should be equivalent to the following:

bool cmp(const Type1 &a, const Type2 &b);
  • The signature does not need to have const&, but must not modify arguments.
  • Must accept all values of type (possibly const) Type and Type2, regardless of value category (so Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (since C++11))
  • The types Type1 and Type2 must be such that an object of type RandomIt can be implicitly converted to both of them.

Type requirements

InputIt1
InputIt2
LegacyInputIterator
ForwardIt1
ForwardIt2
ForwardIt3
LegacyForwardIterator

Return value

An output iterator to element past the last element copied.

Complexity

Given N as std::distance(first1, last1) + std::distance(first2, last2):

  • (1) At most N - 1 comparisons using operator<.
  • (2) O(N) comparisons using operator<.
  • (3) At most N - 1 comparisons using comp.
  • (4) O(N) comparisons using comp.

Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

  • If execution of a function invoked as part of the algorithm throws an exception and ExecutionPolicy is one of the standard policies, std::terminate is called. For any other ExecutionPolicy, the behavior is implementation-defined.
  • If the algorithm fails to allocate memory, std::bad_alloc is thrown.

Possible implementation

merge(1)
template<class InputIt1, class InputIt2, class OutputIt>
OutputIt merge(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first)
{
for (; first1 != last1; ++d_first)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);

if (*first2 < *first1)
{
*d_first = *first2;
++first2;
}
else
{
*d_first = *first1;
++first1;
}
}
return std::copy(first2, last2, d_first);
}
merge(2)
template<class InputIt1, class InputIt2,
class OutputIt, class Compare>
OutputIt merge(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first, Compare comp)
{
for (; first1 != last1; ++d_first)
{
if (first2 == last2)
return std::copy(first1, last1, d_first);

if (comp(*first2, *first1))
{
*d_first = *first2;
++first2;
}
else
{
*d_first = *first1;
++first1;
}
}
return std::copy(first2, last2, d_first);
}

Notes

This algorithm performs a similar task as std::set_union does. Both consume two sorted input ranges and produce a sorted output with elements from both inputs.

The difference between these two algorithms is with handling values from both input ranges which compare equivalent (see notes on LessThanComparable.

If any equivalent values appeared n times in the first range and m times in the second, std::merge would output all n + m occurrences whereas std::set_union would output std::max(n, m) ones only.

So std::merge outputs exactly std::distance(first1, last1) + std::distance(first2, last2) values and std::set_union may produce fewer.

Examples

Main.cpp
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

auto print = [](auto const rem, auto const& v)
{
std::cout << rem;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
};

int main()
{
// fill the vectors with random numbers
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> dis(0, 9);

std::vector<int> v1(10), v2(10);
std::generate(v1.begin(), v1.end(), std::bind(dis, std::ref(mt)));
std::generate(v2.begin(), v2.end(), std::bind(dis, std::ref(mt)));

print("Originally:\nv1: ", v1);
print("v2: ", v2);

std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());

print("After sorting:\nv1: ", v1);
print("v2: ", v2);

// merge
std::vector<int> dst;
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));

print("After merging:\ndst: ", dst);
}
Possible Output
Originally:
v1: 2 6 5 7 4 2 2 6 7 0
v2: 8 3 2 5 0 1 9 6 5 0
After sorting:
v1: 0 2 2 2 4 5 6 6 7 7
v2: 0 0 1 2 3 5 5 6 8 9
After merging:
dst: 0 0 0 1 2 2 2 2 3 4 5 5 5 6 6 6 7 7 8 9
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.