|
发表于 2022-10-27 14:53:59
|
显示全部楼层
那我就练习练习C++了
- sh-5.1$ cat main.cpp
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <numeric>
- using std::vector, std::pair;
- using std::string;
- using std::sort, std::accumulate;
- using std::cout, std::endl;
- using std::ostream;
- ostream &operator<<(ostream &os, const vector<double> &vd) {
- bool flag = false;
- for(const auto &i: vd) {
- if(flag) os << ' ';
- flag = true;
- os << i;
- }
- return os;
- }
- ostream &operator<<(ostream &os, const vector<pair<string, vector<double>>> &vp) {
- bool flag = false;
- for(const auto &i: vp) {
- if(flag) os << endl;
- flag = true;
- os << i.first << ": " << i.second;
- }
- return os;
- }
- int main() {
- vector<pair<string, vector<double>>> vp = {
- {"张飞", {78, 75}},
- {"李大刀", {92, 67}},
- {"李墨白", {84, 88}},
- {"王老虎", {50, 50}},
- {"雷小米", {99, 98}}
- };
- sort(vp.begin(), vp.end(), [](const pair<string, vector<double>> &a, const pair<string, vector<double>> &b) -> bool {
- return accumulate(a.second.begin(), a.second.end(), 0) > accumulate(b.second.begin(), b.second.end(), 0);
- });
- cout << vp << endl;
- return 0;
- }
- sh-5.1$ g++ -g -Wall -o main main.cpp
- sh-5.1$ ./main
- 雷小米: 99 98
- 李墨白: 84 88
- 李大刀: 92 67
- 张飞: 78 75
- 王老虎: 50 50
- sh-5.1$
复制代码 |
|