I have a following code
#include <bits/stdc++.h>
using namespace std;
int main () {
pair<int, int> p[4];
p[0] = pair<int, int>(5, 2);
p[1] = pair<int, int>(40, -2);
p[2] = pair<int, int>(-3, 2);
p[3] = pair<int, int>(4, 45);
auto print_pairii = [](pair<int, int> pp[]) {
for (int i = 0; i < 4; i++) {
cout << pp[i].first << " ";
}
cout << endl;
};
print_pairii(p);
sort(p, p + 4);
print_pairii(p);
return 0;
}
The first print_pairii
shows 5 40 -3 4
.
After sorting the array of pair, the print_pairii
shows -3 4 5 40
, meaning that the sorting was done in the basis of the first element of the pair.
Why does this happen instead of the basis of the second element? How does sort work in this sense?