代码
#include<iostream>#include<string>using namespace std;//string字符串的比较//比较方式:按照ASCll// = 返回 0// > 返回 1// < 返回 -1//函数原型/*① int compare(const string &s) const //与字符串s比较② int compare(const char *s) const //与字符串s比较*/void test01(){string s1 = "xello";string s2 = "hello";if (s1.compare(s2) == 0) {cout << "s1 等于 s2" << endl;}else if (s1.compare(s2) > 0) {cout << "s1 大于 s2" << endl;}else {cout << "s1 小于 s2" << endl;}}int main() {test01();return 0;}
字符串的比较主要用于判断两个字符串是否相同。
