[libcxx-commits] [libcxx] Optimize the implementation of std::find_first_of to use any_of #129319 (PR #129574)
Ali Raeisdanaei via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Apr 19 16:23:17 PDT 2025
================
@@ -26,11 +26,9 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _ForwardIterator1 __find_fir
_ForwardIterator2 __first2,
_ForwardIterator2 __last2,
_BinaryPredicate&& __pred) {
- for (; __first1 != __last1; ++__first1)
- for (_ForwardIterator2 __j = __first2; __j != __last2; ++__j)
- if (__pred(*__first1, *__j))
- return __first1;
- return __last1;
+ return std::find_if(first1, last1, [&](const auto& x) {
+ return std::any_of(first2, last2, [&](const auto& y) { return x == y; });
----------------
aliraeisdanaei wrote:
You are right `std::find` and `std::any_of` have the same function; however, `std::find` returns an iterator, and `std::any_of` returns a predicate which is needed in the `std::find_any`.
https://github.com/llvm/llvm-project/pull/129574
More information about the libcxx-commits
mailing list