STL之for_each, find_if,adjacent_find,count_if的使用

// STL_Algorithm.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<hash_map>
#include<algorithm>
using namespace std;
struct stuInfo
{
	int num;
	char name[30];
};
struct stuEqual
{
	const static size_t bucket_size=56;
	//根据学号决定是不是可以进行插入操作,如果不同则可以进行插入操作
	bool operator()(const int a,const int b) const
	{
		return a!=b;
	}
	size_t operator()(const int a)const
	{
		return a;
	}
};
struct strForEach
{
	void operator()(const pair<int,stuInfo> a)const
	{
		cout<<"该生学号是:"<<a.second.num<<endl;
		cout<<"该生姓名是:"<<a.second.name<<endl;
	}
};
class claFindIf
{
private:
	int num;
public:
	claFindIf(int a):num(a)
	{
		;
	}
	bool operator()(const pair<int,stuInfo> a)const
	{
		if(a.second.num==num)
		{
			return true;
		}
		return false;
	}
};
class claAdjacentFind
{

public:
	claAdjacentFind()
	{
		;
	}
	bool operator()(const pair<int,stuInfo>a,const pair<int,stuInfo>b)const
	{
		if(a.second.num>b.second.num)
		{
			if((a.second.num-b.second.num)<3)
				return true;
			else
			{
				return false;
			}
		}
		else
		{
			if((b.second.num-a.second.num)<3)
				return true;
			else
			{
				return false;
			}
		}
	}
};
class claCountIf
{
private:
	char name[30];
public:
	claCountIf(char* a)
	{
		strcpy(name,a);
	}
	bool operator()(pair<int,stuInfo>a)
	{
		if(strcmp(a.second.name,name)==0)
		{
			return true;
		}
		else
		{
			return false;
		}		
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	hash_map<int,stuInfo,stuEqual> stuList;	
	stuInfo a={100,"周建业"};
	stuList[100]=a;
	a.num=101;
	strcpy(a.name,"申玲艳");
	stuList[101]=a;
	a.num=129;
	strcpy(a.name,"周建业");
	stuList[129]=a;
	stuList[129]=a;
	//开始遍历hash_map中的所有内容
	cout<<"开始遍历所有学生信息\n";
	for_each(stuList.begin(),stuList.end(),strForEach());
	//我们开始使用find操作,查找
	cout<<"开始查找学号是100的信息\n";//这样查找到的是符合条件的最后一个元素
	hash_map<int,stuInfo,stuEqual>::iterator itr=find_if(stuList.begin(),stuList.end(),claFindIf(100));
	if(itr!=stuList.end())
	{
		cout<<"我们找到了该生信息\n";
		cout<<"该生姓名是:"<<(*itr).second.name<<endl;
		cout<<"该生学号是:"<<(*itr).second.num<<endl;
	}	
	//这样查找的是符合条件的相邻元素的首要元素的迭代器
	itr=adjacent_find(stuList.begin(),stuList.end(),claAdjacentFind());
	if(itr!=stuList.end())
	{
		cout<<"我们找到了学号相邻差在3以内的学生信息对\n";
		cout<<"该生姓名是:"<<(*itr).second.name<<endl;
		cout<<"该生学号是:"<<(*itr).second.num<<endl;
		cout<<"该生姓名是:"<<(*(itr++)).second.name<<endl;
		cout<<"该生学号是:"<<(*(itr++)).second.num<<endl;
	}	
	//使用count_if函数进行查找
	int nCount=count_if(stuList.begin(),stuList.end(),claCountIf("周建业"));
	cout<<"符合条件的数量是:"<<nCount<<endl;
	return 0;
}
现在想来,迭代器,也就是指针的泛化类型,对于自定义的查找或者比较,等函数,主要是针对迭代器指向的泛化类型,也就是指针的*(取内容)的操作。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

世纪殇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值