4 Replies - 332 Views - Last Post: 04 December 2012 - 01:42 AM Rate Topic: -----

#1 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Relational string operators

Posted 03 December 2012 - 11:36 PM

I apologize if this is in the wrong fourm/ sub-fourm - I didn't know where else it might have fit.
I was going over my notes and found a part that I don't quite seem to understand. We were talking about C++ String processing and I came to a part about relational operators. I can understand how == and != are the same as usage with numbers because it's straight up yes/no as to whether "hello" = "hello", but what exactly does it mean if one string is "less than" another? Does it correspond to length of the string? I'm looking at the examples that are given, but I don't quite understand the pattern. I'm guessing it can't be regarding just the length because I didn't have to use length()?
...
  string s1, s2;
  cout << "Enter first string: ";
  getline(cin, s1);

  cout << "Enter second string: ";
  getline(cin, s2);

  if (s1 == s2)
    {
      cout << s1 << " equals " << s2 << endl;
    }
  else if (s1 < s2)
    {
      cout << s1 << " < " << s2 << endl;
    }
  else
    {
      cout << s1 << " > " << s2 << endl;
    }


hello < howdy - 4 letters < 5 letters
hello > Howdy - capital letter is less?
HelloWorld > Hello World - blanks are greater?

Is This A Good Question/Topic? 0
  • +

Replies To: Relational string operators

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Relational string operators

Posted 03 December 2012 - 11:56 PM

The strings are compared lexicographically, i.e. str1 is less than str2 iff str1's first character is less than that of str2 or the first characters are equal and the rest of str1 is less than the rest of str2.

The individual characters are compared using their numeric value in the given encoding. So H is less than h because the ASCII value of H is less than that of h.

It has nothing to do with the length of the string's except that the prefix of a string is less than that string.

This post has been edited by sepp2k: 03 December 2012 - 11:59 PM

Was This Post Helpful? 1
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Relational string operators

Posted 03 December 2012 - 11:58 PM

The overloaded relational operators are syntactical sugar for the compare function: http://www.cplusplus...string/compare/

Read up on how compare works, and then read how the operators are implemented here:
http://www.cplusplus...ring/operators/

This post has been edited by jjl: 03 December 2012 - 11:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 moot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-September 12

Re: Relational string operators

Posted 04 December 2012 - 12:39 AM

Thanks for the quick reply - sorry it's a late reply on my part. I was running into an error loading the page.

Quote

the first characters are equal and the rest of str1 is less than the rest of str2.

To reiterate, since str1 < str2 iff strq's first character is less than that of str2, "hello" is less than "howdy" simply because the ASCII value for "e" is less than "o" ?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: Relational string operators

Posted 04 December 2012 - 01:42 AM

Right.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1