473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Need help sorting a list please...

According to the docs...
http://www.cppreference.com/cpplist/sort.html

There is a parameter to the std::list.sort method.
I don't understand this methods use...

I want to sort the list which is a list of points x,y,z.
I have three points involved in the sort...
The two points the sort algorithm is providing for comparison
and a refrence point. I want the list sorted by in order of closest
to refrence point. (Euclidian distance)

I suppose I need a class to represent the points
and an operator<, which knows about the refrence point...

Am I going about this the right way?
Help?

Feb 14 '06 #1
5 1564
JustSomeGuy wrote:
According to the docs...
http://www.cppreference.com/cpplist/sort.html

There is a parameter to the std::list.sort method.
I don't understand this methods use...

I want to sort the list which is a list of points x,y,z.
I have three points involved in the sort...
The two points the sort algorithm is providing for comparison
and a refrence point. I want the list sorted by in order of closest
to refrence point. (Euclidian distance)

I suppose I need a class to represent the points
and an operator<, which knows about the refrence point...

Am I going about this the right way?
Help?


You need an object that holds the coordinates for the reference point
and provides an operator() that takes two points and returns true if the
first is less than (i.e. closer to the reference point than) the second.

struct nearer
{
nearer(point ref0) : ref(ref0) {}
bool operator()(point p0, point p1)
{
return distance(p0, ref) < distance(p1, ref);
}
private:
point ref;
};

And then you should be able to do this:

mylist.sort(nearer(reference));

If you're uncomfortable with that nesting, it's roughly equivalent to this:

nearer n(reference);
mylist.sort(n);

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Feb 14 '06 #2
This looks good...
I've don't quite grasp why this is a struct rather than a class...

Feb 14 '06 #3
JustSomeGuy wrote:
This looks good...
I've don't quite grasp why this is a struct rather than a class...


Doesn't really matter.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Feb 14 '06 #4
Pete Becker wrote:
JustSomeGuy wrote:
According to the docs...
http://www.cppreference.com/cpplist/sort.html

There is a parameter to the std::list.sort method.
I don't understand this methods use...

I want to sort the list which is a list of points x,y,z.
I have three points involved in the sort...
The two points the sort algorithm is providing for comparison
and a refrence point. I want the list sorted by in order of closest
to refrence point. (Euclidian distance)

I suppose I need a class to represent the points
and an operator<, which knows about the refrence point...

Am I going about this the right way?
Help?


You need an object that holds the coordinates for the reference point
and provides an operator() that takes two points and returns true if the
first is less than (i.e. closer to the reference point than) the second.

struct nearer
{
nearer(point ref0) : ref(ref0) {}
bool operator()(point p0, point p1)
{
return distance(p0, ref) < distance(p1, ref);
}
private:
point ref;
};


Any reason not to make the arguments to the ctor and to operator()
references to constants?
Feb 14 '06 #5
Mark P wrote:

Any reason not to make the arguments to the ctor and to operator()
references to constants?


A point holds two or three values. Not a big thing to pass, and passing
by reference can be slower.

--

Pete Becker
Roundhouse Consulting, Ltd.
Feb 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
3
by: Leszek Klich | last post by:
Hello All ! I have a task: QT library: List ListBox. I generating n random numbers from range 0 to 100. I have to sort it by hand... It has to look nicely. It's my workhome from my school....
3
by: Jef Driesen | last post by:
The number of items in my std::list is changed after sorting (using std::list member function sort). I'm using MSVC6 and the actual (pseudo) code is posted below. The output from size() is...
1
by: aredo3604gif | last post by:
On Sun, 10 Apr 2005 19:46:32 GMT, aredo3604gif@yahoo.com wrote: >The user can dynamically enter and change the rule connection between >objects. The rule is a "<" and so given two objects: >a <...
4
by: chellappa | last post by:
Hi Everybody, This is i am trying for sorting linked using pointer.......... but i dont know wthere this logic is good or not........... please correct eroor.......it is not working ,i t simple...
8
by: ianaré | last post by:
Hey all, if i use a os.walk() to append files to a list like so... files = root = self.path.GetValue() # wx.TextCtrl input filter = self.fileType.GetValue().lower() # wx.TextCtrl input...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
15
by: rhino | last post by:
I've put together a prototype of two-tiered CSS tabs that works really well in IE6, IE7, and FF2. It also works very well in Opera 9.27 _except_ that the placement of the lower tier of tabs is...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.