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

五 參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章