#include <iostream>
#include <algorithm>
using namespace std;
struct arr
{
int a;int b;
}a[1000];
bool comp(arr &lhs, arr &rhs)
{ return lhs.a < rhs.a ; }
int main()
{
int n,i ;
sort(a,a+n,comp);
int ind= lower_bound(a,a+n,x,comp)-a;
return 0;
}
错误信息 :
/usr/include/c++/4.9/bits/predefined_ops.h: 在 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = arr*; _Value = 常量 int; _Compare = bool ( )(arr&, arr&)]': /usr/include/c++/4.9/bits/stl_algobase.h:965:30:
来自'_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [与 _ForwardIterator = arr ; _Tp = int; _Compare = __gnu_cxx::__ops::_Iter_comp_val]' /usr/include/c++/4.9/bits/stl_algo.h:2036:46: 来自'_FIter std::lower_bound(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = arr*; _Tp = int; _比较=布尔()(arr&, arr&)]' prog.cpp:28:38: 需要从这里 /usr/include/c++/4.9/bits/predefined_ops.h:141:37: 错误: 对类型 'arr&' 的引用的初始化无效'const int' 类型的表达式 { return bool(_M_comp( __it, __val)); } ^
我希望在结构上使用 lower_bound 来搜索等于 a[i].a 的值 x ?我已经相应地构建了比较器函数,但收到了一条很长的错误消息,我无法做出任何事情。
运行该函数需要进行哪些更改。