Setting predicate for std::list::sort().

Discussion in 'C++' started by jason.cipriani@gmail.com, Mar 18, 2008.

  1. Guest

    How can I use my own custom comparison function with
    std::list::sort()? The only call to sort() I see does not take a
    predicate argument.

    Specifically, I have:

    list<pair<double,MyType> > ...;

    And I want to sort only on the value of the first member in that pair.
    My solution right now is to not use a pair; doing something like this
    instead:

    struct A {
    double d;
    MyType t;
    bool operator < (const A &a) const {
    return d < a.d;
    }
    };

    list<A> ...;

    But I am wondering how to do it in general.

    Thanks,
    Jason
    , Mar 18, 2008
    #1
    1. Advertising

  2. wrote:
    > How can I use my own custom comparison function with
    > std::list::sort()? The only call to sort() I see does not take a
    > predicate argument.
    >
    > Specifically, I have:
    >
    > list<pair<double,MyType> > ...;
    >
    > And I want to sort only on the value of the first member in that pair.
    > My solution right now is to not use a pair; doing something like this
    > instead:
    >
    > struct A {
    > double d;
    > MyType t;
    > bool operator < (const A &a) const {
    > return d < a.d;
    > }
    > };
    >
    > list<A> ...;
    >
    > But I am wondering how to do it in general.


    What seems to be the problem?

    bool firstIsLess(std::pair<double,MyType> const &p1,
    std::pair<double,MyType> const &p2)
    {
    return p1.first < p2.first;
    }

    ...
    std::list<std::pair ...> myList;

    myList.sort(firstIsLess);

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 18, 2008
    #2
    1. Advertising

  3. red floyd Guest

    wrote:
    > How can I use my own custom comparison function with
    > std::list::sort()? The only call to sort() I see does not take a
    > predicate argument.
    >
    > Specifically, I have:
    >
    > list<pair<double,MyType> > ...;
    >
    > And I want to sort only on the value of the first member in that pair.
    > My solution right now is to not use a pair; doing something like this
    > instead:
    >
    > struct A {
    > double d;
    > MyType t;
    > bool operator < (const A &a) const {
    > return d < a.d;
    > }
    > };
    >
    > list<A> ...;
    >
    > But I am wondering how to do it in general.
    >


    std::list::sort is overloaded. One takes no parameters, using std::less
    as the predicate, the other takes a predicate.

    e.g.

    struct comp {
    typedef std::pair<double, MyType> pr;
    bool operator()(const pr& lhs, const pr& rhs) const
    {
    // do the compare and return accordingly
    }
    }

    std::list<std::pair<double, MyType> > l;

    l.sort(comp());
    red floyd, Mar 18, 2008
    #3
  4. red floyd Guest

    wrote:
    > How can I use my own custom comparison function with
    > std::list::sort()? The only call to sort() I see does not take a
    > predicate argument.
    >
    > Specifically, I have:
    >
    > list<pair<double,MyType> > ...;
    >
    > And I want to sort only on the value of the first member in that pair.
    > My solution right now is to not use a pair; doing something like this
    > instead:
    >
    > struct A {
    > double d;
    > MyType t;
    > bool operator < (const A &a) const {
    > return d < a.d;
    > }
    > };
    >
    > list<A> ...;
    >
    > But I am wondering how to do it in general.
    >


    Plus, you are allowed to overload std::less for your types -- one
    of the rare cases where you are allowed to mess around in std::.

    e.g:

    template<>
    bool std::less<std::pair<double, MyType> >(
    const std::pair<double, MyType>& lhs,
    const std::pair<double, MyType>& rhs)
    {
    return lhs.d < rhs.d;
    }
    red floyd, Mar 18, 2008
    #4
  5. Guest

    On Mar 18, 5:55 pm, ""
    <> wrote:
    > How can I use my own custom comparison function with
    > std::list::sort()? The only call to sort() I see does not take a
    > predicate argument.


    Thanks Victor and red floyd.

    The problem was that I don't use std::list::sort() much, jumped to a
    conclusion about a different compiler error; and then, unfortunately,
    also missed the mention of the overloaded sort() in the documentation
    I looked at. So I screwed it up and there was no problem. Thanks for
    answering.

    Jason
    , Mar 18, 2008
    #5
    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:
    656
  2. matthias_k
    Replies:
    5
    Views:
    7,805
    Karthik Kumar
    Jan 24, 2005
  3. Replies:
    1
    Views:
    484
    Marcus Kwok
    Apr 6, 2007
  4. Ganesh
    Replies:
    3
    Views:
    353
    James Kanze
    Sep 30, 2008
  5. Victor Bazarov
    Replies:
    3
    Views:
    821
    James Kanze
    Feb 2, 2010
Loading...

Share This Page