且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++字符串文字相等性检查?

更新时间:2023-12-03 19:29:04

在这里,您必须非常小心,因为您正在查看的某些情况与其他情况并不完全相同。

You have to be very careful here, as some of the cases you're looking at aren't quite equivalent to others.

在第一个示例中:

char a[] = "aaa";
char b[] = "aaa";

if (a == b)

您要创建两个 arrays ,每个均从字符串文字初始化。然后,您尝试将这些阵列相互比较。在大多数情况下(包括此情况),数组的名称将求值为该数组中第一个元素的地址。因此,您实际上是在比较两个阵列的地址。那些不能相同,因此保证比较得出 false

You're creating two arrays of char, each initialized from an string literal. You're then attempting to compare those arrays to each other. In most cases (including this one) the name of an array evaluates to the address of the first element in that array. So you're really comparing the addresses of the two arrays. Those can't be the same, so the comparison is guaranteed to yield false.

在第二个示例中: if( aaa == aaa),您要比较的是字符串文字自身,而不是根据字符串文字初始化的数组。

In your second example: if ("aaa" == "aaa"), you're comparing the string literals themselves, rather than arrays initialized from the string literals.

标准不保证这样做的结果。该标准允许(但不要求)将相同的字符串文字合并在一起。同样,您真正要比较的不是字面量的内容,而是它们存储的地址。如果编译器合并了字符串文字,因此它们位于相同的地址,则将生成 true 。如果将它们分开,则会产生 false

The result of this is not guaranteed by the standard. The standard allows (but does not require) that identical string literals be merged together. Again, however, what you're really comparing isn't the contents of the literals--it's the addresses at which they're stored. If the compiler merges the string literals so they're at the same address, this will yield true. If it keeps them separate, it'll yield false.

在您的 auto case:

auto a = "whuiwhqohqweihqweohi";
auto b = "whuiwhqohqweihqweohi";

您的情况几乎相同- a b 最终都作为指向char的指针,它们保存着字符串文字的地址。如果编译器合并了这些文字,它们都将指向相同的地址,因此它们将比较相等。如果编译器没有将它们合并在一起,则每个都有自己的地址,它们将比较不相等。

You have pretty much the same situation--a and b both end up as pointers to char, holding the addresses of the string literals. If the compiler merges those literals, they'll both point to the same address so they'll compare equal. If the compiler doesn't merge them together, each will have its own address, and they'll compare as not equal.

这里的重点是,这些都不是完全比较字符串的内容,仅比较存储它们的地址。内容仅在两个字符串文字具有(或至少以)相同内容合并时才能合并。

The big point here is that none of these is comparing the contents of the strings at all, only the addresses at which they're stored. The content counts only to the degree that two string literals can only be merged if they have (or at least end with) the same content.

结尾为,我指的是这样一个事实:如果您有以下内容,则是编译器: wing 在一个位置,而 swing 在另一种情况下,编译器可以***地合并两者,因此可以使用以下代码:

As far as "at least end with" goes, I'm referring to the fact that a compiler if you had something like: "wing" in one place and "swing" in another, the compiler is free to merge the two, so with code something like:

auto a = "wing";
auto b = "swing";

...编译器可以存储 swing 放在一个位置,并初始化 a 指向该存储文字的第二个字符。

...the compiler could store swing in one place, and initialize a to point to the second character of that stored literal.