#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
//vector容器存放内置数据类型
//对应下边的第三种遍历方式所创建的
void myprint(int val)
{
cout << val << endl;
}
void test01()
{
//创建一个vector容器,数组
vector<int> v;
//向容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//通过迭代器访问容器中的数据
vector<int>::iterator itbegin = v.begin(); //起始迭代器 指向容器中第一个元素
vector<int>::iterator itend = v.end(); //结束迭代器 指向容器中最后一个元素的下一个位置
//第一种遍历方式
//while (itbegin != itend)
//{
// cout << *itbegin << endl;
// itbegin++;
//}
//第二种遍历方式
//for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
//{
// cout << *it << endl;
//}
//第三种遍历方式 利用stl提供遍历算法
for_each(v.begin(), v.end(), myprint);
}
int main01()
{
test01();
system("pause");
return 0;
}
vector存储整型数据并使用三种方式进行遍历
最新推荐文章于 2023-04-09 16:41:08 发布