向量容器有几种类型的构造函数。默认构造函数用来创建一个空序列。语句vector<T> vector1(n)将创建一个含有n个元素的序列;vector<T> vector1(n,value);将创建一个以n个value的拷贝初始化的序列;下面的是最简单的STL向量构造函数:
#include <stdafx.h> #include <iostream> #include <cassert> #include <vector> using namespace std; int main() { cout<<"Demonstrating simplest vector constructors"<<endl; vector<char> vector1,vector2(3,'x'); assert(vector1.size()==0); assert(vector2.size()==3); assert(vector2[0]=='x' && vector2[1]=='x' && vector2[2]=='x'); assert(vector2==vector<char>(3,'x') && vector2!=vector<char>(4,'x')); system("pause"); return 0; }