问题
What's difference between C and C++ in string comparison in other words?
I came from 'C' camp. I saw program using == to compare strings, I tried to find its overloading program, but did not find one. Does it mean C++ deal with string (char []) naturally with == and !=?
If I have my own defined String class, would it be same that I can use == and != without defining them? or it applies only to char []?
Edit:
It looks like I mixed C's char[] with C++ std::string class. OK, the old question still apply. Some followed questions as below:
My programs defined char[] variables, but compared with "==" operator. It seemed work. They are C-style string, but work with "==". Did compiler auto-convert them to std::string for me so that the programs worked?
Edit2:
Here is a working sample.
if(name == "") return;
where "name" is a MyString class item. It has a construtor with a parameter char*.
MyString has a public function data(), it returns char* C-style string pointer.
So, am I comparing MyString objects? or C-style strings?
Without overload "!=" myself, can I do something like below?
if( name.data() != somes_[i].data() )
....
回答1:
"Does it mean C++ deal with string (char []
) naturally with ==
and !=
?"
No. It is wrong to compare C-style strings (i.e. char[]
or char*
) between each other with ==
or !=
(in both C and C++), because you will end up comparing pointers rather than strings.
In C++ it is possible to compare std::string object with a C-style string or another std::string object because of the std::string
's operator== and operator!=:
std::string str1("abc"), str2("def");
if (str1 != str2 && str1 == "abc")
std::cout << "Yep, I got it.";
回答2:
Does it mean C++ deal with string (char []) naturally with == and !=?
No; "aaa" == "aaa"
will convert the string literals to char*
as normal, and compare the pointer values. However most implementations of C and C++ will de-duplicate string literal data, at least within an object file, so that those pointers will be the same. This is not guaranteed by the standard, but is widely implemented.
In other words, C++ does not differ from C in this regard and comparing char*
values this way is no better than doing so in C.
Did compiler auto-convert them to std::string for me so that the programs worked?
std::string
has overloaded comparison operators that work with char*
. If you compare a char*
with an std::string
then one of those will be used.
char const *s = "aaa";
std::string t = "aaa";
s == t; // uses overloaded operator== to compare string values.
http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp
template< class CharT, class traits, class Alloc >
bool operator==( const CharT* lhs, const basic_string<CharT,Traits,Alloc>& rhs );
回答3:
Do not use != or != to compare C-style strings (char * or char[]).
If you are sure the strings are null terminated use std::strcmp(). If you aren't absolutely 100% POSITIVE that they are null terminated, use std::strncmp() to compare them.
if either of your strings is an std::string you can use operator == or operator !=.
[edited in response to comment from barnes53]
来源:https://stackoverflow.com/questions/19232814/can-i-use-and-in-c-for-string-comparison-without-writing-my-own