std::ranges::merge() 算法
- 自 C++20 起
- 简化
- 详细
// (1)
constexpr merge_result<I1, I2, O>
merge( I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
// (2)
constexpr merge_result<ranges::borrowed_iterator_t<R1>,
ranges::borrowed_iterator_t<R2>, O>
merge( R1&& r1, R2&& r2, O result, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
参数类型是泛型的,并具有以下约束
I1
,I2
-std::forward_iterator
S1
,S2
-std::sentinel_for<I1>
,std::sentinel_for<I2>
R1
,R2
-std::ranges::forward_range
O
-std::weakly_incrementable
Comp
- (无)Proj1
,Proj2
- (无)
所有重载的 Proj
和 Comp
模板参数具有以下默认类型:std::identity
, ranges::less
。
此外,每个重载都有以下约束
- (1) -
mergeable<I1, I2, O, Comp, Proj1, Proj2>
- (2) -
mergeable<ranges::iterator_t<R1>, ranges::iterator_t<R2>, O, Comp, Proj1, Proj2>
(为便于阅读,此处省略了 std::
命名空间)
// (1)
template<
std::input_iterator I1,
std::sentinel_for<I1> S1,
std::input_iterator I2
std::sentinel_for<I2> S2
std::weakly_incrementable O
class Comp = ranges::less
class Proj1 = std::identity
class Proj2 = std::identity
>
requires std::mergeable<I1, I2, O, Comp, Proj1, Proj2>
constexpr merge_result<I1, I2, O>
merge( I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
// (2)
template<
ranges::input_range R1
ranges::input_range R2
std::weakly_incrementable O
class Comp = ranges::less
class Proj1 = std::identity
class Proj2 = std::identity
>
requires std::mergeable<ranges::iterator_t<R1>, ranges::iterator_t<R2>,
O, Comp, Proj1, Proj2>
constexpr merge_result<ranges::borrowed_iterator_t<R1>,
ranges::borrowed_iterator_t<R2>, O>
merge( R1&& r1, R2&& r2, O result, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {} );
辅助类型定义如下:
template< class I1, class I2, class O >
using merge_result = ranges::in_in_out_result<I1, I2, O>;
将两个已排序的范围 [first1
; last1
) 和 [first2
; last2
) 合并为一个从 result
开始的已排序范围。
如果对于指向序列的任何迭代器 it
以及任何非负整数 n
(使得 it + n
是指向序列元素有效迭代器),std::invoke(comp, std::invoke(proj2, *(it + n)), std::invoke(proj1, *it)))
评估为 false
,则称序列相对于比较器 comp
已排序。
- (1) 使用给定的二元比较函数
comp
比较元素。 - (2) 与 (1) 相同,但使用
r1
作为第一个范围,r2
作为第二个范围,如同使用ranges::begin(r1)
作为first1
,ranges::end(r1)
作为last1
,ranges::begin(r2)
作为first2
,以及ranges::end(r2)
作为last2
。
这个合并函数是**稳定的**,这意味着对于原始两个范围中等效的元素,来自第一个范围的元素会优先于来自第二个范围的元素,并保持它们的原始顺序。
行为未定义
如果目标范围与任一输入范围重叠(输入范围可以相互重叠)。本页描述的函数类实体是niebloids。
参数
first1 last1 | 要合并的第一个已排序元素范围。 |
r r1 | 要合并的第一个已排序元素范围。 |
first2 last2 | 要合并的第二个已排序元素范围。 |
r2 | 要合并的第二个已排序元素范围。 |
result | 目标范围的开头。 |
proj1 | 应用于第一个范围中元素的投影。 |
proj2 | 应用于第二个范围中元素的投影。 |
返回值
类型为 ranges::merge_result
的值,初始化如下
{
last1,
last2,
result_last
}
其中 result_last
是构造范围的末尾。
复杂度
给定 N
为 ranges::distance(first1, last1) + ranges::distance(first2, last12)
最多 N - 1
次比较和每次投影的应用。
异常
(无)
可能的实现
merge(1) 和 merge(2)
struct merge_fn
{
template<std::input_iterator I1, std::sentinel_for<I1> S1,
std::input_iterator I2, std::sentinel_for<I2> S2,
std::weakly_incrementable O, class Comp = ranges::less,
class Proj1 = std::identity, class Proj2 = std::identity>
requires std::mergeable<I1, I2, O, Comp, Proj1, Proj2>
constexpr ranges::merge_result<I1, I2, O>
operator()(I1 first1, S1 last1, I2 first2, S2 last2, O result, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
for (; !(first1 == last1 or first2 == last2); ++result)
{
if (std::invoke(comp, std::invoke(proj2, *first2), std::invoke(proj1, *first1)))
*result = *first2, ++first2;
else
*result = *first1, ++first1;
}
auto ret1 {ranges::copy(std::move(first1), std::move(last1), std::move(result))};
auto ret2 {ranges::copy(std::move(first2), std::move(last2), std::move(ret1.out))};
return {std::move(ret1.in), std::move(ret2.in), std::move(ret2.out)};
}
template<ranges::input_range R1, ranges::input_range R2, std::weakly_incrementable O,
class Comp = ranges::less,
class Proj1 = std::identity, class Proj2 = std::identity>
requires std::mergeable<ranges::iterator_t<R1>, ranges::iterator_t<R2>,
O, Comp, Proj1, Proj2>
constexpr ranges::merge_result<ranges::borrowed_iterator_t<R1>,
ranges::borrowed_iterator_t<R2>, O>
operator()(R1&& r1, R2&& r2, O result, Comp comp = {},
Proj1 proj1 = {}, Proj2 proj2 = {}) const
{
return (*this)(ranges::begin(r1), ranges::end(r1),
ranges::begin(r2), ranges::end(r2),
std::move(result), std::move(comp),
std::move(proj1), std::move(proj2));
}
};
inline constexpr merge_fn merge {};
备注
此算法执行与ranges::set_union
类似的任务。两者都消耗两个已排序的输入范围并产生一个包含来自两个输入元素的已排序输出。
这两个算法之间的区别在于处理来自两个输入范围的比较等效值(参见LessThanComparable的说明)。
如果任何等效值在第一个范围中出现 n
次,在第二个范围中出现 m
次,则ranges::merge
将输出所有 n + m
次出现,而ranges::set_union
将仅输出 ranges::max(n, m)
次。
因此std::merge
精确输出 std::distance(first1, last1) + std::distance(first2, last2)
个值,而std::set_union
可能产生更少的值。
示例
以下代码使用 ranges::merge
将字符串就地转换为大写,使用 std::toupper
函数,然后将每个字符与其序数值合并。
然后使用带投影的 ranges::merge
将 std::vector<Foo>
的元素合并为字符以填充 std::string
。
#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());
}
{ 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 }