Visual Studio C++ 字符串比较而不带声明

Visual studio C++ string compare without declaration

本文关键字:声明 比较 Studio C++ 字符串 Visual      更新时间:2023-10-16

我想了解以下代码在Microsoft Visual Studio C/C++(2012版本)中的结果。实际上,当生成解决方案时,结果为"1"(True)。但是,单词"a"小于ASCII表中的单词"z"。因此,结果应为"0"(假)。即使我反转操作,平均值("z">"a")。结果为"1"。我也尝试了此操作("a"<"z")和("z"<"a"),结果是"0"任何人都可以解释我发生了什么?

#include <iostream>
#include <string>
using namespace std;
int main()
{
    cout <<  ("a" >  "z") << endl;
}

你的代码几乎和你写的一样:

const char s1[2] = {'a'};
const char s2[2] = {'z'};
int main()
{
    cout << (s1 < s2) << endl;
}

因此,您可以看到您的代码实际上是在比较两个字符数组的地址。