vector自定義比較函數


這里就結構體類型的vector數組以及結構體類型元素比較來自定義cmp比較函數;

 1 #include <algorithm>
 2 ...
 3 struct Node{
 4  int id;
 5  string name;
 6  int age;
 7 }node[100];
 8 bool cmp(Node a,Node b){
 9      if(a.age!=b.age) return a.age<b.age;
10      if(a.id!=b.id) return a.id<b.id;
11      return strcmp(a.chr,b.chr)<0;
12 //    if(a.age<b.age) return true;
13 //    else if(a.age==b.age)
14 //    {
15 //        if(a.id<b.id) return true;
16 //        else if(a.id==b.id)
17 //        {
18 //            if(a.chr<b.chr) return true;
19 //        }
20 //    }
21 //    return false;
22 }
23 vector<Node> vec;
24 ...
25 sort(vec.begin(),vec.end(),cmp);
26 ...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM