std::lexico...e_three_way(3)
π NAME π
Okay so bestie, welcome to the <pink>std::lexicographical_compare_three_way</pink> page β this is the <pink>std::lexico...e_three_way(3)</pink> manual entry from the C++ Standard Library, section 3. It's literally the function that lexicographically compares two ranges using three-way comparison. No cap. π
π Synopsis π
Defined in header <algorithm>.
Overload (1) β since C++20:
template< class InputIt1, class InputIt2, class Cmp >
constexpr auto lexicographical_compare_three_way
( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2,
Cmp comp ) -> decltype(comp(*first1, *first2));
Overload (2) β since C++20:
template< class InputIt1, class InputIt2 >
constexpr auto lexicographical_compare_three_way
( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 );
Lexicographically compares two ranges [first1, last1) and [first2, last2) using three-way comparison and produces a result of the strongest applicable comparison category type. Slay.
- Returns the order between the first non-equivalent pair of elements according to
compin both ranges if any, otherwise (if one ranges is equivalent to the prefix of another according tocomp), returns the order between the length of both ranges. - Equivalent to
return std::lexicographical_compare_three_way( first1, last1, first2, last2, std::compare_three_way());
If the return type is not one of the three comparison category types, the program is ill-formed:
std::strong_orderingstd::weak_orderingstd::partial_ordering
π Parameters π
first1,last1β the first range of elements to examinefirst2,last2β the second range of elements to examinecompβ a function object
π§ Type requirements π§
InputIt1,InputIt2must meet the requirements ofLegacyInputIterator.
π Return value π
The value of a comparison category type specified above.
β±οΈ Complexity β±οΈ
Given N1 as std::distance(first1, last1) and N2 as std::distance(first2, last2):
- At most
min(N1,N2)applications ofcomp. - At most
min(N1,N2)applications ofstd::compare_three_way().
π οΈ Possible implementation π οΈ
template<class I1, class I2, class Cmp>
constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp comp)
-> decltype(comp(*f1, *f2))
{
using ret_t = decltype(comp(*f1, *f2));
static_assert(std::disjunction_v<
std::is_same<ret_t, std::strong_ordering>,
std::is_same<ret_t, std::weak_ordering>,
std::is_same<ret_t, std::partial_ordering>>,
"The return type must be a comparison category type.");
bool exhaust1 = (f1 == l1);
bool exhaust2 = (f2 == l2);
for (; !exhaust1 && !exhaust2; exhaust1 = (++f1 == l1), exhaust2 = (++f2 == l2))
if (auto c = comp(*f1, *f2); c != 0)
return c;
return !exhaust1 ? std::strong_ordering::greater:
!exhaust2 ? std::strong_ordering::less:
std::strong_ordering::equal;
}
π Example π
Run this code:
#include <algorithm>
#include <cctype>
#include <compare>
#include <iomanip>
#include <iostream>
#include <string_view>
#include <utility>
using namespace std::literals;
void show_result(std::string_view s1, std::string_view s2, std::strong_ordering o)
{
std::cout << std::quoted(s1) << " is ";
std::is_lt(o) ? std::cout << "less than ":
std::is_gt(o) ? std::cout << "greater than ":
std::cout << "equal to ";
std::cout << std::quoted(s2) << '\n';
}
std::strong_ordering cmp_icase(unsigned char x, unsigned char y)
{
return std::toupper(x) <=> std::toupper(y);
};
int main()
{
for (const auto& [s1, s2] :
{
std::pair{"one"sv, "ONE"sv}, {"two"sv, "four"sv}, {"three"sv, "two"sv}
})
{
const auto res = std::lexicographical_compare_three_way(
s1.cbegin(), s1.cend(), s2.cbegin(), s2.cend(), cmp_icase);
show_result(s1, s2, res);
}
}
π Output π
"one" is equal to "ONE"
"two" is greater than "four"
"three" is less than "two"
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 3410β applied toC++20; behavior as published: extraneous comparisons between iterators were required; correct behavior: such requirement removed.
π See also π
lexicographical_compareβ returns true if one range is lexicographically less than another (function template)compare_three_way(C++20) β constrained function object implementingx <=> y(class)ranges::lexicographical_compare(C++20) β returns true if one range is lexicographically less than another (niebloid)
This page came from <pink>http://cppreference.com</pink>, version <pink>2024.06.10</pink>, and it's the <pink>std::lexico...e_three_way(3)</pink> manual entry. Bye bestie, go compare some ranges! π
β¨π¦
π¦ 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