Example 1 — Compare two vectors
The first difference decides: 3 is less than 4.
#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
- The first two values are equal, so they are skipped.
- At the third position 3 is less than 4.
- That one difference decides the whole result:
true.

