Need help sorting a list please...

J

JustSomeGuy

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?
 
P

Pete Becker

JustSomeGuy said:
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);
 
J

JustSomeGuy

This looks good...
I've don't quite grasp why this is a struct rather than a class...
 
M

Mark P

Pete said:
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?
 
P

Pete Becker

Mark said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top