string compare

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

string compare

 
0
  #1
Nov 4th, 2007
Is it safe to compare strings with == operator or is .compare recommended?

One of my teachers said NOT to use the == operator and always use a function when available. The example given was somthing along the lines of "when cpu is under high usage the == operator behaviour is unpredictable and will return false on the same two strings..."

Another C++ teacher in another situation said something along the lines of "it's easier to convert a char*array to the string class and use == instead of strcmp function."

I know both are talking about different things but I'm confused about the == issue.
Last edited by rugae; Nov 4th, 2007 at 1:20 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: string compare

 
2
  #2
Nov 4th, 2007
Both your teachers are full of crap.

Use ==.

Keep in mind, though, that == is limited to an exact match comparison and only returns bool. You can get a little more information back using functions like string.compare().

Hope this helps.

[EDIT]
I should give a little more info.

For the first teacher: if system load were to affect your code then there is something seriously wrong with your compiler. Chances are that == just calls the compare function... (but I don't actually know, having not bothered to read the STL code for it).

For the second teacher: why waste time and space converting a C-string to a std::string when you have a function that will do what you want perfectly well without going through the long winded and wholly unnecessary effort... Kind of like this sentence, no?

@rugae, just smile and nod when they start spouting stuff about odd behavior and extra hoops. :wink:
Last edited by Duoas; Nov 4th, 2007 at 1:28 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: string compare

 
0
  #3
Nov 4th, 2007
Roger that!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: string compare

 
0
  #4
Nov 4th, 2007
is a java programmer trying to teach c++?
in c++, == always means logical equivalence.
as the language is not pointer-disadvantaged, you can compare addresses (again with an ==) to check for identity.
the case is different in java; you cannot comare strings for equivalence using an ==. see http://www.beginner-java-tutorial.co...omparison.html

  1. void compare( const std::string& a, const std::string& b )
  2. {
  3. bool equivalent = a == b ; // a and b are logically equivalent
  4. bool identity_equal = &a == &b ; // a and b are the same string object
  5. }
Last edited by vijayan121; Nov 4th, 2007 at 1:37 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4217 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC