日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

      歡迎訪問 生活随笔!

      生活随笔

      當前位置: 首頁 > 编程语言 > c/c++ >内容正文

      c/c++

      c++ list sort

      發布時間:2025/3/8 c/c++ 33 豆豆
      生活随笔 收集整理的這篇文章主要介紹了 c++ list sort 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
      ??1.? bool operator < (S & b) {return ID < b.ID;}

      struct S {std::string firstname;std::string secondname;int ID;bool operator < (S & b) {return ID < b.ID;}// 重新定義小于,因為默認的sort函數調用的操作符是<,所以我們只需要重載 < 就好了 };int main(int argc, char* argv[]) {std::list<S> mylist;std::list<S>::iterator iter;S a;a.firstname ="dfadf";a.ID = 5;mylist.push_back (a);a.firstname ="得到";a.ID = 9;mylist.push_back (a);a.firstname ="xxx";a.ID = 7;mylist.push_back (a);a.firstname ="gggg";a.ID = 25;mylist.push_back (a);mylist.sort();

      2.

      #include "stdafx.h" #include <iostream> #include <list> using namespace std;class A{ public:A(int m):a(m){}bool operator <(const A& other){<span style="font-size: 18px;"> return a < other.a;</span>}friend std::ostream& operator <<(std::ostream& out, const A& obj){out<< obj.a<<endl;return out;} private:int a; };int _tmain(int argc, _TCHAR* argv[]) { list<A> S;typedef list<A>::iterator it;S.push_back(A(3));S.push_back(A(2));S.push_back(A(5));S.sort();it it1 = S.begin();it it2 = S.end();while(it1 != it2){cout<<*it1<<endl;it1++;}return 0; }

      3.

      33 // list_sort.cpp 34 // compile with: /EHsc 35 #include <list> 36 #include <iostream> 37 38 int main( ) 39 { 40 using namespace std; 41 list <int> c1; 42 list <int>::iterator c1_Iter; 43 44 c1.push_back( 20 ); 45 c1.push_back( 10 ); 46 c1.push_back( 30 ); 47 48 cout << "Before sorting: c1 ="; 49 for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ ) 50 cout << " " << *c1_Iter; 51 cout << endl; 52 53 c1.sort( ); 54 cout << "After sorting c1 ="; 55 for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ ) 56 cout << " " << *c1_Iter; 57 cout << endl; 58 59 c1.sort( greater<int>( ) ); 60 cout << "After sorting with 'greater than' operation, c1 ="; 61 for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ ) 62 cout << " " << *c1_Iter; 63 cout << endl; 64 }

      ?

      總結

      以上是生活随笔為你收集整理的c++ list sort的全部內容,希望文章能夠幫你解決所遇到的問題。

      如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。