1.2 简单的STL案例

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;
}

谢谢阅读(〃’ ▽ '〃)如有纰漏欢迎指出,觉得还不错就点个赞吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只子美

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值