C++ forward_list 庫 - forward_list() 函式



描述

C++ 建構函式std::forward_list::forward_list()建立一個新的列表,其中包含n個元素,並將val賦值給列表的每個元素。

宣告

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

C++11

explicit forward_list (size_type n, const value_type& val,
                       const allocator_type& alloc = allocator_type());

引數

  • n − 要插入到容器中的元素數量。

  • val − 要分配給容器中每個元素的值。

  • alloc − 分配器物件。

返回值

建構函式不返回值。

異常

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

時間複雜度

線性,即 O(n)

示例

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl(5, 10);

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

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

   return 0;
}

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

List contains following elements
10
10
10
10
10
forward_list.htm
廣告
© . All rights reserved.