c/c++中自定义结构体的排序

#include<vector>
#include<algorithm>
#define NUM 10
using namespace std;

//自定义结构体的排序
struct Student {
    int id;
    int age;
};
vector<Student> stus;

bool cmd(Student a, Student b) {
    if (a.age < b.age)
        return true;
    return false;
}
void main() {
    
    for (int i = 0; i < NUM; i++) {
        Student s;
        s.id = i;
        s.age = rand() % 30;
        stus.push_back(s);
    }
    sort(stus.begin(),stus.end(),cmd);
    for (int i = 0; i < NUM; i++) {
        printf("student NO %d,age : %d\n",i,stus[i].age);
    }
    system("pause");
}

发布了4 篇原创文章 · 获赞 0 · 访问量 132

猜你喜欢

转载自blog.csdn.net/zhangkkit/article/details/103919135