0
C++ dot operator
string dog = "dog" string cat = "cat" cout << dog.compare(cat); // output is 1 I don't fully understanding the third line. The dot operator is used to access the members of a class right? How do strings lead to an int output? What concepts are involved here?
3 odpowiedzi
+ 4
A string is an object which has functions compare() is one of those functions that returns a integer value and displays it using the console output function.
+ 1
Yes, the dot is member access operator. In this case access to `compare` method of std::string class. The `compare` method is used to compare the string object <dog> to another string object (<cat>), lexicographically.
To understand what value may be returned by `compare` method, refer to the following 👇
https://en.cppreference.com/w/cpp/string/basic_string/compare
0
So the compare() method is essentially testing for string difference, yielding 0 if false, and 1 (or -1 depending on alphabetical spelling of the first string relative to the second) if true.