Search code examples
c++stringoperator-overloadingstring-comparisonstdstring

What is the meaning of comparing strings with >=


I am studying C++. I am surprised that it can compare strings. The following code compiles and runs successfully for strings a and b.

if (b >= a)
{}

What does it mean?


Solution

  • All comparisons of std::string are lexicographical. See std::basic_string::operator>=.

    You can find an excellent answer which explains this in detail here: Using the less than comparison operator for strings. The operators < and >= are not equivalent, but the principle is the same.