简体   繁体   English

使用std :: find_if和std :: vector来查找大于某个值的最小元素

[英]Using std::find_if with std::vector to find smallest element greater than some value

I have a vector of doubles which are ordered. 我有一个有序的双打向量。

std::vector<double> vec;
for(double x{0.0}; x < 10.0 + 0.5; x += 1.0) vec.push_back(x);

I am trying to use std::find_if to find the first element, and its corresponding index, of this vector for which y < element . 我正在尝试使用std::find_if查找此向量的第一个元素及其对应的索引,其中y < element Where y is some value. y是一些值。

For example, if y = 5.5 then the relevant element is element 6.0 which has index 6 , and is the 7th element. 例如,如果y = 5.5则相关元素是元素6.0 ,索引为6 ,是第7个元素。

I suspect that a lambda function could be used do to this. 我怀疑可以使用lambda函数来做到这一点。

  • Can a lambda function be used to do this? 可以使用lambda函数执行此操作吗?

  • If so, how do I implement a find_if statement to do what I want? 如果是这样,我如何实现find_if语句来执行我想要的操作?

1 line of code is necessary, if a lambda function is used: x is a local double (value we are searching for) const double x = 5.5 如果使用lambda函数,则需要1行代码: x是本地double(我们正在搜索的值) const double x = 5.5

std::find_if(vec.cbegin(), vec.cend(), [x] (double element) { return (x < element); })

Breaking this down, we have: 分解来看,我们有:

std::find_if(vec.cbegin(), vec.cend(), lambda)

The lambda is: Lambda是:

[x] (double element) { return (x < element); }

This means: 这意味着:

  • capture the local variable x for use inside the lambda body 捕获局部变量x以在lambda体内使用
  • the lambda function takes a single double as an argument (the algorithm find_if will pass each element as it iterates over the vector to this function as element ) lambda函数将一个double find_if作为参数(算法find_if将在对向量进行迭代时将每个元素传递给此函数作为element
  • the function body returns true when the first element is found for which x < element 当找到第一个元素的x < element时,函数主体返回true

Note: I answered this question myself while researching possible solutions. 注意:我在研究可能的解决方案时亲自回答了这个问题。 The context in which I am using this in my own program is slightly different. 我在自己的程序中使用此上下文的环境略有不同。 In my context, the vector is a specification for numerical boundries, and I am looking to find lower < x < higher . 在我的上下文中,向量是数字边界的规范,我正在寻找lower < x < higher Therefore the code I used is slightly different to this, in that I had to shift my returned iterator by 1 place because of how the numerical boundries are specified. 因此,我使用的代码与此稍有不同,因为由于如何指定数字边界,我不得不将返回的迭代器移位1位。 If I introduced a bug in transcribing the solution, let me know. 如果我在转录解决方案时引入了一个错误,请告诉我。

Just use std::upper_bound() - it is more effective (it is using binary search) and does not need a lambda: 只需使用std::upper_bound() -它更有效(它正在使用二进制搜索)并且不需要lambda:

auto it = std::upper_bound( vec.begin(), vec.end(), x );

if you need to find lower < x < upper you can use std::equal_range() , but you would need additional logic to find proper lower as std::lower_bound will give element which less or equal to x , so you need to make sure lower is less than x and not equal. 如果您需要找到lower < x < upper ,则可以使用std::equal_range() ,但是您将需要其他逻辑来找到适当的lower因为std::lower_bound将给出小于或等于x元素,因此您需要确保lower值小于x且不相等。

live example 现场例子

看来您尚未被引入std::upper_bound

std::upper_bound(vec.begin(), vec.end(), x);

If you need the index, you can add a counter. 如果需要索引,可以添加一个计数器。

Something like 就像是

x = 5;

cnt = -1;

std::find_if(vec.cbegin(), vec.cend(),
   [&] (double element) { ++cnt; return (x < element); })

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

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