lower_bound
编辑 讨论
本词条缺少概述图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧!
lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个不小于value 的值。该函数为C++ STL内的函数。
中文名
lower_bound
函数原型
template<classForward
举 例
vector<int>nums;
注意事项
必须确定序列为有序序列
目录
- ▪ 第二个版本
C++ STL
编辑
函数原型
第一个版本(default):
1 |
|
1 |
|
函数介绍
例如,有如下序列:
a[i]={12,15,17,19,20,22,23,26,29,35,40,51};
用值21调用lower_bound(),返回一个指向22的iterator。用值22调用lower_bound(),也返回一个指向22的iterator。第一个版本使用底层 < (小于)操作符,第二个版本根据comp进行排序和比较。
注意事项
调用lower_bound之前必须确定序列为有序序列,否则调用出错。第一个版本排序根据底层的 <(小于)操作符,第二个版本根据comp进行排序。
举例
编辑
第一个版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
输出:
Before nums is: -242 -1 0 5 8 8 11
After, nums is: -242 -1 0 5 7 8 8 11
第二个版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
iter指向person(12, 96.8, "你好")这一条记录。读者也可以更改compareByTwoKey为compareByThreeKey。