std::equal_range not working with strucutre having operator< defined

520 views Asked by At

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?

3

There are 3 answers

0
quantdev On BEST ANSWER

Provide non-member comparison operators :

 bool operator<(int v, const MyFoo& foo)
 { 
   return foo.v_ < v;
 }

 bool operator<(const MyFoo& foo, int v)
 {
   return v < foo;
 }

Alternatively, you can provide a conversion operator to int :

operator int() cont {return v_;}

Which is probably unwanted, since the compiler will be able to perform silent conversions in other places of your code.

0
Jarod42 On

As an other alternative: provide

bool operator<(const MyFoo& rhs) const { return v_ < rhs.v_; }

And use std::equal_range on a dummy object with correct v_ as:

std::pair<Ptr, Ptr> pr = std::equal_range(data.begin(), data.end(), MyFoo{10, ""});
0
lordjeb On

You may be having trouble because the std::equal_range implementation uses std::less. This is going to try to convert your MyFoo to an int to do the comparison, rather than just using an operator<() overload. Try adding this to your MyFoo class...

operator int() const
{
    return v_;
}