By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
 429,613 Members | 1,890 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,613 IT Pros & Developers. It's quick & easy.

# sorting question

 P: n/a I have a class class Point2D { double x; 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 Sep 20 '06 #1
Share this Question
5 Replies

 P: n/a John wrote: I have a class class Point2D { double x; 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. Sep 20 '06 #2

 P: n/a On 20 Sep 2006 12:25:58 -0700 in comp.lang.c++, "John" I want to implement a function that can take in as inputpoint2Dsort( 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 implementpoint2Dsort 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. Sep 20 '06 #3

 P: n/a John wrote: I have a class class Point2D { double x; 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 Sep 20 '06 #4

 P: n/a Kai-Uwe Bux wrote: John wrote: >I have a classclass Point2D {double x;public:....}I want to implement a function that can take in as inputpoint2Dsort( 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 implementpoint2Dsort 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? Sep 20 '06 #5

 P: n/a Mark P wrote: Kai-Uwe Bux wrote: 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? Yes, if it's a specialization of a template already in namespace std, as in this example. Best regards, Tom Sep 20 '06 #6

### This discussion thread is closed

Replies have been disabled for this discussion. 