On this page
std::ranges::merge, std::ranges::merge_result
Defined in header <algorithm> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
Helper types | ||
|
(3) | (since C++20) |
Merges two sorted ranges [
[first1
,
last1
)
and [
first2
,
last2
)
into one sorted range beginning at result
.
A sequence is said to be sorted with respect to the 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, std::invoke(comp, std::invoke(proj2, *(it + n)), std::invoke(proj1, *it)))
evaluates to false
.
comp
.
r1
as the first range and r2
as the second range, as if using ranges::begin(r1)
as first1
, ranges::end(r1)
as last1
, ranges::begin(r2)
as first2
, and ranges::end(r2)
as last2
.
The behavior is undefined if the destination range overlaps either of the input ranges (the input ranges may overlap each other).
This merge function is stable, which means that for equivalent elements in the original two ranges, the elements from the first range (preserving their original order) precede the elements from the second range (preserving their original order).
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions.
Parameters
first1, last1 | - | the first input sorted range |
first2, last2 | - | the second input sorted range |
result | - | the beginning of the output range |
comp | - | comparison to apply to the projected elements |
proj1 | - | projection to apply to the elements in the first range |
proj2 | - | projection to apply to the elements in the second range |
Return value
{last1, last2, result_last}
, where result_last
is the end of the constructed range.
Complexity
At most N − 1
comparisons and applications of each projection, where N = ranges::distance(first1, last1) + ranges::distance(first2, last12)
.
Notes
This algorithm performs a similar task as ranges::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, ranges::merge
would output all n + m
occurrences whereas ranges::set_union
would output max(n, m)
ones only. So ranges::merge
outputs exactly N
values and ranges::set_union
may produce fewer.
Possible implementation
|
Example
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
void print(const auto& in1, const auto& in2, auto first, auto last)
{
std::cout << "{ ";
for (const auto& e : in1)
std::cout << e << ' ';
std::cout << "} +\n{ ";
for (const auto& e : in2)
std::cout << e << ' ';
std::cout << "} =\n{ ";
while (!(first == last))
std::cout << *first++ << ' ';
std::cout << "}\n\n";
}
int main()
{
std::vector<int> in1, in2, out;
in1 = {1, 2, 3, 4, 5};
in2 = {3, 4, 5, 6, 7};
out.resize(in1.size() + in2.size());
const auto ret = std::ranges::merge(in1, in2, out.begin());
print(in1, in2, out.begin(), ret.out);
in1 = {1, 2, 3, 4, 5, 5, 5};
in2 = {3, 4, 5, 6, 7};
out.clear();
out.reserve(in1.size() + in2.size());
std::ranges::merge(in1, in2, std::back_inserter(out));
print(in1, in2, out.cbegin(), out.cend());
}
Output:
{ 1 2 3 4 5 } +
{ 3 4 5 6 7 } =
{ 1 2 3 3 4 4 5 5 6 7 }
{ 1 2 3 4 5 5 5 } +
{ 3 4 5 6 7 } =
{ 1 2 3 3 4 4 5 5 5 5 6 7 }
See also
(C++20)
|
merges two ordered ranges in-place (niebloid) |
(C++20)
|
checks whether a range is sorted into ascending order (niebloid) |
(C++20)
|
computes the union of two sets (niebloid) |
(C++20)
|
sorts a range into ascending order (niebloid) |
(C++20)
|
sorts a range of elements while preserving order between equal elements (niebloid) |
merges two sorted ranges (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/algorithm/ranges/merge