How can I compare C- string with C++ string?
Asked Answered
F

2

7

I want to find out why compare function doesn't give me correct result ?

As I know it should return 0 if two string are the same!

bool validatePassword(char id[], string password) {

    // cannot be the same as the id string
    if(password.compare(id) == 0) {
        cout << "password can not be as same as id\n";
        return false;
    }

    return true;
}
Foliated answered 17/1, 2016 at 0:58 Comment(7)
Cannot reproduce. Please provide a minimal reproducible example like the one I linked to, or even with just the comparison part.Aguilera
I guess you may be compareing something like "hoge\n" with "hoge" and the reason for not giving correct result may be that the input is wrong.Berkie
Responce for both same and not same strings seems correct on Wandbox.Berkie
Why not use strcmp(password.c_str(), (char*)id)? I'd have to go with MikeCAT and say the input string for 'id' may not be null terminated or something.Geopolitics
@Geopolitics Why not use strcmp(password.c_str(), id) without the meaningless cast?Berkie
I wasn't sure off the top of my head, but yes, the cast is meaningless here.Geopolitics
Atefe please see the same question asked "Differences between C++ string == and compare()?" at this web site!Helbonia
T
7

As Matteo Italia mentioned in another answer's comment. Use the std::string's operator== like this:

bool validatePassword(char id[], string password) {
    return password == id;
}

This function is really unnecessary because your caller should call operator== directly instead.

Tolley answered 17/1, 2016 at 3:42 Comment(0)
C
2

You can do it by converting id to a string and compare to strings:

string idStr(id);
if (password == idStr){

}

Or use strcmp to compare two char arrays:

if(strcmp (password.c_str(), id) == 0){

}

You have to convert the string to a char array with the method c_str()

Cheque answered 17/1, 2016 at 1:51 Comment(5)
Constructing a new string just to make the comparison is wasteful, and both approaches are needlessly complicated, as std::string already provides an overload of operator== to compare an std::string with a C string.Radioactivity
@MatteoItalia, fortunately, wasteful is some architecture dependant issue. You cannot say that constructing a new string to make the comparison is wasteful. You have two values in two different internal formats. What is true is that you have to forcely change one format to the other to be able to compare. Even if that is done behind the scenes as automatic char * to string conversion being made to use == operator or if you use .c_str() method to make a standard char * from a string. Either you do, you'll not know which method is more efficient until you know target arch.Did
@MatteoItalia, saying that using a native standard C++ type is worse that using an old C type says too little about the C++ language. Are you programming all your C++ code using old plain C constructs for efficiency reasons. As far as I know the string class was invented for efficiency reasons also. Don't recommend using C constructs in a C++ forum, please.Did
Feel free to live in the fantasy world where constructing an std::string is free (it's not; recently I had to fix a parser that used substr all the time, needlessly hammering the allocator; it became 5 times faster). Also, as far as using C constructs for no reason, I agree, and that's why I criticized the strcmp approach. But the main point here is that both solutions are complicated for no reason, since std::string can simply be compared with a C string using ==.Radioactivity
The second option worked great for me, thanks. (I didn't try the first)Cal

© 2022 - 2024 — McMap. All rights reserved.