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



描述

C++ 填充建構函式 std::list::list() 構造一個新的列表,其中包含n個元素,並將零值賦給列表的每個元素。

宣告

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

C++11

explicit list (size_type n);

引數

n - 要插入容器的元素數量。

返回值

建構函式從不返回值。

異常

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

時間複雜度

線性,即 O(n)

示例

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

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l(5);

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

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

   return 0;
}

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

List contains following element
0
0
0
0
0
list.htm
廣告

© . All rights reserved.