利用 STL 當中的其中的資料結構 vector 和 pair
就可以簡單的寫一個比起陣列更好維護的程式碼
程式實作大概如下
可以注意一下註解。
#include <bits/stdc++.h>#define int long long#define endl "\n"#define F first#define S secondusing namespace std;signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<pair<int,int>> p; //利用 vector 包 pair for(int i = 0; i < n; ++i) { int a,b; cin >> a >> b; p.emplace_back(make_pair(a,b)); //將 a b 用 pair 裝起來 } sort(p.begin(), p.end()); //排序 for(int i = 0; i < n; ++i) { cout << p[i].F << " " << p[i].S << endl; // 輸出, F 是 first , S 則是 Second(在上面的 define) }}