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)
Call
What 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
Parameter
Description
first1, last1
Required. The first range.
first2, last2
Required. The second range.
comp
Optional. A three-way comparison function. The default is std::compare_three_way.
Return Value
Return Value
Description
Less
The first range is smaller (std::is_lt(r) is true).
Equal
Both ranges match (std::is_eq(r) is true).
Greater
The 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;
}
Output
a is less than b
How It Works
The call returns an ordering object, not a bool.
The third elements decide it: 3 comes before 4.
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;
}
Output
Equal
How It Works
Both ranges hold 1 and 2, with the same length.
No difference is found and neither range runs out first, so the result is equal.
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;
}
Output
pear is greater than peach
How It Works
pear and peach share the letters pea.
The fourth letters are r and c, and r is later in the alphabet.
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.