Why use strcmp instead of == in C ++?

Interestingly, my code works strcmpjust fine either using or simply ==in C ++ to compare char arrays. Can anyone justify the reason for using strcmpinstead ==;

+4
source share
2 answers

strcmpcompares the actual contents C-string, and when used ==between the two, the C-stringquestion is asked whether these two pointers point charto the same position.

If we have C-string, defined as follows:

char string_a[] = "foo";
char string_b[] = "foo";
char * string_c = string_a;

strcmp(string_a, string_b) == 0 true, string_a == string_b false. "" string_a string_c == true.

C-string, , strcmp.

: C++ C, , std::string. ,

std::string string_d = "bar";
std::string string_e = "bar";

string_d == string_e true. string_d.compare(string_e) 0, C++ strcmp.

+8

strcmp , .... < 0, str1 str2

0, str1 str2 0, .

==, true false.

+1

Source: https://habr.com/ru/post/1532938/


All Articles