C++ List 库 - list() 函数
描述
C++ 填充构造函数std::list::list() 用 n 个元素构造一个新列表,并为列表的每个元素分配零值。
声明
以下是 std::list::list() 构造函数形式 std::list 头的声明。
C++11
explicit list (size_type n);
参数
n − 要插入容器的元素数。
返回值
Constructor never returns value.异常
此成员函数从不抛出异常。
时间复杂度
线性,即 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

