I am trying to use std::equal_range
with the structure below I have compilation error saying that error: no match for ‘operator<’
.
struct MyFoo {
int v_;
string n_;
bool operator<(int v) const
{ return v_ < v;}
};
vector<MyFoo> data;
// data is sorted by int v_
typedef vector<MyFoo>::iterator Ptr;
std::pair< Ptr, Ptr > pr = std::equal_range(data.begin(), data.end(), 10);
I've looked into the template implementatino and what is failing is the following where *it
is deferenging the iterator pointing to an object of MyFoo and val_
is 10.
if(*it < val_) {
...
}
Why it is not working? I thought probably because it is trying to call the the global operator<
that is not defined, but since I defined it as class member that should not be a problem, isn't it?
Provide non-member comparison operators :
Alternatively, you can provide a conversion operator to
int
:Which is probably unwanted, since the compiler will be able to perform silent conversions in other places of your code.