< cpp‎ | algorithm
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Concepts and utilities: std::Sortable, std::projected, ...
Constrained algorithms: std::ranges::copy, std::ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations

Modifying sequence operations
Operations on uninitialized storage
Partitioning operations
Sorting operations
Binary search operations
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Numeric operations
C library
Defined in header <algorithm>
template< class InputIt1, class InputIt2, class Cmp >

constexpr auto lexicographical_compare_3way( InputIt1 first1, InputIt1 last1,
                                             InputIt2 first2, InputIt2 last2,
                                             Cmp comp)

-> std::common_comparison_category_t<decltype(comp(*first1, *first2)), std::strong_ordering>;
(1) (since C++20)
template< class InputIt1, class InputIt2 >

constexpr auto lexicographical_compare_3way( InputIt1 first1, InputIt1 last1,

                                             InputIt2 first2, InputIt2 last2);
(2) (since C++20)

Lexicographically compares two ranges [first1, last1) and [first2, last2) using three-way comparison and produces a result of the strongest applicable comparison category type.

1) Behaves as if defined as follows:
for ( ; first1 != last1 && first2 != last2; void(++first1), void(++first2) )
  if (auto cmp = comp(*first1,*first2); cmp != 0)
    return cmp;
  return first1 != last1 ? std::strong_ordering::greater :
         first2 != last2 ? std::strong_ordering::less :
2) Behaves as if defined as follows:
return std::lexicographical_compare_3way(first1, last1, first2, last2,
           [](const auto& t, const auto& u) {
               return std::compare_3way(t, u);


first1, last1 - the first range of elements to examine
first2, last2 - the second range of elements to examine
comp - a function object type. The behavior is undefined if its return type is not one of the five comparison category types (strong_equality, weak_equality, strong_ordering, weak_ordering, or partial_ordering)
Type requirements
InputIt1, InputIt2 must meet the requirements of LegacyInputIterator.

Return value

A comparison category type as defined above.


See also

compares two values using three-way comparison
(function template)