使用algorithm头文件中的find()函数查找自定义类型的数据

// 使用find()函数查找自定义类型中的数据.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
class Student
{
private:
	int age;
	string name;
public:
	Student(string name, int age) :name(name), age(age) {}
	friend class PrintStu;		//将PrintStu类作为Student的友元类
	friend class FindStu;
	string GetName() { return name; }
	int GetAge() { return age; }
};
class PrintStu			//打印信息的仿函数
{
public:
	void operator()(Student& stu)
	{
		cout<< stu.name <<"\t"<< stu.age << endl;
	}
};
class FindStu		//查找操作的仿函数,很有技巧性
{
private:
	string name;
	int age;
public:
	FindStu(const string &name, const int age)
	{
		this->name = name;
		FindStu::age = age;
	}
	bool operator()(vector<Student>::value_type &stu)	//或者这样写  bool operator()(Studetn &stu)也是没问题的
	{
		return stu.name == this->name&&stu.age == this->age;
	}
};
bool Sort_age_less(vector<Student>::value_type &stu1,Student &stu2)
{
	return stu1.GetAge() < stu2.GetAge();
}

//map的操作
void PrintMap(pair<string, int> myPair)		//输出map数据
{
	cout << myPair.first << "\t" << myPair.second << endl;
}
class FindMap				//查找操作用到的仿函数
{
private:
	string name;
	int age;
public:
	FindMap(const string & name, const int age)
	{
		this->name = name;
		this->age = age;
	}
	bool operator()(pair<string, int> myPair)
	{
		if (myPair.first == this->name&&myPair.second == this->age)
		{
			return true;
		}
		return false;
	}
};
int main()
{
	//使用自定义类型
	vector<Student> myStuVect;
	Student Hong("Hong", 12);
	Student Ming("Ming", 23);
	Student Sun("Sun", 21);
	myStuVect.push_back(Hong);
	myStuVect.push_back(Ming);
	myStuVect.push_back(Sun);
	cout << endl << " 使用仿函数输出容器中的信息" << endl;
	for_each(myStuVect.begin(), myStuVect.end(), PrintStu());

	cout <<endl<< "查找结果" << endl;
	//使用find_if()函数调用仿函数进行查找数据
	vector<Student>::iterator myIter=find_if(myStuVect.begin(), myStuVect.end(), FindStu("Hong", 12));
	if (myIter == myStuVect.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
		//注意.运算符运算级别要高于*  ,所以要记得加括号
		cout << (*myIter).GetName() << "\t" << (*myIter).GetAge() << endl;
	}
	//在查找到的数据之前插入一个新数据:
	cout << endl << "在查找到的数据之前插入一个新数据" << endl;
	Student zhang("zhang", 23);
	myStuVect.insert(myIter, zhang);
	for_each(myStuVect.begin(), myStuVect.end(), PrintStu());
	cout << endl << "按照年龄升序排列" << endl;
	sort(myStuVect.begin(), myStuVect.end(), Sort_age_less);
	for_each(myStuVect.begin(), myStuVect.end(), PrintStu());

	//查找map<>中的数据
	cout << endl << "map<>类型的操作" << endl;
	map<string, int>  myMap;
	myMap.insert(pair<string, int>("Guang", 10));
	myMap.insert(pair<string, int>("Chang", 21));
	myMap.insert(pair<string, int>("Kong", 22));
	cout << endl << "输出map<string,int>中的数据" << endl;
	for_each(myMap.begin(), myMap.end(), PrintMap);
	cout << endl << "查找map<string,int>中的某一数据" << endl;
	map<string, int>::iterator myMapIter;
	//调用上面使用的仿函数不行,因为参数类型不一致
	//myMapIter = find_if(myMap.begin(), myMap.end(), FindStu("Guang",10));	//不可以使用FindStu
	myMapIter = find_if(myMap.begin(), myMap.end(), FindMap("Guang", 10));	//使用pair类型的仿函数
	if (myMapIter == myMap.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
		cout << (*myMapIter).first << "\t" << (*myMapIter).second << endl;
		myMap.insert(myMapIter,make_pair("Dragon", 100));
		cout << endl << "再找到的数据之前插入一个数据" << endl;
		for_each(myMap.begin(), myMap.end(), PrintMap);
	}
	system("pause");
    return 0;
}
/*
	find-_if(first,last,函数)  
			使用仿函数查找自定义类型中的数据
	sort(firsr,last,函数)	 
			可以使用仿函数对<小于号进行重载,也可以使用普通的函数
	count_if(firsr,last,bind1st(less<int>(),60));  
			统计  60 < value的个数,是一个仿函数
			使用仿函数类实现统计想要的信息
		函数的返回值都是bool类型

	另外for_each()第三个参数的函数既可以使用一般的函数,也可以使用仿函数不需要有返回值


*/

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值