Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::string usage with !=, <, and > [closed]

Tags:

c++

string

What are the examples on usages of std::string class with !=, >, and <?

like image 778
Simplicity Avatar asked Feb 26 '23 06:02

Simplicity


1 Answers

  • s1 != s2 returns true if s1 is not equal to s2. It's case sensitive!
  • s1 < s2 returns true if s1 comes before s2 if they're arranged in dictionary order. If string s1 ="Nawaz" and string s2 = "nawaz", then s1 < s2 will return true.
  • s1 > s2 returns true if s1 comes after s2 if they're arranged in dictionary order.

As a general guideline, ascii value of uppercases is smaller than the ascii value of lowercases: A is smaller than a, B is smaller than b, and so on.

So uppercases are considered before the lowercases. By dictionary order, I meant the same thing. "A" is before "a". "Nawaz" is before "nawaz", "nAwaz", "nAWAZ" etc.

Compare character by character:

  • If ascii value of all characters in one string is equal to the ascii value of corresponding characters in the other string, then the two strings are equal.
  • If ascii value of a character is smaller than the corresponding character in the other string, then the first string is considered smaller. No need to compare all characters.
  • If so far all characters are equal, and you reached to the end in the first string, while there are still some characters in the second string to be compared, then the first string is considered smaller. That means, "Nawa" is smaller than "Nawaz"

I hope this explanation helps you understanding how comparisons work for std::string.

like image 146
Nawaz Avatar answered Mar 01 '23 09:03

Nawaz