标签:else auto include ace student -- bool clu str
对cmp的理解能力
常规题
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student{
int idx;
int num;
string name;
Student(int idx_, int num_, string name_) : idx(idx_), num(num_), name(name_){};
bool cmp(const Student& b) {
if(num < b.num) {
return true;
}else if(num == b.num){
return idx < b.idx;
}else{
return false;
}
}
};
bool cmp(const Student& a, const Student& b) {
if(a.num < b.num) {
return true;
}else if(a.num == b.num){
return a.idx < b.idx;
}else{
return false;
}
}
bool cmpC(const Student& a, const Student& b) {
if(a.num > b.num) {
return true;
}else if(a.num == b.num){
return a.idx < b.idx;
}else{
return false;
}
}
int main () {
int n;
int t;
while(cin >> n) {
cin >> t;
int idx = 0;
int num = 0;
string name;
vector<Student> v;
while(n) {
cin >> name >> num;
idx++;
v.push_back({idx, num, name});
n--;
}
if(t != 0) {
sort(v.begin(), v.end(), cmp);
for(auto it:v){
cout << it.name << " " << it.num << endl;
}
}else{
sort(v.begin(), v.end(), cmpC);
for(auto it:v){
cout << it.name << " " << it.num << endl;
}
}
}
}
标签:else auto include ace student -- bool clu str
原文地址:https://www.cnblogs.com/eat-too-much/p/14944149.html