std::binary_search 的自定义比较函数
这段代码有问题吗?
bool Spellcheck::smart_comp(string value, string key){
return true;
}
void func(){
std::string aprox_key = "hello";
if(std::binary_search(this->words.begin(), this->words.end(), aprox_key, smart_comp)){
std::cout << "Found" << std::endl;
}
}
我正在尝试编写自己的比较函数来比较二进制搜索中的字符串,
但出现以下错误:
xyz.cpp:40:85: error: no matching function for call to ‘binary_search(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::string&, <unresolved overloaded function type>)’
xyz.cpp:40:85: note: candidates are:
/usr/include/c++/4.6/bits/stl_algo.h:2665:5: note: template<class _FIter, class _Tp> bool std::binary_search(_FIter, _FIter, const _Tp&)
/usr/include/c++/4.6/bits/stl_algo.h:2698:5: note: bool std::binary_search(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >, _Tp = std::basic_string<char>, _Compare = bool (Spellcheck::*)(std::basic_string<char>, std::basic_string<char>)]
/usr/include/c++/4.6/bits/stl_algo.h:2698:5: note: no known conversion for argument 4 from ‘<unresolved overloaded function type>’ to ‘bool (Spellcheck::*)(std::basic_string<char>, std::basic_string<char>)’
感谢任何帮助...
Is there any problem with this code?
bool Spellcheck::smart_comp(string value, string key){
return true;
}
void func(){
std::string aprox_key = "hello";
if(std::binary_search(this->words.begin(), this->words.end(), aprox_key, smart_comp)){
std::cout << "Found" << std::endl;
}
}
I am trying to write my own compare function for comparing strings in binarysearch
I am getting following error:
xyz.cpp:40:85: error: no matching function for call to ‘binary_search(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::string&, <unresolved overloaded function type>)’
xyz.cpp:40:85: note: candidates are:
/usr/include/c++/4.6/bits/stl_algo.h:2665:5: note: template<class _FIter, class _Tp> bool std::binary_search(_FIter, _FIter, const _Tp&)
/usr/include/c++/4.6/bits/stl_algo.h:2698:5: note: bool std::binary_search(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >, _Tp = std::basic_string<char>, _Compare = bool (Spellcheck::*)(std::basic_string<char>, std::basic_string<char>)]
/usr/include/c++/4.6/bits/stl_algo.h:2698:5: note: no known conversion for argument 4 from ‘<unresolved overloaded function type>’ to ‘bool (Spellcheck::*)(std::basic_string<char>, std::basic_string<char>)’
Any help is appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了它总是返回
true
?是的,基本问题是成员函数有一个隐式参数this
,因此签名与预期谓词的签名不匹配。您应该使用static
这个函数,甚至是一个免费函数(如果需要的话,friend
ed)。此外,您每次都会复制字符串
,最好通过 const 引用获取参数,以避免不必要的复制。如果谓词的实际结果取决于 Spellcheck 对象的状态,则必须将该状态绑定到成员函数才能创建具有适当签名的函数对象:
Other than it always returns
true
? Yes, the basic problem is that a member function has an implicit parameterthis
, so the signature does not match that of the expected predicate. You should be doing this functionstatic
or even a free function (friend
ed if needed). Also you are copying thestrings
each time, it would be best if you take the arguments by const reference to avoid unneeded copies.In case the real result of the predicate depends on the state of the
Spellcheck
object, you will have to bind that state to the member function in order to create a function object with the appropiate signature:您试图传递一个非静态成员函数,该函数不可转换为所需的二进制函数(因为有三个实际参数)。
尝试将您的
smart_comp
函数声明为static
。 (当然,你不能引用实例成员;如果你需要有状态,你就必须编写一个完整的函子。)You're trying to pass a non-static member function, which is not convertible to the required binary function (on account of having three actual parameters).
Try declaring your
smart_comp
functionstatic
. (Of course then you can't refer to instance members; if you need statefulness, you'll have to write a full functor.)假设
this->words
的类型为std::vector
并且func
是的成员拼写检查,您可以通过将
smart_comp
声明为static
来解决此问题。但我会对你的课程设计三思而后行。Assuming
this->words
's type isstd::vector<std::string>
andfunc
is a member ofSpellcheck
, you can work it around declaringsmart_comp
to bestatic
. But I would think twice on your class design.