C++ std::lexicographical_compare() Function

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

Definition

In C++, std::lexicographical_compare() compares two ranges like words in a dictionary. It returns true when the first range is smaller than the second.

Elements are compared from left to right. The first difference decides the result. If one range is a prefix of the other, the shorter range is smaller.

Syntax

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

C++
std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), comp);
CallWhat it does
std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());Is a less than b?
std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), comp);Use your own ordering, such as std::greater.
Library declarations
C++
template<class InputIt1, class InputIt2>
bool lexicographical_compare(InputIt1 first1, InputIt1 last1,
                             InputIt2 first2, InputIt2 last2);

Parameters

ParameterDescription
first1, last1Required. The first range.
first2, last2Required. The second range.
compOptional. A function that returns true when its first argument is less than the second.

Return Value

Return ValueDescription
trueThe first range comes before the second.
falseThe first range is equal to or comes after the second.

C++ Examples

Example 1 — Compare two vectors

The first difference decides: 3 is less than 4.

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

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

    bool less = std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
    std::cout << std::boolalpha << "a comes before b: " << less << '\n';
    return 0;
}

How It Works

  1. The first two values are equal, so they are skipped.
  2. At the third position 3 is less than 4.
  3. That one difference decides the whole result: true.

Example 2 — Compare words

Dictionary order for two strings.

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

int main() {
    std::string x = "apple";
    std::string y = "apricot";

    bool before = std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
    std::cout << x << (before ? " comes before " : " comes after ") << y << '\n';
    return 0;
}

How It Works

  1. Both words start with ap, so those letters tie.
  2. The third letters are p and r, and p is earlier in the alphabet.
  3. So apple is smaller than apricot.

Example 3 — Reverse the order

Pass std::greater to flip the meaning of less.

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

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

    bool before = std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(),
                                               std::greater<int>());
    std::cout << std::boolalpha << "a first under greater: " << before << '\n';
    return 0;
}

How It Works

  1. std::greater<int>() replaces the usual less-than rule.
  2. The first values are 5 and 3. Under greater, 5 counts as "before" 3.
  3. The first difference already decides, so the answer is true.

Frequently Asked Questions

In C++, std::lexicographical_compare() checks whether one range comes before another in dictionary order. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns true if the first range is lexicographically less than the second range, and false otherwise.
It is dictionary order. Compare the first elements, then the second, and so on. The first difference decides which range comes first.
The shorter range is considered smaller. For example, "app" comes before "apple".

Next: lexicographical_compare_three_way()

Compares two ranges in dictionary order and returns less, equal, or greater.

lexicographical_compare_three_way() 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.

21 people found this page helpful