STL SET 类 排序 - 缘起宇轩阁 - 博客频道 - CSDN.NET
- #include <set>
- #include <string>
- #include <iostream>
- using namespace std;
- class CEmployee {
- public:
- CEmployee();
- ~CEmployee();
- const string getName() const;
- void setName(const string name);
- const string getTitle() const;
- void setTitle(string title);
- int getID() const;
- void setID(int id);
- private:
- int m_id;
- string m_username;
- string m_title;
- };
- CEmployee::CEmployee()
- {
- }
- CEmployee::~CEmployee()
- {
- }
- const string CEmployee::getName() const
- {
- return m_username;
- }
- void CEmployee::setName(const string username)
- {
- m_username = username;
- }
- const string CEmployee::getTitle() const
- {
- return m_title;
- }
- void CEmployee::setTitle(string title)
- {
- m_title = title;
- }
- int CEmployee::getID() const
- {
- return m_id;
- }
- void CEmployee::setID(int id)
- {
- m_id = id;
- }
- class sortByID
- {
- public:
- bool operator() (CEmployee const &_A, CEmployee const &_B) const
- {
- if(_A.getID() < _B.getID()) return true;
- if(_A.getID() == _B.getID()) return _A.getName().compare(_B.getName()) < 0;
- return false;
- }
- };
- int main()
- {
- set<CEmployee, sortByID> empIDSet; // se是雇员的set,
- set<CEmployee, sortByID> ::iterator iter;
- CEmployee employeeInfo;
- employeeInfo.setName("employee_one");
- employeeInfo.setTitle("employee");
- employeeInfo.setID(1);
- empIDSet.insert(employeeInfo);
- CEmployee employeeInfo2;
- employeeInfo2.setName("employee_two");
- employeeInfo2.setTitle("CFO");
- employeeInfo2.setID(5);
- empIDSet.insert(employeeInfo2);
- CEmployee employeeInfo3;
- 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;
- }
- return 0;
- }