C++ Forward_list 庫 - forward_list() 函式



描述

C++ 建構函式std::forward_list::forward_list()構造一個列表,其中包含現有列表中每個元素的副本。

宣告

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

C++11

forward_list (const forward_list& other);
forward_list (const forward_list& other, const allocator_type& alloc);

引數

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

  • alloc - 分配器物件。

返回值

建構函式從不返回值。

異常

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

時間複雜度

線性,即 O(n)

示例

以下示例演示了 std::forward_list::forward_list() 建構函式的使用。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl1 = {1, 2, 3, 4, 5};
   forward_list<int> fl2(fl1);

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

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

   return 0;
}

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

List contains following elements
1
2
3
4
5
forward_list.htm
廣告

© . All rights reserved.