sorting question

J

John

I have a class

class Point2D {

double x[2];

public:
.....

}

I want to implement a function that can take in as input

point2Dsort( double * pdA, int number_of_points)

and sort it in lexicographic order. (i.e. if p1.x() > p2.x() then p1 >
p2 ...

Is there a way to define operator< on the point class and implement
point2Dsort using c++ sort() function?

Thanks,
--j
 
M

Mark P

John said:
I have a class

class Point2D {

double x[2];

public:
....

}

I want to implement a function that can take in as input

point2Dsort( double * pdA, int number_of_points)

and sort it in lexicographic order. (i.e. if p1.x() > p2.x() then p1 >
p2 ...

Is there a way to define operator< on the point class and implement
point2Dsort using c++ sort() function?

Thanks,
--j

You can give Point2D a public member function:

bool operator<( const Point2D& rhs) const;

Then you can use std::sort.
 
D

David Harmon

On 20 Sep 2006 12:25:58 -0700 in comp.lang.c++, "John"
I want to implement a function that can take in as input

point2Dsort( double * pdA, int number_of_points)

and sort it in lexicographic order. (i.e. if p1.x() > p2.x() then p1 >
p2 ...

Is there a way to define operator< on the point class and implement
point2Dsort using c++ sort() function?

Yes.

bool operator<(Point2D l, Point2D r)
{
// return true or false here
}

However, it's not obvious how you are going to get from a pointer to
double, to anything having to do with Point2D, in point2Dsort().
That "point" may need some rethinking.
 
K

Kai-Uwe Bux

John said:
I have a class

class Point2D {

double x[2];

public:
....

}

I want to implement a function that can take in as input

point2Dsort( double * pdA, int number_of_points)

and sort it in lexicographic order. (i.e. if p1.x() > p2.x() then p1 >
p2 ...

Is there a way to define operator< on the point class and implement
point2Dsort using c++ sort() function?

Yes, you have several options:

a) define a member function

class Point2D {
...
bool operator< ( Point2D const & rhs ) const {
}

}

b) define a free-standing friend operator:

bool operator< ( Point2D const & lhs, Point2D const & rhs ) {
}

c) add a specialization to std::less<>:

namespace std {

template <>
struct less< Point2D > : binary_predicate< Point2D > {

bool operator() ( Point2D const & lhs, Point2D const & rhs ) {
}

};

d) define a free standing friend predicate

bool is_less ( Point2D const & lhs, Point2D const & rhs ) {
}

and pass this as a parameter to std::sort and similar critters.

The third and fourth alternatives convey the understanding that the ordering
is not natural. The third is more convenient since std::less will be
automatically used by std::sort and its friends.


Best

Kai-Uwe Bux
 
M

Mark P

Kai-Uwe Bux said:
John said:
I have a class

class Point2D {

double x[2];

public:
....

}

I want to implement a function that can take in as input

point2Dsort( double * pdA, int number_of_points)

and sort it in lexicographic order. (i.e. if p1.x() > p2.x() then p1 >
p2 ...

Is there a way to define operator< on the point class and implement
point2Dsort using c++ sort() function?

Yes, you have several options:


c) add a specialization to std::less<>:

namespace std {

template <>
struct less< Point2D > : binary_predicate< Point2D > {

bool operator() ( Point2D const & lhs, Point2D const & rhs ) {
}

};

Is one "allowed" to add stuff to namespace std?
 
T

Thomas Tutone

Mark said:
Kai-Uwe Bux wrote:

Is one "allowed" to add stuff to namespace std?

Yes, if it's a specialization of a template already in namespace std,
as in this example.

Best regards,

Tom
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top