C++ vector排序实例 Posted on 2019-05-10 | Views: 话不多说,直接上c++代码: 1234567891011121314151617181920212223242526272829303132333435363738394041424344//// Created by chengw on 2019-05-09.//#include <iostream>#include <vector>#include <algorithm>#include <string>using namespace std;struct student { string name; int score;};bool More(const student &a, const student &b){ return a.score > b.score;}bool Less(const student &a, const student &b){ return a.score < b.score;}int main(){ int N,type; cout << "Please input nums and order:(1-increase;0-decrease)" << endl; while (cin >> N >> type){ vector<student> s(N); cout << "name:" << " score:" << endl; for(int i=0; i < N; i++){ cin >> s[i].name >> s[i].score; } cout << "after sort:" << endl; if(type == 0) stable_sort(s.begin(), s.end(), More); //稳定排序 else stable_sort(s.begin(), s.end(), Less); for(int i = 0; i < N; i ++){ cout << s[i].name << " " << s[i].score << endl; } } return 0;} 编译后,执行输出: 123456789101112131415Please input nums and order:(0-increase;1-decrease)5 1name: score:a 98b 78c 99d 100e 67after sort:e 67b 78a 98c 99d 100 您的鼓励是我持之以恒的动力 打赏 WeChat Pay Alipay