class Widget;
std::vector< std::shared_ptr<Widget> > container
class Criterium
{
public:
bool operator()(const Widget& left, const Widget& right)const;
};
How can I sort the container according to the Criterium, without defining another criterium like:
class CriteriumForPointers
{
public:
bool operator()(const std::shared_ptr<Widget>& left,
const std::shared_ptr<Widget>& right)const;
};
You could use a lambda as an adapter:
Criterium criterium;
sort(container.begin(), container.end(),
[&criterium] (const shared_ptr<Widget> &l, const shared_ptr<Widget> &r)
{
return criterium(*l.get(), *r.get());
}
);
Use indirect iterators like this:
std::sort(
boost::make_indirect_iterator(container.begin()),
boost::make_indirect_iterator(container.end()),
Criterium()
);
See also: http://www.boost.org/doc/libs/1_49_0/libs/iterator/doc/indirect_iterator.html
Maybe you could put a second overload of the operator() into your Criterium-classe, like this:
class Criterium {
bool operator()(const Widget& left, const Widget& right)const;
bool operator()(const std::shared_ptr<Widget>& left,
const std::shared_ptr<Widget>& right)const
{ return operator()( *left, *right ); }
};
(note: I didn't test this)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With