String comparison

What is c++ comparing when I have a code like this:
1
2
3
4
5
6
  string a = "car";
  string b = "aar";
  
  if (a > b){
     cout << b << " " << a << endl;
  }


Can I use it to sort alphabetically strings? Thanks!

And, what if I have strings like:
 "aaa 1" "aaa 2"
, if i use the > or < operator, what I'll have?
Last edited on
It performs a (by default, case sensitive) lexicographical comparison of the two strings.

Lexicographical comparison is a operation with the following properties:

Two ranges are compared element by element.
. The first mismatching element defines which range is lexicographically less or greater than the other.
. If one range is a prefix of another, the shorter range is lexicographically less than the other.
. If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.
. An empty range is lexicographically less than any non-empty range.
. Two empty ranges are lexicographically equal.
https://en.cppreference.com/w/cpp/algorithm/lexicographical_compare

Topic archived. No new replies allowed.