list仿函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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仿函数的主要内容,如果未能解决你的问题,请参考以下文章