在 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 thanvalue
它仍然使用 operator<
这样做,而不是 operator>
,因此必须为容器中存储的对象的类/类型定义前者。 可能的实现部分,行 if (!(value < *it)) {
,正好证实了这一点。
但是,例如 std::remove
的引用页面,为此我读到了
Removes all elements that are equal to
value
没有提及任何oparator
根本不存在,所以原则上我不知道假设在容器中存储的对象的类中定义了哪些。 可能的实现使用 operator==
(参见行 if (!(*i == value))
)。
因此我的问题是:某些函数的文档页面是否故意不指定调用该函数的类必须满足的“要求”?
最佳答案
虽然 cppreference 总体上相当不错,但它是一个社区维护的项目;不是官方文档。有时还使用稍微含糊的措辞以使文本更容易理解。
对于官方的要求,我们必须转向标准。在那里,所有这些要求都明确说明:
Let
comp
beless{}
andproj
beidentity{}
for overloads with no parameters by those names.
...
Returns: The furthermost iteratori
in the range[first, last]
such that for every iteratorj
in the range[first, i)
,bool(invoke(comp, invoke(proj, *j), value))
istrue
.
Let
comp
beless{}
andproj
beidentity{}
for overloads with no parameters by those names.
...
Returns: The furthermost iteratori
in the range[first, last]
such that for every iteratorj
in the range[first, i)
,!bool(invoke(comp, invoke(proj, *j), value))
istrue
.
Let E be
--bool(*i == value)
forremove
,
...
Effects: Eliminates all the elements referred to by iteratori
in the range[first, last)
for which E holds.
这些描述没有歧义。 std::lower_bound
和 std::upper_bound
默认使用 std::less
进行比较,而 std::remove
使用operator==
。
关于c++ - C++ 引用中的函数描述与可能的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57746951/