STL vector按多字段值排序

  1 #include <iostream>
  2 #include <vector>
  3 #include <string>
  4 #include <algorithm>
  5 using namespace std;  
  6 
  7 class Student
  8 {
  9 private:
 10     int id;            // 学号
 11     string name;        // 姓名
 12     float eyesight;        // 视力
 13     float height;        // 身高
 14     float chinese;        // 语文成绩
 15     float english;        // 英文成绩
 16     float math;        // 数学成绩
 17 public:
 18     Student(int id, string name, float eyesight, float height, float chinese, float english, float math)
 19     {
 20         this->id = id;
 21         this->name = name;
 22         this->eyesight = eyesight;
 23         this->height = height;
 24         this->chinese = chinese;
 25         this->english = english;
 26         this->math = math;
 27     }
 28 
 29     int get_id()
 30     {
 31         return id;
 32     }
 33 
 34     string get_name()
 35     {
 36         return name;
 37     }
 38 
 39     float get_eyesight()
 40     {
 41         return eyesight;
 42     }
 43 
 44     float get_height()
 45     {
 46         return height;
 47     }
 48 
 49     float get_chinese()
 50     {
 51         return chinese;
 52     }
 53 
 54     float get_english()
 55     {
 56         return english;
 57     }
 58 
 59     float get_math()
 60     {
 61         return math;
 62     }
 63 };
 64 
 65 // 比较大小的函数(谓词)
 66 bool comparer(Student& stu_a, Student& stu_b)
 67 {
 68     // 按eyesight升序 + height升序排列
 69     if(stu_a.get_eyesight() != stu_b.get_eyesight())    
 70         return (stu_a.get_eyesight() < stu_b.get_eyesight());
 71     else
 72         return (stu_a.get_height() < stu_b.get_height());
 73     
 74     // 按eyesight降序° + height降序排列
 75     //if(stu_a.get_eyesight() != stu_b.get_eyesight())    
 76     //    return (stu_a.get_eyesight() > stu_b.get_eyesight());
 77     //else
 78     //    return (stu_a.get_height() > stu_b.get_height());
 79 
 80     // 按eyesight升序 + height降序排列
 81     //if(stu_a.get_eyesight() != stu_b.get_eyesight())    
 82     //    return (stu_a.get_eyesight() < stu_b.get_eyesight());
 83     //else
 84     //    return (stu_a.get_height() > stu_b.get_height());
 85 
 86     // 按eyesight降序 + height升序排列
 87     //if(stu_a.get_eyesight() != stu_b.get_eyesight())    
 88     //    return (stu_a.get_eyesight() > stu_b.get_eyesight());
 89     //else
 90     //    return (stu_a.get_height() < stu_b.get_height());
 91 }
 92 
 93 int main(int argc, char** argv)
 94 {
 95     vector<Student> vec;
 96     vec.push_back(Student(4, "Dudley", 1.1f, 170.2f, 90.5f, 89.5f, 93.0));
 97     vec.push_back(Student(3, "Chris", 1.1f, 163.4f, 93.5f, 90.0f, 83.5f));
 98     vec.push_back(Student(2, "Bob", 1.5f, 166.6f, 86.0f, 98.5f, 85.0f));
 99     vec.push_back(Student(1, "Andrew", 1.5f, 173.2f, 98.5f, 100.0f, 100.f));
100 
101     // 调用STL中的sort函数,其中的第三个参数就是我们前面定义的,比较两个Student对象大小的函数
102     sort(vec.begin(), vec.end(), comparer);
103 
104     vector<Student>::iterator iter;
105     for(iter = vec.begin(); iter != vec.end(); ++iter)
106     {
107         cout << (*iter).get_eyesight() << "	" << (*iter).get_height() << endl;
108     }
109 
110     return 0;
111 }
原文地址:https://www.cnblogs.com/justkong/p/4118430.html