首页 > 代码库 > list仿函数

list仿函数

#include<algorithm>
using namespace std;
typedef struct 
{
	char *name;
	int stuid;
	int age;
}Student;
/**************************/
bool cmp(const Student &stua,const Student &stub)
{
	return stua.age>stub.age;

}


class cmpp
{
public:
	bool operator() (const Student &stua,const Student &stub)
	{
		return stua.age < stub.age;
	}

};


int main()
{
	list<Student> studentlist;
	Student Stu;
	Stu.name="che";
	Stu.age=10;
	Stu.stuid=0;
	studentlist.insert(studentlist.begin(),Stu);
	Stu.name="che1";
	Stu.age=21;
	Stu.stuid=1;
	studentlist.insert(studentlist.begin(),Stu);
	Stu.name="che2";
	Stu.age=12;
	Stu.stuid=2;
	studentlist.insert(studentlist.begin(),Stu);

	//studentlist.sort(cmp);
	studentlist.sort(cmpp());
	list <Student>::iterator it;
	for(it =studentlist.begin();it!=studentlist.end();++it)
	{
		cout<<"it->name"<<it->name<<endl;
		cout<<"it->age"<<it->age<<endl;
		cout<<"it->stuid"<<it->stuid<<endl;
		cout<<"================"<<endl;
	}

	return 0;
}

 

list仿函数