c++ - 为什么STL中没有std::find_not?

标签 c++

我只是想知道为什么 STL 中没有 std::find_not (取值而不是 lambda - 这与 std::find_if_not 不同)。 Boost拥有它是出于我认为它应该存在的确切原因:

The existing find variants are: find(), find_if(), and find_if_not(). It seems natural to also have find_not(), for the very reason that we have find_if_not() -- to avoid having to write a lambda to wrap the negation of the find condition.

这只是“还没有人提出”的问题还是还有其他原因?

- 迈克

最佳答案

这似乎是 find_if_not 的提案:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2569.pdf

它包括这样的理由:

Just as copy_if is the inverse of remove_copy_if, find_if_not is the inverse of find_if: it returns an iterator pointing to the first element that fails to satisfy a predicate p. It's worth adding for the same reason.

copy_if 在同一文档中提出。它包括:

It is formally redundant [...], but it's worth adding anyways. First, C++ isn't really a functional language and transforming a predicate into its negation is sometimes awkward. Second, the workaround of using double negatives is not unconfusing

因此,在不询问作者的情况下,我的最佳猜测是“添加了 copy_iffind_if_not 是为了方便使否定谓词更容易。因为find 是基于值的,作者似乎至少有点不愿意添加算法的冗余版本,find_not 没有被考虑。”

关于c++ - 为什么STL中没有std::find_not?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70174829/

相关文章:

c++ - 是否有一个跨平台的 C/C++ 库可以为我们提供 CPU 和内存使用情况统计信息?

c++ - MSBuild:VS2010 中 native C++ 项目的 Custom.After.Microsoft.Common.targets

c++ - 如果没有使用带有虚函数的类,是否会生成虚函数表?

c++ - 发布和调试之间调用堆栈的奇怪差异

c++ - 以类似 cout 的符号在 C++ 中管理日志流

c++ - '+' 不能添加两个指针,而只是打印一个 int 和一个显式字符串?

面向.NET Core 3.1的C++/CLI

c++ - 是否可以编写一个像 next_permutation 这样的函数,但它只排列 r 值,而不是 n 值?

c++ - SSE - 从 double 到 char 的 AVX 转换

c++ - 如何在 std::deque 中避免 `deque iterator not dereferencable`?锁?