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
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
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
participants (3)
-
Kenny Riddile
-
Nathan Ridge
-
przemyslaw.sliwa@uk.bnpparibas.com