C++ 列表庫 - merge() 函式



描述

C++ 函式std::list::merge() 將兩個已排序的列表合併成一個。這些列表應按升序排序。

宣告

以下是來自 std::list 標頭檔案的 std::list::merge() 函式宣告。

C++98

void merge (list& x);

C++11

void merge (list& x);

引數

x − 另一個相同型別的列表物件。

返回值

無。

異常

此成員函式從不丟擲異常。

時間複雜度

線性,即 O(n)

示例

以下示例演示了 std::list::merge() 函式的用法。

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l1 = {1, 5, 11, 31};
   list<int> l2 = {10, 20, 30};

   l2.merge(l1);

   cout << "List contains following elements after merge operation" << endl;

   for (auto it = l2.begin(); it != l2.end(); ++it)
      cout << *it << endl;

   return 0;
}

讓我們編譯並執行上面的程式,這將產生以下結果:

List contains following elements after merge operation
1
5
10
11
20
30
31
list.htm
廣告
© . All rights reserved.