• STL SET 类 排序


    STL SET 类 排序 - 缘起宇轩阁 - 博客频道 - CSDN.NET

    STL SET 类 排序


    分类:
    C++语言编程


    41人阅读
    评论(0)
    收藏
    举报
    1. #include <set>  
    2. #include <string>  
    3. #include <iostream>  
    4. using namespace std;  
    5.   
    6. class CEmployee {  
    7. public:  
    8.     CEmployee();  
    9.     ~CEmployee();  
    10.      const string getName() const;        
    11.      void setName(const string name);      
    12.      const string getTitle() const;     
    13.      void setTitle(string title);     
    14.      int getID() const;            
    15.      void setID(int id);  
    16. private:  
    17.     int m_id;  
    18.     string m_username;  
    19.     string m_title;  
    20.       
    21. };  
    22. CEmployee::CEmployee()  
    23. {  
    24.   
    25. }  
    26. CEmployee::~CEmployee()  
    27. {  
    28. }  
    29. const string CEmployee::getName() const  
    30. {  
    31.      return m_username;  
    32. }  
    33. void CEmployee::setName(const string username)  
    34. {  
    35.     m_username = username;  
    36. }  
    37.   
    38. const string CEmployee::getTitle() const  
    39. {  
    40.   return m_title;  
    41. }  
    42.   
    43. void CEmployee::setTitle(string title)  
    44. {  
    45.    m_title = title;  
    46. }  
    47. int CEmployee::getID() const  
    48. {  
    49.    return m_id;  
    50. }  
    51. void CEmployee::setID(int id)  
    52. {  
    53.    m_id = id;  
    54. }  
    55. class sortByID  
    56. {  
    57.    public:  
    58.    bool operator() (CEmployee const &_A, CEmployee const &_B) const  
    59.    {  
    60.     if(_A.getID() < _B.getID()) return true;  
    61.     if(_A.getID() == _B.getID()) return _A.getName().compare(_B.getName()) < 0;  
    62.     return false;  
    63.    }  
    64. };   
    65.   
    66. int main()  
    67. {  
    68.        set<CEmployee, sortByID> empIDSet; // se是雇员的set,  
    69.        set<CEmployee, sortByID> ::iterator  iter;  
    70.        CEmployee employeeInfo;  
    71.        employeeInfo.setName("employee_one");  
    72.        employeeInfo.setTitle("employee");  
    73.        employeeInfo.setID(1);  
    74.        empIDSet.insert(employeeInfo);  
    75.        CEmployee employeeInfo2;  
    76.        employeeInfo2.setName("employee_two");  
    77.        employeeInfo2.setTitle("CFO");  
    78.        employeeInfo2.setID(5);  
    79.        empIDSet.insert(employeeInfo2);  
    80.   
    81.        CEmployee employeeInfo3;  
    82.        employeeInfo3.setName("employee_three");  
    83.        employeeInfo3.setTitle("manager");  
    84.        employeeInfo3.setID(3);  
    85.        empIDSet.insert(employeeInfo3);  
    86.          
    87.         
    88.         for (iter=empIDSet.begin(); iter!=empIDSet.end(); iter++)  
    89.         {  
    90.            cout<<iter->getID()<<" " <<iter->getName()<<" " <<iter->getTitle()<<endl;  
    91.         }  
    92.         return 0;  
    93. }  
  • 相关阅读:
    菜单导航组件
    flask+nginx+uwsgi在服务器搭建项目
    安装Anaconda
    vscode上eslink包失效
    js滚动事件
    打字游戏
    js更高文档的样式
    js事件
    Dom对象更改文档结构.html
    js重点
  • 原文地址:https://www.cnblogs.com/lexus/p/2604827.html
Copyright © 2020-2023  润新知