std::experi...nges::search(3)
π std::experimental::ranges::search π
okay so bestie, welcome to the std::experi...nges::search(3) page from the C++ Standard Libary (yes, that spelling is literally what the header says, we donβt make the rules π
). this one is all about hunting down one sequence inside another, and itβs giving βwhere is that subsequence at?β energy. no cap.
πΈ NAME πΈ
std::experimental::ranges::search - std::experimental::ranges::search
β¨ Synopsis β¨
Defined in header <experimental/ranges/algorithm> β and hereβs the whole declaration moment, literally:
Defined in header <experimental/ranges/algorithm>
template< ForwardIterator I1, Sentinel<I1> S1,
ForwardIterator I2, Sentinel<I2> S2, class Pred =
ranges::equal_to<>,
class Proj1 = ranges::identity, class Proj2 =
ranges::identity >
(1) (ranges TS)
requires IndirectlyComparable<I1, I2, Pred, Proj1, Proj2>
I1 search( I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = Pred{}, Proj1 proj1 = Proj1{}, Proj2 proj2 =
Proj2{} );
template< ForwardRange R1, ForwardRange R2, class Pred =
ranges::equal_to<>,
class Proj1 = ranges::identity, class Proj2 =
ranges::identity >
requires IndirectlyComparable<ranges::iterator_t<R1>,
ranges::iterator_t<R2>,
(2) (ranges TS)
Pred, Proj1, Proj2>
ranges::safe_iterator_t<R1> search( R1&& r1, R2&& r2, Pred pred =
Pred{},
Proj1 proj1 = Proj1{}, Proj2
proj2 = Proj2{} );
- It searches for the first occurrence of the sequence of elements
[first2, last2)in the range[first1, last1). Elements are compared usingpredafter being projected withproj2andproj1, respectively. π - Same as (1), but it 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. π«Ά
β οΈ notwithstanding the declarations depicted above, the actual number and order of template parameters for algorithm declarations is unspecified. So if explicit template arguments are used when calling an algorithm, the program is probably non-portable. thatβs the tea. β
π 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 apply to the projected elementsproj1β projection to apply to the elements in the first rangeproj2β projection to apply to the elements in the second range
π«Ά Return value π«Ά
An iterator to the beginning of the first occurrence of the sequence [first2, last2) in the range [first1, last1).
If [first2, last2) is empty, first1 is returned. If no such occurrence is found, an iterator that compares equal to last1 is returned. periodt. π
π₯ Complexity π₯
At most S * N applications of the predicate and each projection, where S = last2 - first2 and N = last1 - first1. thatβs the budget, bestie β donβt blow it. β
π§ Possible implementation π§
template<ForwardIterator I1, Sentinel<I1> S1,
ForwardIterator I2, Sentinel<I2> S2, class Pred =
ranges::equal_to<>,
class Proj1 = ranges::identity, class Proj2 =
ranges::identity>
requires IndirectlyComparable<I1, I2, Pred, Proj1, Proj2>
I1 search(I1 first1, S1 last1, I2 first2, S2 last2,
Pred pred = Pred{}, Proj1 proj1 = Proj1{}, Proj2 proj2 =
Proj2{})
{
for (; ; ++first1)
{
I1 it = first1;
for (I2 it2 = first2; ; (void)++it, (void)++it2)
{
if (it2 == last2)
return first1;
if (it == last1)
return it;
if (!ranges::invoke(pred, ranges::invoke(proj1, *it),
ranges::invoke(proj2, *it2)))
break;
}
}
}
π Example π
This section is incomplete.
Reason: no example.
π weβre manifesting an example for the next update, fr fr.
π See also π
searchβ searches for a range of elements (function template)find_endβ finds the last sequence of elements in a certain range (function template)includesβ returns true if one set is a subset of another (function template)equalβ determines if two sets of elements are the same (function template)findβ finds the first element satisfying specific criteriafind_ifβ (function template)find_if_notβ see-also entry in the same cluster; this page doesnβt give it a separate descriptionlexicographical_compareβ returns true if one range is lexicographically less than another (function template)mismatchβ finds the first position where two ranges differ (function template)search_nβ searches for a number consecutive copies of an element in a range (function template)
π Category π
* Todo no example
π source/version: http://cppreference.com, dated 2024.06.10 β bye bestie, go find those subsequences! π
π¦ 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