std::experi...nd_first_of(3)
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 plainstd::yet, and that's the vibe π§ͺ
- Searches the range
[first1, last1)for any of the elements in the range[first2, last2), after projecting the ranges withproj1andproj2respectively. The projected elements are compared usingpred. - Same as (1), but uses
r1as the first source range andr2as the second source range, as if usingranges::begin(r1)asfirst1,ranges::end(r1)aslast1,ranges::begin(r2)asfirst2, andranges::end(r2)aslast2.
π Parameters π
first1,last1β the range of elements to examiner1β the range of elements to examinefirst2,last2β the range of elements to search forr2β the range of elements to search forpredβ predicate to use to compare the projected elements withproj1β projection to apply to the elements in the first rangeproj2β 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms