std:sort predicate function optional arguments

Discussion in 'C++' started by Ganesh, Sep 29, 2008.

  1. Ganesh

    Ganesh Guest

    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
    Ganesh, Sep 29, 2008
    #1
    1. Advertising

  2. On 9ÔÂ29ÈÕ, ÏÂÎç6ʱ01·Ö, Ganesh <> wrote:
    > 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_;
    };
    Xiaobin Huang, Sep 29, 2008
    #2
    1. Advertising

  3. 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?
    Juha Nieminen, Sep 29, 2008
    #3
  4. Ganesh

    James Kanze Guest

    On Sep 29, 9:51 pm, Pete Becker <> wrote:
    > On 2008-09-29 13:45:24 -0400, Juha Nieminen <> said:
    > > 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 30, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hall
    Replies:
    4
    Views:
    651
  2. matthias_k
    Replies:
    5
    Views:
    7,802
    Karthik Kumar
    Jan 24, 2005
  3. Replies:
    1
    Views:
    484
    Marcus Kwok
    Apr 6, 2007
  4. Replies:
    4
    Views:
    469
  5. Victor Bazarov
    Replies:
    3
    Views:
    818
    James Kanze
    Feb 2, 2010
Loading...

Share This Page