在C++中,std :: string类实现了comparison operators。 下面的代码打印AAA
C++:比较运算符>和字符串文字的意外结果
#include <iostream>
using namespace std;
int main() {
if("9">"111")
cout << "AAA";
else
cout << "not AAA";
return 0;
}
这个片断输出not AAA
:
#include <iostream>
using namespace std;
int main() {
if("9">"111")
cout << "AAA";
else
cout << "not AAA";
if("99">"990")
cout << "BBB";
return 0;
}
为什么会这样?
你的代码在哪里使用'std :: string'? '“blah”'不是'std :: string'。 – NathanOliver
区分'std :: string'和C-string。 –
您正在比较'const char *'值,而不是'std :: string'。 – user0042