【问题标题】:C++ Primer (5th edition); Ch 19 - Algorithms: std::lower_boundC++ 入门(第 5 版);第 19 章 - 算法:std::lower_bound
【发布时间】:2021-09-12 21:46:48
【问题描述】:

这是来自C++ Primer (5th edition);第 19 章“附录:算法”:

lower_bound(beg, end, val)
lower_bound(beg, end, val, comp)

返回一个表示第一个元素的迭代器,使得val 不小于该元素,如果不存在这样的元素,则返回end

upper_bound(beg, end, val)
upper_bound(beg, end, val, comp)

返回一个表示第一个元素的迭代器

  • 但我认为lower_bound返回一个迭代器,表示输入序列中的第一个元素不小于val(大于或等于val)而不是相反(“...第一个元素使得@987654328 @ 不小于那个元素”)。是不是书上写错了?

【问题讨论】:

  • 好像是这样。您是否尝试过通过电子邮件向作者提交勘误表?
  • en.cppreference.com/w/cpp/algorithm/lower_bound 准确地说出你的话,所以可能是错字,是的
  • @selbie:我该怎么办?
  • 联系编辑。
  • @Max:好的,谢谢

标签: c++ algorithm language-lawyer lower-bound


【解决方案1】:

是不是书上写错了?

如果你信任cppreference,那么:是的,这是一个错误:

std::lower_bound

返回一个迭代器,该迭代器指向[first, last) 范围内不小于(即大于或等于)值的第一个元素,如果没有找到这样的元素,则返回最后一个元素。

或者,如果您信任该网站,this Draft C++17 Standard 有:

28.7.3.1 lower_bound       [lower.bound]



2     返回: 范围内最远的迭代器 i [first, last] 使得对于范围内的每个迭代器 j [first, i)以下对应条件成立:*j < valuecomp(*j, value) != false

this (later) online Draft Standard 中,它是§25.8.4.2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2021-08-25
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多