STL SET 類 排序

來源:互聯網
上載者:User

#include <set><br />#include <string><br />#include <iostream><br />using namespace std;</p><p>class CEmployee {<br />public:<br /> CEmployee();<br /> ~CEmployee();<br /> const string getName() const;<br /> void setName(const string name);<br /> const string getTitle() const;<br /> void setTitle(string title);<br /> int getID() const;<br /> void setID(int id);<br />private:<br /> int m_id;<br /> string m_username;<br /> string m_title;</p><p>};<br />CEmployee::CEmployee()<br />{</p><p>}<br />CEmployee::~CEmployee()<br />{<br />}<br />const string CEmployee::getName() const<br />{<br /> return m_username;<br />}<br />void CEmployee::setName(const string username)<br />{<br /> m_username = username;<br />}</p><p>const string CEmployee::getTitle() const<br />{<br /> return m_title;<br />}</p><p>void CEmployee::setTitle(string title)<br />{<br /> m_title = title;<br />}<br />int CEmployee::getID() const<br />{<br /> return m_id;<br />}<br />void CEmployee::setID(int id)<br />{<br /> m_id = id;<br />}<br />class sortByID<br />{<br /> public:<br /> bool operator() (CEmployee const &_A, CEmployee const &_B) const<br /> {<br /> if(_A.getID() < _B.getID()) return true;<br /> if(_A.getID() == _B.getID()) return _A.getName().compare(_B.getName()) < 0;<br /> return false;<br /> }<br />}; </p><p>int main()<br />{<br /> set<CEmployee, sortByID> empIDSet; // se是僱員的set,<br /> set<CEmployee, sortByID> ::iterator iter;<br /> CEmployee employeeInfo;<br /> employeeInfo.setName("employee_one");<br /> employeeInfo.setTitle("employee");<br /> employeeInfo.setID(1);<br /> empIDSet.insert(employeeInfo);<br /> CEmployee employeeInfo2;<br /> employeeInfo2.setName("employee_two");<br /> employeeInfo2.setTitle("CFO");<br /> employeeInfo2.setID(5);<br /> empIDSet.insert(employeeInfo2);</p><p> CEmployee employeeInfo3;<br /> employeeInfo3.setName("employee_three");<br /> employeeInfo3.setTitle("manager");<br /> employeeInfo3.setID(3);<br /> empIDSet.insert(employeeInfo3);</p><p> for (iter=empIDSet.begin(); iter!=empIDSet.end(); iter++)<br /> {<br /> cout<<iter->getID()<<" " <<iter->getName()<<" " <<iter->getTitle()<<endl;<br /> }<br /> return 0;<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.