发布时间:2022-07-05 文章分类:编程知识 投稿人:王小丽 字号: 默认 | | 超大 打印

STL SET 类 排序


分类:
C++语言编程


41人阅读
评论(0)
收藏
举报
[c-sharp] view plaincopyprint?

  1. #include<set>
  2. #include<string>
  3. #include<iostream>
  4. usingnamespacestd;
  5. classCEmployee{
  6. public:
  7. CEmployee();
  8. ~CEmployee();
  9. conststringgetName()const;
  10. voidsetName(conststringname);
  11. conststringgetTitle()const;
  12. voidsetTitle(stringtitle);
  13. intgetID()const;
  14. voidsetID(intid);
  15. private:
  16. intm_id;
  17. stringm_username;
  18. stringm_title;
  19. };
  20. CEmployee::CEmployee()
  21. {
  22. }
  23. CEmployee::~CEmployee()
  24. {
  25. }
  26. conststringCEmployee::getName()const
  27. {
  28. returnm_username;
  29. }
  30. voidCEmployee::setName(conststringusername)
  31. {
  32. m_username=username;
  33. }
  34. conststringCEmployee::getTitle()const
  35. {
  36. returnm_title;
  37. }
  38. voidCEmployee::setTitle(stringtitle)
  39. {
  40. m_title=title;
  41. }
  42. intCEmployee::getID()const
  43. {
  44. returnm_id;
  45. }
  46. voidCEmployee::setID(intid)
  47. {
  48. m_id=id;
  49. }
  50. classsortByID
  51. {
  52. public:
  53. booloperator()(CEmployeeconst&_A,CEmployeeconst&_B)const
  54. {
  55. if(_A.getID()<_B.getID())returntrue;
  56. if(_A.getID()==_B.getID())return_A.getName().compare(_B.getName())<0;
  57. returnfalse;
  58. }
  59. };
  60. intmain()
  61. {
  62. set<CEmployee,sortByID>empIDSet;//se是雇员的set,
  63. set<CEmployee,sortByID>::iteratoriter;
  64. CEmployeeemployeeInfo;
  65. employeeInfo.setName("employee_one");
  66. employeeInfo.setTitle("employee");
  67. employeeInfo.setID(1);
  68. empIDSet.insert(employeeInfo);
  69. CEmployeeemployeeInfo2;
  70. employeeInfo2.setName("employee_two");
  71. employeeInfo2.setTitle("CFO");
  72. employeeInfo2.setID(5);
  73. empIDSet.insert(employeeInfo2);
  74. CEmployeeemployeeInfo3;
  75. employeeInfo3.setName("employee_three");
  76. employeeInfo3.setTitle("manager");
  77. employeeInfo3.setID(3);
  78. empIDSet.insert(employeeInfo3);
  79. for(iter=empIDSet.begin();iter!=empIDSet.end();iter++)
  80. {
  81. cout<<iter->getID()<<""<<iter->getName()<<""<<iter->getTitle()<<endl;
  82. }
  83. return0;
  84. }