简体   繁体   English

集合中的lower_bound(C ++)

[英]lower_bound in set (C++)

I've got a set and I want to find the largest number not greater than x in it. 我有一套,我想找到不大于x的最大数字。 (something like lower_bound(x) ) how should i do it? (像lower_bound(x)这样的东西)我应该怎么做? Is there any predefined functions? 有预定义的功能吗?

set<int> myset;
myset.insert(blahblahblah);
int y;
//I want y to be greatest number in myset not greater than x

You can use upper_bound like this: upper_bound(x)-- . 您可以像这样使用upper_boundupper_bound(x)-- Upper bound gives you the first element greater than x , so the element you seek is the one before that. 上限为您提供大于x的第一个元素,因此您寻找的元素就是之前的元素。 You need a special case if upper_bound returns begin() . 如果upper_bound返回begin()则需要一个特殊情况。

In addition to lower_bound there is also upper_bound C++ reference 除了lower_bound之外,还有upper_bound C ++引用

The function returns an iterator to the first value that is strictly greater than yours. 该函数将迭代器返回到严格大于您的第一个值。 If it returns begin() then all of them are, otherwise subtract one from the resulting iterator to get the value you are looking for. 如果它返回begin(),那么所有它们都是,否则从结果迭代器中减去一个以获得你正在寻找的值。

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

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