當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Forward_list forward_list()用法及代碼示例


描述

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

聲明

以下是 std::forward_list::forward_list() 構造函數形式 std::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

相關用法


注:本文由純淨天空篩選整理自 C++ Forward_list Library - forward_list() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。