我只是想知道为什么 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_if
和 find_if_not
是为了方便使否定谓词更容易。因为find
是基于值的,作者似乎至少有点不愿意添加算法的冗余版本,find_not
没有被考虑。”
关于c++ - 为什么STL中没有std::find_not?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70174829/