std::experi...cal_compare(3)
π std::experimental::ranges::lexicographical_compare(3) π
okay so bestie, we are deep in the C++ Standard Libary manual, and this page is serving std::experimental::ranges::lexicographical_compare realness. Yes, the name is long enough to need its own snack break. π
πΈ NAME πΈ
std::experimental::ranges::lexicographical_compare β std::experimental::ranges::lexicographical_compare
(not me giving you the same name twice, it's just built like that π )
β¨ Synopsis β¨
Defined in header <experimental/ranges/algorithm> β write that down, bestie. π
this overload is (1) (ranges TS):
template< InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2,
class Proj1 = ranges::identity, class Proj2 = ranges::identity,
class Comp = ranges::less<> >
requires IndirectStrictWeakOrder<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
bool lexicographical_compare( I1 first1, S1 last1, I2 first2, S2 last2,
Comp comp = Comp{},
Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
and this overload is (2) (ranges TS):
template< InputRange R1, InputRange R2,
class Proj1 = ranges::identity, class Proj2 = ranges::identity,
class Comp = ranges::less<> >
requires IndirectStrictWeakOrder<Comp,
projected<ranges::iterator_t<R1>, Proj1>,
projected<ranges::iterator_t<R2>, Proj2>>
bool lexicographical_compare( R1&& r1, R2&& r2, Comp comp = Comp{},
Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{} );
- Checks if the first range
[first1, last1)is lexicographically less than the second range[first2, last2). Elements are compared using the given binary comparison functioncomp, after being projected withproj1andproj2respectively. π₯ - 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. π¦
Lexicographical comparison is an operation with the following properties:
- Two ranges are compared element by element.
- The first mismatching element defines which range is lexicographically less or greater than the other.
- If one range is a prefix of another, the shorter range is lexicographically less than the other.
- If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.
- An empty range is lexicographically less than any non-empty range.
- Two empty ranges are lexicographically equal.
π«Ά Parameters π«Ά
first1,last1β the first range of elements to examiner1β the first range of elements to examinefirst2,last2β the second range of elements to examiner2β the second range of elements to examinecompβ comparison function 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 π
true if the first range is lexicographically less than the second.
That's it. That's the tea. π«
π§ Complexity π§
At most 2Β·min(N1, N2) applications of the comparison operation, where N1 = last1 - first1 and N2 = last2 - first2.
π Possible implementation π
template<InputIterator I1, Sentinel<I1> S1, InputIterator I2, Sentinel<I2> S2,
class Proj1 = ranges::identity, class Proj2 = ranges::identity,
class Comp = ranges::less<>>
requires IndirectStrictWeakOrder<Comp, projected<I1, Proj1>, projected<I2, Proj2>>
bool lexicographical_compare(I1 first1, S1 last1, I2 first2, S2 last2,
Comp comp = Comp{},
Proj1 proj1 = Proj1{}, Proj2 proj2 = Proj2{})
{
for (; (first1 != last1) && (first2 != last2); (void) ++first1, (void) ++first2)
{
if (ranges::invoke(comp, ranges::invoke(proj1, *first1),
ranges::invoke(proj2, *first2)))
return true;
if (ranges::invoke(comp, ranges::invoke(proj2, *first2),
ranges::invoke(proj1, *first1)))
return false;
}
return (first1 == last1) && (first2 != last2);
}
π Example π
This section is incomplete.
Reason: no example.
π See also π
lexicographical_compareβ returnstrueif one range is lexicographically less than another (function template)equalβ determines if two sets of elements are the same (function template)
π Category π
Todo no example
π source: http://cppreference.com Β· version/date: 2024.06.10 Β· footer page: std::experi...cal_compare(3) π
π¦ 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