1、容器算法迭代器分离案例
案例:统计某个元素在数组里出现的次数
简易版(帮助理解,无STL)
#include<iostream>
using namespace std;
//算法 负责统计某个元素出现多少次
int mycount(int* start,int * end,int val)
{
int num = 0;
while (start != end)
{
if (*start == val)
num++;
start++;
}
return num;
}
int main()
{
//数组 容器
int arr[] = { 1,2,4,5,3,8,9,1,4,4 };
int* pBegin = arr;//指针 迭代器
int* pEnd = &arr[(sizeof(arr) / sizeof(arr[0]))];
int num = mycount(pBegin, pEnd, 4);
cout << num << endl;
return 0;
}
进阶版(利用STL 算法容器迭代器)
分为text01函数(普通数据),text02函数(类类型数据)两个版本
遍历方法也分别提供两种
#include<iostream>
#include<vector>//动态数组 容器vector
#include<algorithm>//算法
using namespace std;
//回调函数
void myprint(int v)
{
cout << v << endl;
}
//stl基本语法
void text01()
{
//定义容器
vector<int> v;
v.push_back(10);
v.push_back(34);
v.push_back(40);
v.push_back(40);
//通过STL提供的for_each算法
//容器提供迭代器
//vector<int>::iterator迭代器类型
vector<int>::iterator pBegin = v.begin();
vector<int>::iterator pEnd = v.end();
//容器中可能存在基础的数据类型,也可以放自定义的数据类型
//for_each(start,end,回调函数)
//算法
for_each(pBegin, pEnd, myprint);
}
class Person
{
public:
int age;
int id;
Person(int age,int id):age(age),id(id)
{
}
};
void text02()
{
//创建容器,类型为person
vector<Person> v;
Person p1(30, 1), p2(23, 2), p3(54, 3), p4(30,4);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//自己写算法遍历
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << (*it).age << " " << (*it).id << endl;
}
}
int main()
{
text01();//基本数据类型
text02();//类
return 0;
}
2、专栏回顾:第一章到第二章2.2
1.迭代器可以理解为指针,对指针的操作基本都可以对迭代器操作。
实际上迭代器是一个类,这个类封装一个指针。
2.容器分为
序列式容器(由进入的时机和地点决定)
关联式容器(容器本身有规则,进入容器要遵守规则)
3.使用迭代器
//vector<int>::iterator迭代器类型
vector<int>::iterator pBegin = v.begin();
//指向第一个元素的迭代器
vector<int>::iterator pEnd = v.end();
//v.end最后元素的下一个位置
4.for_each算法调用迭代器
//回调函数
void myprint(int v)
{
cout << v << endl;
}
//for_each(start,end,回调函数)
for_each(pBegin, pEnd, myprint);
5.深入理解
任务:尝试用容器存放person类型指针,并且打印for_each
不理解C++类对象和类指针的区别的同学可以看一下这篇文章
C++类对象和类指针的区别
这里text02函数作为比较方便理解
text03函数为本题题解
#include<iostream>
#include<vector>//动态数组 容器vector
#include<algorithm>//算法
using namespace std;
//回调函数
void myprint(int v)
{
cout << v << endl;
}
class Person
{
public:
int age;
int id;
Person(int age, int id) :age(age), id(id)
{
}
};
void text02()
{
//创建容器,类型为person
vector<Person> v;
Person p1(30, 1), p2(23, 2), p3(54, 3), p4(30, 4);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//自己写算法遍历
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << (*it).age << " " << (*it).id << endl;
}
}
void myprint2(Person* v)
{
cout << (*v).id<<" "<<(*v).age<<endl;
}
void text03()
{
//创建容器,类型为person*
vector<Person*> v;
Person* p1 = new Person(20,3);
Person* p2 = new Person(24, 4);
Person* p3 = new Person(50, 5);
Person* p4 = new Person(70, 6);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//使用迭代器
vector<Person*>::iterator start = v.begin();
vector<Person*>::iterator end = v.end();
//算法
for_each(start, end, myprint2);
}
int main()
{
text02();//类(对比用)
text03();//容器存放person类型指针,并且打印for_each
return 0;
}
谢谢阅读(〃’ ▽ '〃)如有纰漏欢迎指出,觉得还不错就点个赞吧。