C++ forward_list 庫 - merge() 函式



描述

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

宣告

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

C++11

void merge (forward_list& x);

引數

x - 另一個相同型別的 forward_list 物件。

返回值

異常

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

時間複雜度

線性,即 O(n)

示例

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {
   forward_list<int> fl1 = {1, 5, 11, 31};
   forward_list<int> fl2 = {10, 20, 30};

   fl1.merge(fl2);

   cout << "List contains following elements" << endl;

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

   return 0;
}

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

List contains following elements
1
5
10
11
20
30
31
forward_list.htm
廣告
© . All rights reserved.