初始化
vector<int>ivec1(10,42); //内置方法,初始化的内容为10个42
vector<int>ivec2(10);
vector<int>::size_type ix=0;
for(ix;ix<10;++ix)
{
ivec2[ix]=42; //下标操作
}
对于vector第一个元素访问的几种方法
cout << "first element from subscript:" << ivec[0] << endl;
cout << "first element from front(): " << ivec.front() << endl;
cout << "first element from begin(): " << *ivec.begin() << endl;
cout << "first element from at(): " << ivec.at(0) << endl;
赋值
int myarray[5] = {1,3,5,7,9};
vector<int> myvector(myarray , myarray+5);
vector<int>ivec5;
vector<int>::size_type cnt=1;
for(cnt;cnt<=10;++cnt)
{
ivec5.push_back(42); //push_back()添加值为的元素到当前vector末尾
}
|