# include<iostream>
#include<vector>
# include<algorithm>
# include<string>
using namespace std;
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
for(int i =0;i<9;i++)
{
v1.push_back(i);
}
printVector(v1);
//[]
for (int i = 0; i < 9; i++)
{
cout<<v1[i]<<" ";
}
cout << endl;
//利用at方式访问
for (int i = 0; i < 9; i++)
{
cout << v1.at(i)<< " ";
}
cout << endl;
//获取第一个元素
cout << "第一个元素 " << v1.front() << endl;
//获取最后一个元素
cout << "最后一个元素 " << v1.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
C++ 提高教程 STL vector容器-数据存取
最新推荐文章于 2024-08-28 14:35:43 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)