|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-03-20 07:55:09
From: "beatlebum1967" <beatlebum1967_at_[hidden]>
> Thanks to this group I have figured out how to use boost::bind in
> conjunction with for_each to call a member function for each pointer
> in a container. Now I would like to use a similar idiom to sum the
> value of a member given a container of boost::shared_ptrs e.g.
>
> class SomeClass
> {
> ..
> long m_Long;
> }
>
> std::vector<boost::shared_ptr<SomeClass> > LongVec;
>
> Use std::count to sum the values of SomeClass::m_Long in LongVec.
The latest CVS bind can do this:
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <vector>
#include <algorithm>
#include <functional>
struct X
{
int data;
};
int main()
{
std::vector< boost::shared_ptr<X> > v;
std::count_if( v.begin(), v.end(),
boost::bind< bool >
(
std::equal_to<int>(), 0,
boost::bind( &X::data, _1 )
)
);
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk