943,614 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6848
  • C++ RSS
Nov 4th, 2007
0

string compare

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Nov 4th, 2007
2

Re: string compare

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 4th, 2007
0

Re: string compare

Roger that!
Reputation Points: 10
Solved Threads: 0
Light Poster
rugae is offline Offline
27 posts
since Mar 2007
Nov 4th, 2007
0

Re: string compare

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Why is my code execution truncated....
Next Thread in C++ Forum Timeline: Copy chars from array into dynamically allocated array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC