| | |
string compare
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 27
Reputation:
Solved Threads: 0
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.
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.
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:
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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
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)
void compare( const std::string& a, const std::string& b ) { bool equivalent = a == b ; // a and b are logically equivalent bool identity_equal = &a == &b ; // a and b are the same string object }
Last edited by vijayan121; Nov 4th, 2007 at 1:37 am.
![]() |
Similar Threads
- to convert string to integer value (Legacy and Other Languages)
- Compare String with Strings (Visual Basic 4 / 5 / 6)
- hi.. string compare (Java)
- string: get token from string and compare token from text file (C++)
- How to see if one string is less than another string (C)
- string compare and fstream (C)
- need info? can't use strcmp/string compare (C++)
Other Threads in the C++ Forum
- Previous Thread: Why is my code execution truncated....
- Next Thread: Copy chars from array into dynamically allocated array
Views: 4217 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






