std::binary_search 的自定义比较函数

发布于 2024-12-13 17:34:55 字数 1515 浏览 0 评论 0原文

这段代码有问题吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心凉怎暖 2024-12-20 17:34:55

这段代码有问题吗?

bool Spellcheck::smart_comp(字符串常量值,字符串常量键){
  返回真;
}

除了它总是返回 true?是的,基本问题是成员函数有一个隐式参数 this,因此签名与预期谓词的签名不匹配。您应该使用static这个函数,甚至是一个免费函数(如果需要的话,friended)。此外,您每次都会复制字符串,最好通过 const 引用获取参数,以避免不必要的复制。

如果谓词的实际结果取决于 Spellcheck 对象的状态,则必须将该状态绑定到成员函数才能创建具有适当签名的函数对象:

std::binary_search(
    this->words.begin(), this->words.end()
  , std::bind( &Spellcheck::smart_comp, this )
);

Is there any problem with this code?

bool Spellcheck::smart_comp(string const value, string const key){
  return true;
}

Other than it always returns true? Yes, the basic problem is that a member function has an implicit parameter this, so the signature does not match that of the expected predicate. You should be doing this function static or even a free function (friended if needed). Also you are copying the strings 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:

std::binary_search(
    this->words.begin(), this->words.end()
  , std::bind( &Spellcheck::smart_comp, this )
);
微凉 2024-12-20 17:34:55

您试图传递一个非静态成员函数,该函数不可转换为所需的二进制函数(因为有三个实际参数)。

尝试将您的 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 function static. (Of course then you can't refer to instance members; if you need statefulness, you'll have to write a full functor.)

稳稳的幸福 2024-12-20 17:34:55

假设 this->words 的类型为 std::vector 并且 func的成员拼写检查,您可以通过将 smart_comp 声明为 static 来解决此问题。但我会对你的课程设计三思而后行。

Assuming this->words's type is std::vector<std::string> and func is a member of Spellcheck, you can work it around declaring smart_comp to be static. But I would think twice on your class design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文