String problem
-
hi all I have problem im my program, I have:
string str_1, str_2; //......... if( strcmp(str_1.data(),str_1.data()) == 0) { cout<<"equals"; } else { cout<<"Not equals"; }
problem is that in 'str_1' and 'str_2' I have same text and rezult is "Not equals", can you help me and say how can I compeer two STL string's? -
hi all I have problem im my program, I have:
string str_1, str_2; //......... if( strcmp(str_1.data(),str_1.data()) == 0) { cout<<"equals"; } else { cout<<"Not equals"; }
problem is that in 'str_1' and 'str_2' I have same text and rezult is "Not equals", can you help me and say how can I compeer two STL string's?Hello, If you refer to your STL implementation doc's, you should find that data(): "Returns a pointer to an array of characters, not necessarily null-terminated, representing the string's contents", so it's probably a bad idea to pass that to a function that expects 0-terminated 'strings'. You should use c_str() instead, if you want to use strcmp. A better way, if you're just testing for equality, would be to use std::string's operator ==(), as in
if (str_1 == str_1)
HTH Jonas--- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot
-
Hello, If you refer to your STL implementation doc's, you should find that data(): "Returns a pointer to an array of characters, not necessarily null-terminated, representing the string's contents", so it's probably a bad idea to pass that to a function that expects 0-terminated 'strings'. You should use c_str() instead, if you want to use strcmp. A better way, if you're just testing for equality, would be to use std::string's operator ==(), as in
if (str_1 == str_1)
HTH Jonas--- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot
if (str_1 == str_1)
? :confused: there is not operator ==() -
if (str_1 == str_1)
? :confused: there is not operator ==()If you're using STL's
std::string
there is indeed anoperator ==
.Steve
-
if (str_1 == str_1)
? :confused: there is not operator ==()In your example code you write
if( strcmp(str_1.data(),str_1.data()) == 0)
so I just went with the same comparison. std::string has the == operator, please refer to your documentation. If your implementation doesn't, consider updating to a proper implementation. You can also use str_1.compare(str_1) (or str_2 if it's just a type-o) /J--- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot
-
In your example code you write
if( strcmp(str_1.data(),str_1.data()) == 0)
so I just went with the same comparison. std::string has the == operator, please refer to your documentation. If your implementation doesn't, consider updating to a proper implementation. You can also use str_1.compare(str_1) (or str_2 if it's just a type-o) /J--- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot
thanks. it help me. :)
-
hi all I have problem im my program, I have:
string str_1, str_2; //......... if( strcmp(str_1.data(),str_1.data()) == 0) { cout<<"equals"; } else { cout<<"Not equals"; }
problem is that in 'str_1' and 'str_2' I have same text and rezult is "Not equals", can you help me and say how can I compeer two STL string's?Don’t do that! If you want to compare two string object use “str_1 == str_2”. If you must use the C comparison function then use “strcmp(str_1.c_str(), str_2.c_str())”. Of course the example you are giving will always be true, provide the system does not object, because both arguments are the same.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra