[zz]c++ list sort方法
生活随笔
收集整理的這篇文章主要介紹了
[zz]c++ list sort方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1: #include <list> 2: #include <string> 3: #include <functional> 4: #include <iostream> 5: 6: struct S { 7: std::string firstname; 8: std::string secondname; 9: int ID; 10: // 重新定義小于,因?yàn)槟J(rèn)的sort函數(shù)調(diào)用的操作符是<,所以我們只需要重載 < 就好了 11: bool operator < (S & b) { 12: return ID < b.ID; 13: } 14: ? 15: }; 16: int main(int argc, char* argv[]) 17: { 18: std::list<S> mylist; 19: std::list<S>::iterator iter; 20: S a; 21: a.firstname ="dfadf"; 22: a.ID = 5; 23: mylist.push_back (a); 24: a.firstname ="得到"; 25: a.ID = 9; 26: mylist.push_back (a); 27: a.firstname ="xxx"; 28: a.ID = 7; 29: mylist.push_back (a); 30: a.firstname ="gggg"; 31: a.ID = 25; 32: mylist.push_back (a); 33: mylist.sort(); 34: // 現(xiàn)在默認(rèn)的operator已經(jīng)被我們重載了,就不用管,直接調(diào)用sort就好了 35: for (iter = mylist.begin(); iter != mylist.end();++iter) 36: { 37: std::cout <<static_cast<S>(*iter).ID << "\t"; 38: } 39: std::cout <<std::endl; 40: return 0; 41: }
轉(zhuǎn)載于:https://www.cnblogs.com/zhangzhang/archive/2013/01/25/2877380.html
總結(jié)
以上是生活随笔為你收集整理的[zz]c++ list sort方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Mvvm模式学习
- 下一篇: openvc学习笔记(4)——两种方法在