C++ std::lexicographical_compare_three_way() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++20

Definition

In C++20, std::lexicographical_compare_three_way() compares two ranges and returns an ordering object instead of a simple bool. The result tells you if the first range is less, equal, or greater.

It uses the same dictionary rule as std::lexicographical_compare() but gives all three answers in one call. Include <compare> too when you use the result helpers.

Syntax

Add #include <algorithm>, then call std::lexicographical_compare_three_way(). Start with these two lines.

C++
auto r = std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end());
std::is_lt(r), std::is_eq(r), std::is_gt(r)
CallWhat it does
auto r = std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end());Get an ordering such as std::strong_ordering.
std::is_lt(r), std::is_eq(r), std::is_gt(r)Turn the ordering into a simple yes or no.

Available since C++20. Compile with -std=c++20 or newer.

Library declarations
C++
template<class InputIt1, class InputIt2>
auto lexicographical_compare_three_way(InputIt1 first1, InputIt1 last1,
                                       InputIt2 first2, InputIt2 last2);

Parameters

ParameterDescription
first1, last1Required. The first range.
first2, last2Required. The second range.
compOptional. A three-way comparison function. The default is std::compare_three_way.

Return Value

Return ValueDescription
LessThe first range is smaller (std::is_lt(r) is true).
EqualBoth ranges match (std::is_eq(r) is true).
GreaterThe first range is bigger (std::is_gt(r) is true).

C++ Examples

Example 1 — First is smaller

Use is_lt to test the result.

C++
#include <algorithm>
#include <compare>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> a = {1, 2, 3};
    std::vector<int> b = {1, 2, 4};

    auto r = std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end());
    if (std::is_lt(r)) {
        std::cout << "a is less than b\n";
    }
    return 0;
}

How It Works

  1. The call returns an ordering object, not a bool.
  2. The third elements decide it: 3 comes before 4.
  3. std::is_lt(r) turns that ordering into true.

Example 2 — Equal ranges

Two matching ranges give the equal ordering.

C++
#include <algorithm>
#include <compare>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> a = {1, 2};
    std::vector<int> b = {1, 2};

    auto r = std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end());
    std::cout << (std::is_eq(r) ? "Equal" : "Different") << '\n';
    return 0;
}

How It Works

  1. Both ranges hold 1 and 2, with the same length.
  2. No difference is found and neither range runs out first, so the result is equal.
  3. std::is_eq(r) is the test for that case.

Example 3 — Compare two words

Test the greater case with text.

C++
#include <algorithm>
#include <compare>
#include <iostream>
#include <string>

int main() {
    std::string a = "pear";
    std::string b = "peach";

    auto r = std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end());
    if (std::is_gt(r)) {
        std::cout << a << " is greater than " << b << '\n';
    }
    return 0;
}

How It Works

  1. pear and peach share the letters pea.
  2. The fourth letters are r and c, and r is later in the alphabet.
  3. So the first word is greater, and std::is_gt(r) is true.

Frequently Asked Questions

In C++, std::lexicographical_compare_three_way() compares two ranges in dictionary order and returns less, equal, or greater. Include <algorithm> to use it.
Include <algorithm>, and <compare> for std::is_lt() and friends. It needs C++20, so compile with -std=c++20 or later.
It returns an ordering object (for example std::strong_ordering) that says whether the first range is less than, equal to, or greater than the second.
Include <compare> to use std::is_lt, std::is_eq, and std::is_gt. The algorithm itself is in <algorithm>.
lexicographical_compare() returns a bool for "is less". The three-way version returns less, equal, or greater in one result.

Next: lower_bound()

Finds the first element that is not less than a value in a sorted range.

lower_bound() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

22 people found this page helpful