我的代码中的binary_search谓词有什么问题
What is wrong with the binary_search predicate in my code
我正在阅读有关binary_search的信息,然后我尝试使用谓词来实现它。这是我的代码(我还包含了我正在使用的排序谓词)。我知道小于是默认值。这是粗略的测试代码
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;
};
实现
person p;
p.age = 24;
std::vector<person> vec;
vec.push_back(p);
std::sort(vec.begin(),vec.end(),less_than_key());
std::binary_search(vec.begin(),vec.end(),bsearch_predicate(24));
现在这里的binary_search会产生错误,但是如果我尝试使用 std::find_if
std::find_if(vec.begin(),vec.end(),bsearch_predicate(24));
以上作品。如果有人能告诉我为什么我的代码中出现 std::binary_search 的链接器错误,我将不胜感激。链接器错误包括:
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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 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.0vcincludealgorithm 2978 1
binary_search
函数不接受相等运算符,而是采用定义顺序的不等式迭代器。您必须对谓词进行建模,就好像它是一个小于比较一样。
原因是binary_search
算法需要知道参数的相对顺序和它正在查看的元素,以决定继续搜索的方向。当pred(*it,value)
和pred(value,*it)
都不为真时,将认为已找到该元素(如果两个值都不小于另一个值,则它们是相同的)
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- std::condition_variable::wait()如何评估给定的谓词
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 什么 STL 算法可以确定容器中的一个项目是否满足谓词?
- 我实现谓词的方法有什么问题
- C++中的谓词是什么?
- 我的谓词函数有什么问题?
- 谓词和元音符有什么区别?
- 我的代码中的binary_search谓词有什么问题
- 替换由谓词确定的 std::string 中的字符的最 stl-ish 方法是什么
- 什么时候可以在没有谓词的情况下使用 std::condition_variable