#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<pair<int,int>>v;
v.push_back(make_pair(1,3));
v.push_back(make_pair(1,1));
v.push_back(make_pair(2,19));
v.push_back(make_pair(2,4));
int n = 4;
stable_sort(v.begin(),v.end());
for (int i = 0; i < n; i++)
cout << "[" << v[i].first << ", " << v[i].second
<< "] ";
return 0;
}
Output : [1, 1] [1, 3] [2, 4] [2, 19]
Expected Output : [1, 3] [1, 1] [2, 19] [2, 4]
Why does the vector of pairs not maintain the relative ordering even after applying stable sort, when we know that by default the vector of pairs is sorted on the basis of first element of the vector? If I write the comparator function, then it is working properly but not when I don't define any comparator function. Why is it so?