例如,我们有两个字符串:
string s = "cat";
string s1 = "dog";
Run Code Online (Sandbox Code Playgroud)
写下面的方法是否正确?
int a = strcmp(s, s1);
Run Code Online (Sandbox Code Playgroud)
或者什么是正确的形式?
ken*_*ytm 11
C++ std::string可以直接比较,所以你可以写例如
if (s == s1)
cout << "the strings are equal" << endl;
else if (s < s1)
cout << "the first string is smaller" << endl;
else
...
Run Code Online (Sandbox Code Playgroud)
但是如果你真的需要整数值,你可以使用该.compare方法.
int a = s.compare(s1);
Run Code Online (Sandbox Code Playgroud)