c++ - 未排序向量上的 lower_bound/upper_bound
问题描述
const std::vector<int> v = {5, 7, 3, 6, 5, 4, 7, 8, 5, 6};
auto low = std::lower_bound( v.begin(), v.end(), 7);
auto high = std::upper_bound( v.begin(), v.end(), 7);
std::cout << low - v.begin() << " " << high - v.begin();
因此,当我尝试在我的 Mac 上使用 clang++ 编译器编译此代码时,它会将输出返回为
10 10
这意味着v.end()
高和低虽然低应该是 = 1 和高 = 7(数字 8)。我究竟做错了什么?
解决方案
std::lower_bound
并std::upper_bound
要求范围是“排序的”(实际上根据谓词和给定值进行分区),这不是你的情况。
推荐阅读
- ggplot2 - ggplot - 行排序,一行在另一行之上
- kotlin - 不能用 mockk 模拟 com.github.kittinunf.fuel.Fuel
- google-apps-script - 谷歌工作表脚本类型错误:无法从未定义中读取属性“源”。(第 3 行,文件“代码副本”)
- python-3.x - cvxpy.error.SolverError:解算器CBC未安装在Windows 10 python中?
- git - 推送时“远程主机关闭与 github.com 的连接”
- javascript - 如何在angularjs中清除uib-typeahead下拉列表
- sql - HIVE SQL 中的 REGEXP_SUBSTR 等效项?
- laravel - 如何检查不记名令牌与数据库中的令牌圣所相同
- azure - 在 ADF V2 中运行 cosmos 范围脚本
- python - 请问这个程序的错误在哪里?