字符串比较是按字符的ASCII码进行对比
(字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小意义不大)
= 返回 0
返回 1
< 返回 - 1
int compare(const string &s) const;//与字符串s比较
int compare(const char *s) const; //与字符串s比较
#include <iostream>
using namespace std;
void test01()
{
string str1 = "hello";
string str2 = "aello";
if (str1.compare(str2) == 0) {
cout << "str1等于str2" << endl;
}
else if (str1.compare(str2) > 0) {
cout << "str1大于str2" << endl;
}
else {
cout << "str1小于str2" << endl;
}
}
int main()
{
test01();
return 0;
}