C++ 算法 归并元素

一 概述
  • C++ 标准库中提供了很多算法,定义于头文件 < algorithm >。本文主要探究以下用于 归并排序区间元素 的算法:
    • std::merge 归并二个已排序范围 [first1, last1) 和 [first2, last2) 到另一个已排序范围中
二 辅助函数
三 定义
template< class InputIt1, class InputIt2, class OutputIt >
OutputIt merge( InputIt1 first1, InputIt1 last1,
                InputIt2 first2, InputIt2 last2,
                OutputIt d_first );(1)(C++20)
template< class InputIt1, class InputIt2, class OutputIt >
constexpr OutputIt merge( InputIt1 first1, InputIt1 last1,
                          InputIt2 first2, InputIt2 last2,
                          OutputIt d_first );(1)(C++20)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class ForwardIt3 >
ForwardIt3 merge( ExecutionPolicy&& policy,
                  ForwardIt1 first1, ForwardIt1 last1,
                  ForwardIt2 first2, ForwardIt2 last2,
                  ForwardIt3 d_first );(2)(C++17)	
template< class InputIt1, class InputIt2, class OutputIt, class Compare>
OutputIt merge( InputIt1 first1, InputIt1 last1,
                InputIt2 first2, InputIt2 last2,
                OutputIt d_first, Compare comp );(3)(C++20)
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)(C++20)
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 );(4)(C++17)
  • ExecutionPolicy 请参考此前文章 std::find std::execution。
  • 若对于任何指向序列的迭代器 it 与任何使得 it + n 为指向序列元素的合法迭代器的非负整数 n , comp(*(it + n), *it) 求值为 false ,则称序列相对于 comp 已排序。
四 Demo
  • 代码
// std::merge
if (1) {
  std::list<int> l;
  std::vector<int> vc1;
  fill(vc1, 1, 9);
  std::vector<int> vc2;
  fill(vc2, 1, 9);

  print("before merge vc1(src): ", vc1);
  print("before merge vc2(src): ", vc2);
  print("before merge list(dst): ", l);
  std::merge(vc1.begin(), vc1.end(), vc2.begin(), vc2.end(),
             std::back_inserter(l));
  print("after merge vc1(src): ", vc1);
  print("after merge vc2(src): ", vc2);
  print("after merge list(dst): ", l);
}

if (1) {
  std::list<int> l;
  std::vector<int> vc1;
  fill(vc1, 1, 9);
  std::vector<int> vc2;
  fill(vc2, 1, 9);

  print("before merge vc1(src): ", vc1);
  print("before merge vc2(src): ", vc2);
  print("before merge list(dst): ", l);
  // Compare 形式
  std::merge(vc1.begin(), vc1.end(), vc2.begin(), vc2.end(),
             std::back_inserter(l), std::less<int>());
  print("after merge vc1(src): ", vc1);
  print("after merge vc2(src): ", vc2);
  print("after merge list(dst): ", l);
}
  • 结果
before merge vc1(src): 1 2 3 4 5 6 7 8 9
before merge vc2(src): 1 2 3 4 5 6 7 8 9
before merge list(dst):
after merge vc1(src): 1 2 3 4 5 6 7 8 9
after merge vc2(src): 1 2 3 4 5 6 7 8 9
after merge list(dst): 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

before merge vc1(src): 1 2 3 4 5 6 7 8 9
before merge vc2(src): 1 2 3 4 5 6 7 8 9
before merge list(dst):
after merge vc1(src): 1 2 3 4 5 6 7 8 9
after merge vc2(src): 1 2 3 4 5 6 7 8 9
after merge list(dst): 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
五 参考
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值