In the below example a struct Comp is created. Is there a way to make it a std::function or write the comparator itself while calling std::equal_range? Can I get rid of the struct?
struct Comp
{
bool operator() ( const S& s, int i ) const { return s.number < i; }
bool operator() ( int i, const S& s ) const { return i < s.number; }
};
const auto p2 = std::equal_range(vec.begin(),vec.end(), 2, Comp{});
You can use
boost::hana::overloadto overload lambdas:Obviously you can put the whole
boost::hana::overload(…)thing in the call tostd::equal_range, but it just makes the code less readable, imho.However, I would stress that whether you write it yourself or not, you'll likely end up with using a
struct/class; e.g.std::functionis a templatedclass,boost::hana::overloadis an object of a givenclass, and so on.Since you haven't specified a standard, probably using Jeff Garrett solution is the way to go (though I'd change
vec.begin(), vec.end(),tovec,, if you are to process the wholevec). If you have to be C++17 compliant, than you can use Range-v3:which is ok for C++14 too, as long as you don't rely on CTAD: