STL SET 类 排序
分类:
C++语言编程
2011-03-04 19:44
41人阅读
评论(0)
收藏
举报
- #include<set>
- #include<string>
- #include<iostream>
- usingnamespacestd;
- classCEmployee{
- public:
- CEmployee();
- ~CEmployee();
- conststringgetName()const;
- voidsetName(conststringname);
- conststringgetTitle()const;
- voidsetTitle(stringtitle);
- intgetID()const;
- voidsetID(intid);
- private:
- intm_id;
- stringm_username;
- stringm_title;
- };
- CEmployee::CEmployee()
- {
- }
- CEmployee::~CEmployee()
- {
- }
- conststringCEmployee::getName()const
- {
- returnm_username;
- }
- voidCEmployee::setName(conststringusername)
- {
- m_username=username;
- }
- conststringCEmployee::getTitle()const
- {
- returnm_title;
- }
- voidCEmployee::setTitle(stringtitle)
- {
- m_title=title;
- }
- intCEmployee::getID()const
- {
- returnm_id;
- }
- voidCEmployee::setID(intid)
- {
- m_id=id;
- }
- classsortByID
- {
- public:
- booloperator()(CEmployeeconst&_A,CEmployeeconst&_B)const
- {
- if(_A.getID()<_B.getID())returntrue;
- if(_A.getID()==_B.getID())return_A.getName().compare(_B.getName())<0;
- returnfalse;
- }
- };
- intmain()
- {
- set<CEmployee,sortByID>empIDSet;//se是雇员的set,
- set<CEmployee,sortByID>::iteratoriter;
- CEmployeeemployeeInfo;
- employeeInfo.setName("employee_one");
- employeeInfo.setTitle("employee");
- employeeInfo.setID(1);
- empIDSet.insert(employeeInfo);
- CEmployeeemployeeInfo2;
- employeeInfo2.setName("employee_two");
- employeeInfo2.setTitle("CFO");
- employeeInfo2.setID(5);
- empIDSet.insert(employeeInfo2);
- CEmployeeemployeeInfo3;
- employeeInfo3.setName("employee_three");
- employeeInfo3.setTitle("manager");
- employeeInfo3.setID(3);
- empIDSet.insert(employeeInfo3);
- for(iter=empIDSet.begin();iter!=empIDSet.end();iter++)
- {
- cout<<iter->getID()<<""<<iter->getName()<<""<<iter->getTitle()<<endl;
- }
- return0;
- }