C++ Vector 库 - vector() 函数
描述
C++ 填充构造函数 std::vector::vector() 构造一个大小为 n 的容器并将值 val(如果提供)分配给容器的每个元素 .
声明
以下是填充构造函数 std::vector::vector() 的声明形式 std::vector 头。
C++98
explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());
C++11
vector (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
explicit vector (size_type n);
参数
n − 容器大小。
val − 分配给容器的每个元素的值。
返回值
构造函数从不返回值。
异常
此成员函数从不抛出异常。
时间复杂度
线性,即 O(n)
示例
下面的例子展示了填充构造函数 std::vector::vector() 的用法。
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
vector<int> v(5, 200);
for (int i = 0; i < v.size(); ++i)
cout << v[i] << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
200 200 200 200 200

