hi,i got 2 variables of string format.how to compare if they are equal????
it gives me error.Code:string a="Asd";
string b="Asd";
if(strcmp(a,b)==0){
cout<<"true";
}
Printable View
hi,i got 2 variables of string format.how to compare if they are equal????
it gives me error.Code:string a="Asd";
string b="Asd";
if(strcmp(a,b)==0){
cout<<"true";
}
> if(strcmp(a,b)==0){
It's even easier.
Code:if (a == b){
A long(er) way (using the string container's compare member function):
Another long way (using the strcmp function):Code:string a="Asd";
string b="Asd";
if( a.compare(b) == 0 ) {
cout<<"true";
}
But yeah, just using operator== is short and sweet.Code:string a="Asd";
string b="Asd";
if(strcmp(a.c_str(),b.c_str())==0){
cout<<"true";
}
;)
strcmp() is only for c-style strings. like char*string_thing. the class string is a C++ construct and will not work with functions that require a char*.
>> the class string is a C++ construct and will not work with functions that require a char*.
Technically it can work if you use the c_str() member function, but in most cases there are better alternatives that are specific to the C++ string class.