std:sort predicate function optional arguments

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

    std:sort predicate function optional arguments

    hi !

    Can the predicate function used in std::sort take optional
    arguments ? For instance. I have a class Point. I create a vector of
    this and then want to compare the slopes of these points with respect
    to another point of the same class (Graham's convex hull algorithm
    anyone ?)

    ganesh
  • Xiaobin Huang

    #2
    Re: std:sort predicate function optional arguments

    On 9ÔÂ29ÈÕ, ÏÂÎç6ʱ01·Ö, Ganesh <ganesh.i...@gm ail.comwrote:
    hi !
    >
    Can the predicate function used in std::sort take optional
    arguments ? For instance. I have a class Point. I create a vector of
    this and then want to compare the slopes of these points with respect
    to another point of the same class (Graham's convex hull algorithm
    anyone ?)
    >
    ganesh
    you can provide your own predicate.

    class cmp {
    public:
    cmp(...) {
    // init thePoint_
    }
    bool operator() (point const& lh, point const& rh) {
    // ...
    }
    private:
    point thePoint_;
    };

    Comment

    • Juha Nieminen

      #3
      Re: std:sort predicate function optional arguments

      Xiaobin Huang wrote:
      you can provide your own predicate.
      >
      class cmp {
      public:
      cmp(...) {
      // init thePoint_
      }
      bool operator() (point const& lh, point const& rh) {
      // ...
      }
      private:
      point thePoint_;
      };
      Nitpicking, but isn't that called a comparator? If is it *also* a
      predicate?

      Comment

      • Pete Becker

        #4
        Re: std:sort predicate function optional arguments

        On 2008-09-29 13:45:24 -0400, Juha Nieminen <nospam@thanks. invalidsaid:
        Xiaobin Huang wrote:
        >you can provide your own predicate.
        >>
        >class cmp {
        >public:
        >cmp(...) {
        >// init thePoint_
        >}
        >bool operator() (point const& lh, point const& rh) {
        >// ...
        >}
        >private:
        >point thePoint_;
        >};
        >
        Nitpicking, but isn't that called a comparator? If is it *also* a
        predicate?
        There is a version of std::sort that takes a predicate. It doesn't care
        whether you call it a comparator, a functoid, or fred. But when you're
        talking about std::sort, if you refer to a predicate by some other
        name, you run the risk of confusing your listener.

        --
        Pete
        Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
        Standard C++ Library Extensions: a Tutorial and Reference
        (www.petebecker.com/tr1book)

        Comment

        • James Kanze

          #5
          Re: std:sort predicate function optional arguments

          On Sep 29, 9:51 pm, Pete Becker <p...@versatile coding.comwrote :
          On 2008-09-29 13:45:24 -0400, Juha Nieminen <nos...@thanks. invalidsaid:
          Xiaobin Huang wrote:
          you can provide your own predicate.
          class cmp {
          public:
          cmp(...) {
          // init thePoint_
          }
          bool operator() (point const& lh, point const& rh) {
          // ...
          }
          private:
          point thePoint_;
          };
          Nitpicking, but isn't that called a comparator? If is it
          *also* a predicate?
          There is a version of std::sort that takes a predicate. It
          doesn't care whether you call it a comparator, a functoid, or
          fred. But when you're talking about std::sort, if you refer to
          a predicate by some other name, you run the risk of confusing
          your listener.
          While it's obviously a predicate; any functional object which
          returns bool is a predicate. The standard never uses the word
          predicate for it, however, and calls it Compare. It also
          imposes a number of additional constraints on it which don't
          apply to predicates in general. In fact, it even says that "A
          sequence is sorted with respect to a comparator comp if [...]",
          using the very same word as Juha. So while it is a predicate,
          I'd consider "comparator " a more precise word. (All comparators
          are predicates, but not all predicates are comparators. And
          here, we need a predicate which is a comparator.)

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Pete Becker

            #6
            Re: std:sort predicate function optional arguments

            On 2008-09-30 05:16:36 -0400, James Kanze <james.kanze@gm ail.comsaid:
            On Sep 29, 9:51 pm, Pete Becker <p...@versatile coding.comwrote :
            >On 2008-09-29 13:45:24 -0400, Juha Nieminen <nos...@thanks. invalidsaid:
            >>Xiaobin Huang wrote:
            >>>you can provide your own predicate.
            >
            >>>class cmp {
            >>>public:
            >>>cmp(...) {
            >>>// init thePoint_
            >>>}
            >>>bool operator() (point const& lh, point const& rh) {
            >>>// ...
            >>>}
            >>>private:
            >>>point thePoint_;
            >>>};
            >
            >>Nitpicking, but isn't that called a comparator? If is it
            >>*also* a predicate?
            >
            >There is a version of std::sort that takes a predicate. It
            >doesn't care whether you call it a comparator, a functoid, or
            >fred. But when you're talking about std::sort, if you refer to
            >a predicate by some other name, you run the risk of confusing
            >your listener.
            >
            While it's obviously a predicate; any functional object which
            returns bool is a predicate. The standard never uses the word
            predicate for it, however, and calls it Compare. It also
            imposes a number of additional constraints on it which don't
            apply to predicates in general. In fact, it even says that "A
            sequence is sorted with respect to a comparator comp if [...]",
            using the very same word as Juha. So while it is a predicate,
            I'd consider "comparator " a more precise word. (All comparators
            are predicates, but not all predicates are comparators. And
            here, we need a predicate which is a comparator.)
            You're right, of course. Sorting and partitioning rely on comparators
            (which take two arguments), and most other algorithms rely on
            predicates (which take one argument). I should know better than to post
            while I'm in the middle of post-meeting editing.

            --
            Pete
            Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
            Standard C++ Library Extensions: a Tutorial and Reference
            (www.petebecker.com/tr1book)

            Comment

            Working...