关注小韩 轻松编程 一起由浅入深
#include<iostream>
using namespace std;
#include<string>
//实际应用中更多的是比较两字符串是否相等;
void test()
{
string str1 = "hellod";
string str2 = "hello";
//原理:对应位置字符的ASCII码 相减
int t = str1.compare(str2); //注意调用函数的方式
if (t == 0)
cout << "两字符串相等" << endl;
else if (t > 0)
cout << "字符串1大与字符串2" << endl;
else
cout << "字符串1小于字符串2" << endl;
}
int main()
{
test();
return 0;
}