C++ sort by tuple or by pair<int, pair<int, int>>

Jimmy (xiaoke) Shen
1 min readAug 7, 2020

C++ can support sort by tuple which is the same as sorting by tuple in python.

One sort by tuple example can be found HERE of the solution of

987. Vertical Order Traversal of a Binary Tree

At the same time, C++ also support sort by pair, even for hybrid pairs.

#include <bits/stdc++.h>using namespace std;
typedef pair<int, int> ii;
int main()
{
vector<pair<int, ii>> a;
for (int i = 0; i < 10; ++i)
a.push_back(make_pair(i, make_pair(i+1, i+2)));
swap(a[0], a[3]);
for (auto v : a) cout<<v.first<<endl;
sort(a.begin(), a.end());
for (auto v : a) cout<<v.first<<endl;
return 0;
}

Output of the above code can be found here

3
1
2
0
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9

--

--