std::max_element

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

    std::max_element

    Hi. To find the maximum element in a container using the predicate form, do
    we supply a less predicate (one whose operator()(lhs, rhs) returns true if
    lhs < rhs), or a more predicate (one whose operator()(lhs, rhs) returns true
    if lhs > rhs)?


    I found something like this works, but want to confirm:

    find = std::max_elemen t(v.begin(), v.end(), less());

    where

    // real struct and operator() in my application are more complex
    struct less : std::binary_fun ction<int, int, bool> {
    bool operator()(int lhs, int rhs) const { return lhs<rhs; }
    };


    Second, to find the maximum element, one could also use either of the
    following, right?

    find = std::max_elemen t(v.begin(), v.end(), less());

    or

    find = std::min_elemen t(v.begin(), v.end(), more());


    Thanks!


  • John Harrison

    #2
    Re: std::max_elemen t


    "Siemel Naran" <SiemelNaran@RE MOVE.att.net> wrote in message
    news:q_ndd.7257 08$Gx4.100292@b gtnsc04-news.ops.worldn et.att.net...[color=blue]
    > Hi. To find the maximum element in a container using the predicate form,
    > do
    > we supply a less predicate (one whose operator()(lhs, rhs) returns true if
    > lhs < rhs), or a more predicate (one whose operator()(lhs, rhs) returns
    > true
    > if lhs > rhs)?
    >[/color]

    A less predicate.
    [color=blue]
    >
    > I found something like this works, but want to confirm:
    >
    > find = std::max_elemen t(v.begin(), v.end,less
    >
    > where
    >
    > // real struct and operator() in my application are more complex
    > struct less : std::binary_fun ction<int, int, bool> {
    > bool operator()(int lhs, int rhs) const { return lhs<rhs; }
    > };
    >
    >
    > Second, to find the maximum element, one could also use either of the
    > following, right?
    >
    > find = std::max_elemen t(v.begin(), v.end,less
    >
    > or
    >
    > find = std::min_elemen t(v.begin(), v.end,more
    >
    >
    > Thanks!
    >[/color]

    Seems reasonable, but the two calls won't necessarily return the same result
    in the case where your container contains several equal maximum elements.

    John


    Comment

    Working...