std::experi...nd_first_of(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

okay bestie, welcome to the std::experi...nd_first_of(3) page from the C++ Standard Libary manual (yes, that's literally how it's spelled in the header, no cap 😭). this is the ranges TS version of find_first_of, so it lives in std::experimental::ranges and it's giving experimental energy πŸ§ͺ✨ it's here to help you find the first element in one range that matches any element from another range. periodt. let's go πŸ’…

✨ NAME ✨

std::experimental::ranges::find_first_of β€” std::experimental::ranges::find_first_of

that's the whole name, bestie β€” it's a mouthful but we love her πŸ’–

πŸ“œ Synopsis πŸ“œ

          Defined in header <experimental/ranges/algorithm>
          template< InputIterator I1, Sentinel<I1> S1, ForwardIterator I2,
          Sentinel<I2> S2,

                    class Proj1 = ranges::identity, class Proj2 =
          ranges::identity,
                    IndirectRelation<projected<I1, Proj1>,
       (1) (ranges TS)
                                     projected<I2, Proj2>> Pred =
          ranges::equal_to<> >
          I1 find_first_of( I1 first1, S1 last1, I2 first2, S2 last2, Pred
          pred = Pred{},

                            Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
          template< InputRange R1, ForwardRange R2,

                    class Proj1 = ranges::identity, class Proj2 =
          ranges::identity,
                    IndirectRelation<projected<ranges::iterator_t<R1>, Proj1>,
                                     projected<ranges::iterator_t<R2>, Proj2>>
       (2) (ranges TS)
          Pred = ranges::equal_to<> >
          ranges::safe_iterator_t<R1> find_first_of( R1&& r1, R2&& r2, Pred
          pred = Pred{},

                                                     Proj1 proj1 = Proj1{},
          Proj2 proj2 = Proj2{} );
this is experimental ranges TS energy, so it's (ranges TS) β€” not plain std:: yet, and that's the vibe πŸ§ͺ
  1. Searches the range [first1, last1) for any of the elements in the range [first2, last2), after projecting the ranges with proj1 and proj2 respectively. The projected elements are compared using pred.
  2. Same as (1), but uses r1 as the first source range and r2 as the second source range, as if using ranges::begin(r1) as first1, ranges::end(r1) as last1, ranges::begin(r2) as first2, and ranges::end(r2) as last2.

πŸŽ€ Parameters πŸŽ€

  • first1, last1 β€” the range of elements to examine
  • r1 β€” the range of elements to examine
  • first2, last2 β€” the range of elements to search for
  • r2 β€” the range of elements to search for
  • pred β€” predicate to use to compare the projected elements with
  • proj1 β€” projection to apply to the elements in the first range
  • proj2 β€” projection to apply to the elements in the second range

the way these parameter names are so descriptive, we stan 🫢

πŸ’– Return value πŸ’–

Iterator to the first element in the range [first1, last1) that is equal to an element from the range [first2, last2) after projection. If no such element is found, an iterator comparing equal to last1 is returned.

so if it's a no-match, you get an iterator comparing equal to last1 β€” that's your "nothing found" signal, bestie πŸ’…

⚑ Complexity ⚑

At most (S * N) applications of the predicate and each projection, where S = distance(last2 - first2) and N = last1 - first1.

so it's at most that many applications β€” keep that formula in your notes, iykyk πŸ“

πŸ› οΈ Possible implementation πŸ› οΈ

          template<InputIterator I1, Sentinel<I1> S1, ForwardIterator I2,
       Sentinel<I2> S2,
                   class Proj1 = ranges::identity, class Proj2 =
       ranges::identity,
                   IndirectRelation<projected<I1, Proj1>,
                                    projected<I2, Proj2>> Pred =
       ranges::equal_to<>>
          I1 find_first_of(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred
       = Pred{},
                           Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{})
          {
              for (; first1 != last1; ++first1)
                  for (ForwardIt it = first2; it != last2; ++it)
                      if (ranges::invoke(pred, ranges::invoke(proj1, *first1),
                                               ranges::invoke(proj2, *it)))
                          return first1;
              return first1;
          }

this is how it could look under the hood β€” not the only way, but the vibe ✨ and yes, copy it exactly as it appears, bestie β€” including ForwardIt in the inner loop, because that's literally what the page has πŸ™ˆ

🌈 Example 🌈

This section is incomplete Reason: no example

😭 not a single example, but that's what the page says. if you write one, you'd be the main character.

πŸ‘€ See also πŸ‘€

  • find_first_of β€” searches for any one of a set of elements (function template)
  • find β€” finds the first element satisfying specific criteria (function template)
  • find_if β€” (function template)
  • find_if_not

πŸ—‚οΈ Category πŸ—‚οΈ

  • Todo no example

okay bestie, that's the whole page πŸ’– this gem is from http://cppreference.com, version 2024.06.10 β€” stay slay, and happy range-searching βœ¨πŸ’…

πŸ“¦ where this page comes from