一、pair+数组
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, pair<double, string>> ips;
ips ans[N];
int main()
{
int n;
int x;
double y;
string z;
cin >> n;
for(int i = 0; i < n; ++ i){
cin >> x >> y >> z;
ans[i] = {x, {y, z}};
}
sort(ans,ans + n);
for(int i = 0; i < n; ++i){
printf("%d %.2lf %s\n", ans[i].first, ans[i].second.first, ans[i].second.second.c_str());
}
return 0;
}
二、pair+vector
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e5 + 10;
typedef pair<int, pair<double, string>> ips;
vector<ips> ans;
int main()
{
int n;
int x;
double y;
string z;
cin >> n;
for(int i = 0; i < n; ++ i){
cin >> x >> y >> z;
ans.push_back({x, {y, z}});
}
sort(ans.begin(), ans.end());
for(auto i : ans)
printf("%d %.2lf %s\n", i.first, i.second.first, i.second.second.c_str());
return 0;
}
三、map+范围遍历
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N = 1e5 + 10;
typedef pair<double, string> pii;
map<int, pii> ans;
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
int n;
int x;
double y;
string z;
cin >> n;
for(int i = 0; i < n; ++ i){
cin >> x >> y >> z;
ans.insert({x, {y, z}});
}
for(auto i : ans){
printf("%d %.2lf %s\n", i.first, i.second.first, i.second.second.c_str());
}
return 0;
}
四、map+迭代器遍历
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N = 1e5 + 10;
typedef pair<double, string> pii;
map<int, pii> ans;
int main()
{
int n;
int x;
double y;
string z;
cin >> n;
for(int i = 0; i < n; ++ i){
cin >> x >> y >> z;
ans.insert({x, {y, z}});
}
for(map<int, pii>::iterator i = ans.begin(); i != ans.end(); ++ i){
printf("%d %.2lf %s\n", i-> first, i-> second.first, i -> second.second.c_str());
}
return 0;
}
五、结构体