Using boost::shared_ptr with std::set

Hello, I have to use a set of shared pointers to objects. The struct I have to use looks like struct eventIntLim { enum eType { eStart = 0, eEnd }; eType type; double varianceBump; eventIntLim(eType t, double v) : type(t), varianceBump(v) {} bool operator < (const eventIntLim &rhs) const { return fabs(varianceBump) < fabs(rhs.varianceBump); } }; and the definition of the set is typedef boost::shared_ptr<eventIntLim> eventIntLimPtr; typedef std::set<eventIntLimPtr, lessThan> eventIntLimPtrSet; typedef std::map<size_t, eventIntLimPtrSet> eventIntLimMap; In order to sort the objects in the set I have to use the functor struct lessThan : public std::binary_function<boost::shared_ptr<eventIntLim>, boost::shared_ptr<eventIntLim>, bool> { bool operator ()(const boost::shared_ptr<eventIntLim> &lhs, const boost::shared_ptr<eventIntLim> &rhs) const { return (*lhs) < (*rhs); } }; This is quite long and I believe boost can shorten it a lot. I have tried it with boost::lamba and boost::bind but could not make it working. Basically I would like to eliminate the need of writing the definition of struct lessThan. Could someone help me with that? Cheers ___________________________________________________________ This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorised copying, disclosure or distribution of the material in this e-mail is prohibited. Please refer to http://www.bnpparibas.co.uk/en/information/legal_information.asp?Code=ECAS-8... for additional disclosures.

Use boost::less_pointees_t< boost::shared_ptr<eventIntLim> > http://www.boost.org/doc/libs/1_46_1/boost/utility/compare_pointees.hpp

From: przemyslaw.sliwa@uk.bnpparibas.com
<snip>
In order to sort the objects in the set I have to use the functor
struct lessThan : public std::binary_function, boost::shared_ptr, bool> { bool operator ()(const boost::shared_ptr &lhs, const boost::shared_ptr &rhs) const { return (*lhs) < (*rhs); } };
This is quite long and I believe boost can shorten it a lot. I have tried it with boost::lamba and boost::bind but could not make it working. Basically I would like to eliminate the need of writing the definition of struct lessThan. Could someone help me with that?
You could write one class that works for all types: struct indirectLessThan { template <typename PtrT> bool operator()(PtrT a, PtrT b) { return *a < *b; } }; And use it like this: typedef std::set<eventIntLimPtr, indirectLessThan> eventIntLimPtrSet; And you can re-use this indirectLessThan class in other situtations where you want to compare pointers (whether shared_ptr or raw pointers) by comparing the things they point to. Regards, Nate.

From: przemyslaw.sliwa@uk.bnpparibas.com
<snip>
In order to sort the objects in the set I have to use the functor
struct lessThan : public std::binary_function, boost::shared_ptr, bool> { bool operator ()(const boost::shared_ptr &lhs, const boost::shared_ptr &rhs) const { return (*lhs) < (*rhs); } };
This is quite long and I believe boost can shorten it a lot. I have tried it with boost::lamba and boost::bind but could not make it working. Basically I would like to eliminate the need of writing the definition of struct lessThan. Could someone help me with that?
Here's an alternate solution using boost::lambda and BOOST_TYPEOF. #include <boost/lambda/lambda.hpp> #include <boost/typeof/typeof.hpp> using namespace boost::lambda; typedef BOOST_TYPEOF(*_1 < *_2) lambda_type; typedef std::set<eventIntLimPtr, lambda_type> eventIntLimPtrSet; The only wrinkle is that the type of the lambda *_1 < *_2 does not default construct to give you *_1 < *_2, so you will have to give *_1 < *_2 as an extra constructor parameter every time you construct an eventIntLimPtrSet. If that really bothers you, you can write a derived class with forwarded constructors that put in the extra argument for you: struct eventIntLimPtrSet : std::set<eventIntLimPtr, lambda_type> { typedef std::set<eventIntLimPtr, lambda_type> base_type; eventIntLimPtrSet() : base_type(*_1 < *_2) {} // Other forwarded set constructors }; Regards, Nate.
participants (3)
-
Kenny Riddile
-
Nathan Ridge
-
przemyslaw.sliwa@uk.bnpparibas.com