容器vector的常用接口函数

vector<double> v1;
vector<double>::iterator iter;
v1.push_back(10.4);
v1.push_back(20.2);
v1.push_back(30.7);
for(iter = v1.begin(); iter != v1.end(); iter++)
cout<<*iter<<' '; //输出10.4 20.2 30.7
cout<<endl;


double a = v1.at(0); //也可写成:    double a = v1[0];      即像数组一样访问
double b = v1.at(1);
double c = v1.at(2);
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;


int size = 0;
size = v1.size();
cout<<"size = "<<size<<endl; //输出size = 3
v1.clear();
size = v1.size();
cout<<"size = "<<size<<endl; //输出size = 0

你可能感兴趣的:(容器vector的常用接口函数)