1. 程式人生 > >C++比較器的實現_函式

C++比較器的實現_函式

#include <iostream>
#include <algorithm>
using namespace std;

struct student{
    int ID;
    int Age;
    int Score;
};
student stu [3] = {
    {1, 18, 88},
    {2, 19, 90},
    {3, 20, 70}
};
bool comparator(const student &a, const student &b)
{
    return (a.Score < b.Score);
} int main() { sort(stu, stu + 3, comparator); for(int i = 0; i < 3; i++) cout << stu[i].ID << ' ' << stu[i].Age << ' ' << stu[i].Score << endl; system("pause"); return 0; }