简体   繁体   English

在 C++ 中对 Structure 使用 lower_bound 或 upper_bound

[英]using lower_bound or upper_bound on Structure in C++

#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;
}

Error message :错误信息:

/usr/include/c++/4.9/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = arr*; /usr/include/c++/4.9/bits/predefined_ops.h:在 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = arr*; _Value = const int; _Value = const int; _Compare = bool ( )(arr&, arr&)]': /usr/include/c++/4.9/bits/stl_algobase.h:965:30: _Compare = bool ( )(arr&, arr&)]': /usr/include/c++/4.9/bits/stl_algobase.h:965:30:
required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = arr ;来自 '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = arr ; _Tp = int; _Tp = 整数; _Compare = __gnu_cxx::__ops::_Iter_comp_val]' /usr/include/c++/4.9/bits/stl_algo.h:2036:46: required from '_FIter std::lower_bound(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = arr*; _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) [与_FIter = arr*; _Tp = int; _Tp = 整数; _Compare = bool ( )(arr&, arr&)]' prog.cpp:28:38: required from here /usr/include/c++/4.9/bits/predefined_ops.h:141:37: error: invalid initialization of reference of type 'arr&' from expression of type 'const int' { return bool(_M_comp( __it, __val)); _Compare = bool ( )(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)); } ^ } ^

I wish to use lower_bound over the struct to search for the value x that equals to a[i].a ?我希望在结构上使用 lower_bound 来搜索等于 a[i].a 的值 x ? I have constructed the comparator function accordingly but getting a long error message of which I am not able to make anything.我已经相应地构建了比较器函数,但收到了一条很长的错误消息,我对此无能为力。

What changes are required for the function to run.函数运行需要哪些更改。

lower_bound returns an iterator, not an index of the element found. lower_bound 返回一个迭代器,而不是找到的元素的索引。 You'll need to use std::distance to retrieve the index, but usually, the iterator is what you need/want for further processing.您需要使用 std::distance 来检索索引,但通常,迭代器是您需要/想要进一步处理的。

Also note an index is usually returned as an std::size_t, not an int as you seem to assume.另请注意,索引通常作为 std::size_t 返回,而不是您认为的 int 。

As pointed in previous answers, lower_bound returns an iterator.正如之前的答案所指出的, lower_bound返回一个迭代器。
The type of x in lower_bound is not clear, x should also be the same type as that of the type in the container and compare function. lower_boundx的类型不清楚, x也应该与容器和比较函数中的类型相同。
In this case, the type of x should be arr .在这种情况下, x的类型应该是arr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM