sort没用对吗?为啥后面不排了?

学术版

```cpp #include<bits/stdc++.h> using namespace std; int n,i,s,t; float b[45],c[45],d[45]; string a[45]; bool compare(int a,int b){ return a > b; } int main(){ cin >> n; for(i = 1; i <= n; i++){ cin >> a[i] >> b[i]; } for(i = 1; i <= n; i++){ if(a[i] == "male"){ s++; c[s] = b[i]; } else{ t++; d[t] = b[i]; } } sort(c + 1,c + s + 1); sort(d + 1,d + t + 1,compare); for(i = 1; i <= s; i++) cout<< fixed << setprecision(2) << c[i] << " " ; for(i = 1; i <= t; i++) cout<< fixed << setprecision(2) << d[i] << " " ; return 0; } ```
by ImposterAnYu @ 2021-05-07 09:51:31


首先,数据类型不对真的会排序吗?
by EDqwq @ 2021-05-07 09:53:48


`std::greater` 不好吗?
by ud2_ @ 2021-05-07 09:58:08


@[ImopsterAnYu](/user/510555) 你的compare是给int比较,但你的d是float
by WYXkk @ 2021-05-07 09:58:11


@[EDqwq](/user/294562) 会的,只不过比较的时候强制转换了类型
by SSerxhs @ 2021-05-07 10:02:39


@[SSerxhs](/user/29826) az 是我孤陋寡闻了
by EDqwq @ 2021-05-07 10:04:41


@[WYXkk](/user/130151) 哦,是要float a和float b吧?
by ImposterAnYu @ 2021-05-07 11:13:09


```cpp #include<bits/stdc++.h> using namespace std; int n,i,s,t; float b[45],c[45],d[45]; string a[45]; bool compare(float a,float b){ return a > b; } int main(){ cin >> n; for(i = 1; i <= n; i++){ cin >> a[i] >> b[i]; } for(i = 1; i <= n; i++){ if(a[i] == "male"){ s++; c[s] = b[i]; } else{ t++; d[t] = b[i]; } } sort(c + 1,c + s + 1); sort(d + 1,d + t + 1,compare); for(i = 1; i <= s; i++) cout<< fixed << setprecision(2) << c[i] << " " ; for(i = 1; i <= t; i++) cout<< fixed << setprecision(2) << d[i] << " " ; return 0; } ``` 改完没问题了,谢谢大家的帮助!
by ImposterAnYu @ 2021-05-07 11:14:58


|