题目链接:
#include <iostream>
using namespace std;
string get(string s)
{
string res;
for (auto c : s) res += tolower(c);
return res;
}
int main()
{
string a, b;
getline(cin, a);
getline(cin, b);
a = get(a), b = get(b);
if (a == b) puts("=");
else if (a > b) puts(">");
else puts("<");
return 0;
}
本题需要注意的是使用\(\rm auto\)对\(\rm string\)自动遍历的时候,有以下两种情况需要注意:
①
string s = "hello";
for (auto &i : s ) i = toupper(i); //改变成大写,影响s的值
cout << s; //s的值是HELLO
②
string s = "hello";
for (auto i : s ) i = toupper(i); //改变成大写,不影响s的值
cout << s; //s的值是hello
for (int i = 0; i < s.size(); i++) s[i] = tolower(s[i]);
与for (auto &i : s ) i = toupper(i);
等价。