C++ 我的代码中的二进制搜索谓词有什么问题

C++ 我的代码中的二进制搜索谓词有什么问题,c++,stl,C++,Stl,我正在阅读关于二进制搜索的文章,然后我尝试使用谓词来实现它。这是我的代码(我还包括了我正在使用的排序谓词)。我知道小于是默认值。这是粗略的测试代码 class person { public: int age; }; //sort predicate class less_than_key { public: inline bool operator()(const person& pa , const person& pb) { ret

我正在阅读关于二进制搜索的文章,然后我尝试使用谓词来实现它。这是我的代码(我还包括了我正在使用的排序谓词)。我知道小于是默认值。这是粗略的测试代码

class person
{
public:
    int age;
};

//sort predicate
class less_than_key
{
public:
    inline bool operator()(const person& pa , const person& pb)
    {
        return (pa.age < pb.age);
    }
};

//Binary Search predicate
class bsearch_predicate
{
    public:
    bool operator()(const person& pa)
    {
        return pa.age == n;
    }
    bsearch_predicate(int i):n(i) {}
    int n;
};
以上工作。如果有人能告诉我为什么我的代码中的std::binary_搜索出现链接器错误,我将不胜感激。链接器错误包括:

Error   12  error C2676: binary '<' : 'const bsearch_predicate' does not define this operator or a conversion to a type acceptable to the predefined operator   d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   4   error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const bsearch_predicate'  d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   10  error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const bsearch_predicate' d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   7   error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const bsearch_predicate'  d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   3   error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const bsearch_predicate'    d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   5   error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const bsearch_predicate'  d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   1   error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const bsearch_predicate' d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   2   error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const bsearch_predicate'    d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   11  error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const bsearch_predicate'   d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   9   error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const bsearch_predicate'   d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   8   error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const bsearch_predicate' d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   6   error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const bsearch_predicate'   d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1

Error 12 Error C2676:binary'该
binary\u search
函数不采用等式运算符,而是采用定义顺序的不等式迭代器。您必须将谓词建模为小于比较


原因是
binary_search
算法需要知道参数的相对顺序和它正在查看的元素,以决定继续搜索的方向。当
pred(*it,value)
pred(value,*it)
均不为真时(如果两个值都不小于另一个值,则它们是相同的)

我的谓词已在进行小于的比较,则认为找到了该元素?我在这里遗漏了什么吗?@Rajeshwar:不,你没有:
boolbsearch_谓词::operator()(const person&pa){return pa.age==n;}
基本上你需要使用传递给
sort
的相同谓词进行
二进制搜索。
std::find_if(vec.begin(),vec.end(),bsearch_predicate(24));
Error   12  error C2676: binary '<' : 'const bsearch_predicate' does not define this operator or a conversion to a type acceptable to the predefined operator   d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   4   error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const bsearch_predicate'  d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   10  error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const bsearch_predicate' d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   7   error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const bsearch_predicate'  d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   3   error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const bsearch_predicate'    d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   5   error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const bsearch_predicate'  d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   1   error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const bsearch_predicate' d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   2   error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const bsearch_predicate'    d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   11  error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const bsearch_predicate'   d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   9   error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const bsearch_predicate'   d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   8   error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const bsearch_predicate' d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1
Error   6   error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'const bsearch_predicate'   d:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    2978    1