如何在集合中找到低于关键字的数字?C++
How to find number lower than key in set? C++
我是C++世界的新手。集合中有一个upper_bound函数,它返回的数字大于传递的值。我想知道是否有任何方法可以得到一个低于传递值的数字?假设我们在集合中有{3,6,8,9,11},通过传递给我的函数:func(8(,我将得到6,因为6是在8之前的数字。知道吗?
您的方法是合理的,但您应该使用std::lower_bound而不是std::upper_bound
,例如:
#include <iostream>
#include <set>
using namespace std;
int main(void)
{
set<int> myset{3, 6, 8, 9, 11};
set<int>::iterator it = myset.lower_bound(8);
if (it != myset.begin()) {
it--;
cout << *it << endl;
} else {
cout << "No smaller element found!" << endl;
}
return 0;
}
Ouptut:
6
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++中的"inline"关键字
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何确保C++函数在定义之前声明(如override关键字)
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- 在处理C 中的数字格式时,在SetPrecision中使用固定关键字
- 使用关键字从文本文件返回数字
- 仅使用关键字和数字提取来解析简单语法