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

荒凉一梦 提交于 2019-12-10 18:29:47

问题


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?


回答1:


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.




回答2:


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, ""});



回答3:


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_;
}


来源:https://stackoverflow.com/questions/24308993/stdequal-range-not-working-with-strucutre-having-operator-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!