#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int main()
{
pair<int, double> p;
vector<pair<int, double> > vp;
p.first = 2;
p.second = 2.4;
vp.push_back(p);
p.first = 9;
p.second = 3.0;
vp.push_back(p);
p.first = 10;
p.second = 3.1;
vp.push_back(p);
p.first = 1;
p.second = 2.4;
vp.push_back(p);
p.first = 5;
p.second = 3.1;
vp.push_back(p);
cout << "before sort:" << endl;
for(auto &p : vp) {
cout << p.first << ", " << p.second << endl;
}
sort(vp.begin(), vp.end()/*,
[](const pair<int, double> &p1, const pair<int, double> &p2){
if (p1.second > p2.second) return true;
if (p1.second < p2.second) return false;
return p1.first < p2.first;
}*/
);
cout << endl;
cout << "after sort:" << endl;
for(auto &p : vp) {
cout << p.first << ", " << p.second << endl;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dXRpbGl0eT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkKewogICAgcGFpcjxpbnQsIGRvdWJsZT4gcDsKICAgIHZlY3RvcjxwYWlyPGludCwgZG91YmxlPiA+IHZwOwoKICAgIHAuZmlyc3QgPSAyOwogICAgcC5zZWNvbmQgPSAyLjQ7CiAgICB2cC5wdXNoX2JhY2socCk7CiAgICAKICAgIHAuZmlyc3QgPSA5OwogICAgcC5zZWNvbmQgPSAzLjA7CiAgICB2cC5wdXNoX2JhY2socCk7CiAgICAKICAgIHAuZmlyc3QgPSAxMDsKICAgIHAuc2Vjb25kID0gMy4xOwogICAgdnAucHVzaF9iYWNrKHApOwogICAgCiAgICBwLmZpcnN0ID0gMTsKICAgIHAuc2Vjb25kID0gMi40OwogICAgdnAucHVzaF9iYWNrKHApOwogICAgCiAgICBwLmZpcnN0ID0gNTsKICAgIHAuc2Vjb25kID0gMy4xOwogICAgdnAucHVzaF9iYWNrKHApOwogICAgCgljb3V0IDw8ICJiZW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