Retrieving y co-ordinates

Hi friends,
I recently came up with a question. Kindly tell me it’s solution. My question:

There are n points with co-ordinates (x1,y1), (x2,y2),…, (xn,yn). I want to sort these points based on the value of their x co-ordinates. Therefore I am using the sort function to sort the x co-ordinates, but am unable to access there respective y co-ordinates. Please tell me a way by which I can retrieve the corresponding y co-ordinates after sorting with the best efficiency.

Thanks in advance

sort using struct,bool func.

      struct vert

    {
         int xcord,ycord;
    };

    bool cmp (vert a,vert b) //two objects a&b of type vert(struct)

    {

        return a.xcord<b.xcord;//sorting structs based on xcord
    
    }

//in main function

   vert v[n];//assume n pts are there

   sort(v,v+n,cmp);

@a1a99_3 I carried out your answer and am getting the desired answer. Thanks! However, can you kindly tell me why/how is the ‘cmp’ function sorting the structure based on the xcord and how is it being carried out in the sort function.Thanks onc e again!

2 Likes

You can also use a vector of pairs. Try running the below code to understand how.

https://www.dropbox.com/s/gjx00t93lh5hifh/Vector%20of%20pairs.cpp?dl=0

Personally, I am biased towards this method of sorting as I’ve never used structs before :stuck_out_tongue:

Or an array of pairs.

1 Like

@anupam_datta……Didn’t I explain it to you a billion times in school!!!
And HOW DO YOU carry out AN !?Answer?! and get the “DESIRED” !?Answer?!..LOL
(CaRrY oUt???)

I am the one who liked ur SILLY comment and am very sure that I’ll remain to ONLY one to do so…HA HA HA!!!

i bet u r solving RECTANGLES…

OOOOOOOOOOOOOOOOOOOOOO…u got another like!!!

@arpanb8 Therefore aren’t you SILLY enough to consider my comment SILLY?!?!?!?!?!

i mean 2 is the upper bound for numberOfLikes.
numberOfLikes<=2…