c++ - C++ 引用中的函数描述与可能的实现

标签 c++ standards requirements

std::lower_bound 的两个引用页中和 std::upper_bound 来自C++ reference我读过

[...] uses operator< to compare the elements, [...]

这对我来说非常有用,因为有了这些信息我就知道,即使后一个函数

Returns an iterator pointing to the first element in the range [first, last) that is greater than value

它仍然使用 operator<这样做,而不是 operator> ,因此必须为容器中存储的对象的类/类型定义前者。 可能的实现部分,行 if (!(value < *it)) { ,正好证实了这一点。

但是,例如 std::remove 的引用页面,为此我读到了

Removes all elements that are equal to value

没有提及任何oparator根本不存在,所以原则上我不知道假设在容器中存储的对象的类中定义了哪些。 可能的实现使用 operator== (参见行 if (!(*i == value)) )。

因此我的问题是:某些函数的文档页面是否故意不指定调用该函数的类必须满足的“要求”?

最佳答案

虽然 cppreference 总体上相当不错,但它是一个社区维护的项目;不是官方文档。有时还使用稍微含糊的措辞以使文本更容易理解。

对于官方的要求,我们必须转向标准。在那里,所有这些要求都明确说明:

来自[lower.bound]

Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
...
Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the range [first, i), bool(invoke(comp, invoke(proj, *j), value)) is true.

来自[upper.bound]

Let comp be less{} and proj be identity{} for overloads with no parameters by those names.
...
Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the range [first, i), !bool(invoke(comp, invoke(proj, *j), value)) is true.

来自[alg.remove]

Let E be
-- bool(*i == value) for remove,
...
Effects: Eliminates all the elements referred to by iterator i in the range [first, last) for which E holds.

这些描述没有歧义。 std::lower_boundstd::upper_bound 默认使用 std::less 进行比较,而 std::remove 使用operator==

关于c++ - C++ 引用中的函数描述与可能的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57746951/

相关文章:

没有提交按钮的 HTML 表单提交

requirements - 设计师/需求编写者的好面试问题

language-agnostic - 软件需求分析

javascript - 需要/首选事件捕获的真实世界示例?

c++ - 派生类中的抽象类实现

css - 哪个浏览器返回 SVG 元素的 getBoundingClientRect 的正确结果?

php - 函数/方法参数验证标准

c++ - 在 Ubuntu 上使用 wxWidgets2.9.3 出错

c++ - 简化的元组实现

c++ - 延迟构造函数调用