I have written my own compare function to sort a vector of pairs. My sort function should be like this. The point (i,j) will be ahead of point(x,y) if it is closer to (5,5), vice-versa. I am finding the distance and then comparing based on that. The code is
#include<iostream>
#include<stdlib.h>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
double distance(int a, int b, int x, int y)
{
return sqrt(pow(a-x,2.0)+pow(b-y,2.0));
}
bool mycomp(const pair<int, int >&i, const pair<int, int >&j)
{
double dis=distance(i.first, i.second, 5,5);
double dis2=distance(j.first, j.second, 5, 5);
if(dis<dis2)
return i.first< j.first;
return i.first>j.first;
}
int main()
{
int n;
cin>>n;
vector<pair<int, int> > p;
for(int i=0; i < n; i++)
{
int a,b;
cin>>a >>b;
p.push_back(make_pair(a,b));
}
sort(p.begin(),p.end(),mycomp);
for(int i=0; i<n; i++)
cout<<p[i].first<<" "<<p[i].second<<endl;
return 0;
}