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.