sorting question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    sorting question

    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_point s)

    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

  • Mark P

    #2
    Re: sorting question

    John wrote:
    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_point s)
    >
    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.

    Comment

    • David Harmon

      #3
      Re: sorting question

      On 20 Sep 2006 12:25:58 -0700 in comp.lang.c++, "John"
      <weekender_ny@y ahoo.comwrote,
      >I want to implement a function that can take in as input
      >
      >point2Dsort( double * pdA, int number_of_point s)
      >
      >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<(Point 2D 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.

      Comment

      • Kai-Uwe Bux

        #4
        Re: sorting question

        John wrote:
        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_point s)
        >
        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_predicat e< 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

        Comment

        • Mark P

          #5
          Re: sorting question

          Kai-Uwe Bux wrote:
          John wrote:
          >
          >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_point s)
          >>
          >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_predicat e< Point2D {
          >
          bool operator() ( Point2D const & lhs, Point2D const & rhs ) {
          }
          >
          };
          >
          Is one "allowed" to add stuff to namespace std?

          Comment

          • Thomas Tutone

            #6
            Re: sorting question


            Mark P wrote:
            Kai-Uwe Bux wrote:
            c) add a specialization to std::less<>:

            namespace std {

            template <>
            struct less< Point2D : binary_predicat e< 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

            Comment

            Working...