std::ranges::merge, std::ranges::merge_result
定义于头文件 <algorithm> |
||
调用签名 (Call signature) |
||
template< std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, |
(1) | (C++20 起) |
template< ranges::input_range R1, ranges::input_range R2, std::weakly_incrementable O, class Comp = ranges::less, |
(2) | (C++20 起) |
辅助类型 |
||
template< class I1, class I2, class O > using merge_result = ranges::in_in_out_result<I1, I2, O>; |
(3) | (C++20 起) |
将两个*已排序*范围`[``[first1`, `last1`)` 和 `[``first2`, `last2`)` 合并到一个从 result 开始的*已排序*范围中。
如果对于指向序列的任何迭代器 `it` 和任何非负整数 `n`,使得 `it + n` 是指向序列元素的有效迭代器,std::invoke(comp, std::invoke(proj2, *(it + n)), std::invoke(proj1, *it))) 评估为 false,则称序列相对于比较器 comp 是*已排序*的。
如果目标范围与任一输入范围重叠,则行为未定义(输入范围可以相互重叠)。
此合并函数是*稳定的*,这意味着对于原始两个范围中的等效元素,第一个范围中的元素(保留其原始顺序)优先于第二个范围中的元素(保留其原始顺序)。
本页描述的类函数实体是 算法函数对象(非正式地称为 niebloids),即
目录 |
[编辑] 参数
first1, last1 | - | 定义要合并的第一个输入已排序范围元素的迭代器-哨兵对 |
first2, last2 | - | 定义要合并的第二个输入已排序范围元素的迭代器-哨兵对 |
result | - | 输出范围的起始 |
comp | - | 应用于投影元素的比较 |
proj1 | - | 应用于第一个范围元素的投影。 |
proj2 | - | 应用于第二个范围元素的投影。 |
[编辑] 返回值
{last1, last2, result_last},其中 result_last 是构造范围的末尾。
[编辑] 复杂度
至多 N − 1 次比较和每个投影的应用,其中 N = ranges::distance(first1, last1) + ranges::distance(first2, last12)。
[编辑] 注意
此算法执行的任务类似于 ranges::set_union。两者都消耗两个已排序的输入范围,并生成一个包含两个输入元素的已排序输出。这两个算法之间的区别在于处理两个输入范围中比较等效的值(参见 LessThanComparable 的注意事项)。如果任何等效值在第一个范围中出现 n 次,在第二个范围中出现 m 次,则 ranges::merge 将输出所有 n + m 次出现,而 ranges::set_union 将只输出 max(n, m) 次。因此 ranges::merge 精确输出 N 个值,而 ranges::set_union 可能会产生更少的值。
[编辑] 可能的实现
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 {}; |
[编辑] 示例
#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 }
[编辑] 另请参阅
(C++20) |
就地归并两个有序范围 (算法函数对象) |
(C++20) |
检查一个范围是否按升序排序 (算法函数对象) |
(C++20) |
计算两个集合的并集 (算法函数对象) |
(C++20) |
将一个范围按升序排序 (算法函数对象) |
(C++20) |
对一个范围的元素进行排序,同时保留相等元素之间的顺序 (算法函数对象) |
归并两个已排序的范围 (函数模板) |