Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-09-01 12:48:59


AMDG

Rodolfo Federico Gamarra wrote:
> Hello Boost users,
>
> I'm working on my thesis with the Boost library (Boost-1.34.1) running on
> g++ (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2).
>
> What I'm trying to do can be done very simply, but I preferred to do it more
> appropriately, I believe, using the Boost Min-Max library. I'm trying to
> simultaneously obtain the maximum and minimum values of a member function
> call over a lists of objects.
>
> Suppose there exists a
> class utl::Particle;
> with a member function
> double GetEnergy() const;
>
> In a member field of another class I have a list of those particles
> std::list<utl::Particle> particles;
> for which I want min/max GetEnergy() values.
>
> How is that accomplished using Boost.Min-Max?
>

Use minmax_element rather than minmax.

#include <boost/algorithm/minmax_element.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/bind.hpp>
#include <list>

class Particle {
public:
    double getEnergy() const { return(0.0); }
};

int main() {
    std::list<Particle> particles;
    std::list<Particle>::iterator min, max;
    boost::tie(min, max) =
        boost::minmax_element(particles.begin(), particles.end(),
            boost::bind(&Particle::getEnergy, _1) <
            boost::bind(&Particle::getEnergy, _2));
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net