问题描述
代码如下,它输出 1
:
int main(int argc,char * argv [])
{
vector< const char *> vec = {nima,123};
vector< const char *> vec2 = {nima,123};
auto result = equal(vec.cbegin(),vec.cend(),vec2.cbegin());
cout<<结果< endl;
return 0;
}
我知道我可以测试两个c风格的字符串是否相等使用 strcmp
(因为 char *
不是我理解的对象)。但是等于
是< algorithm>
的函数。它是否重载 ==
运算符,以便它可以测试两个 char *
? b
$ b
@ Damon说,它们等于将同一个字符串文字合并到同一个地址,就像我的理解。但是,当我尝试不同地址的 char *
时,它仍然给出了相同的结果:
int main(int argc,char * argv [])
{
char * k =123
char * b =123;
vector< const char *> vec = {nima};
vector< const char *> vec2 = {nima};
cout<< & k< endl;
cout<< & b< endl;
vec.push_back(k);
vec2.push_back(b);
auto result = equal(vec.cbegin(),vec.cend(),vec2.cbegin());
cout<<结果< endl;
return 0;
}
结果是:
0x7fff5f73b370
0x7fff5f73b368
1
这里可能发生的是编译器/链接器将其中两个相同的四个字符串文字合并为两个文字。因此,nima
和123
都有相同的地址。
您将地址存储在向量中并将它们(地址上的 operator ==
)进行比较。由于地址相同,比较是相等的。
请注意,这是 accidential 。它只有工作,因为两个原因:
- 字符串是字面量的(也就是说,不是从<$ c $
- 允许编译器,但不需要合并相同的字面值(无论这是否是实现定义的)。
这可能会导致两种情况,你会得到一个有趣的惊喜(如果你必须找出为什么突然不工作,它工作所有的时间),当你使用不同的编译器,甚至相同的编译器,不同的优化设置,或当你分配非文字字符串。
The codes are like this, it outputs 1
:
int main(int argc, char *argv[])
{
vector <const char*> vec = {"nima","123"};
vector <const char*> vec2 = {"nima","123"};
auto result = equal(vec.cbegin(), vec.cend(), vec2.cbegin());
cout << result << endl;
return 0;
}
I knew that I can test whether two c-style string is equal only by using strcmp
(because char*
is not an object as I understood). But here equal
is a function from <algorithm>
. Does it overload the ==
operator so that it can test the equality of two char*
?
@Damon says that they're equal as the merges the same string literal into same address, as I understood. However, when I tried char*
with different addresses, it still gives me the same result:
int main(int argc, char *argv[])
{
char* k = "123";
char* b = "123";
vector <const char*> vec = {"nima"};
vector <const char*> vec2 = {"nima"};
cout << &k << endl;
cout << &b << endl;
vec.push_back(k);
vec2.push_back(b);
auto result = equal(vec.cbegin(), vec.cend(), vec2.cbegin());
cout << result << endl;
return 0;
}
The result is:
0x7fff5f73b370
0x7fff5f73b368
1
What probably happens here is that the compiler/linker merges the four string literals of which two are identical into two literals. Therefore, both "nima"
and both "123"
have the same address.
You store the addresses in a vector and compare them (operator==
on an address). Since the addresses are the same, the comparison is equal.
Note that this is accidential. It only "works" because of two reasons:
- The strings are literals (that is, not some strings read from e.g.
stdin
). - A compiler is allowed, but not required to merge identical literals (whether or not this happens is implementation-defined).
This can lead to two situations in which you get a funny surprise (not so funny if you must find out why it suddenly doesn't work when it "worked" all the time), either when you use a different compiler or even the same compiler with different optimization settings, or when you assign non-literal strings.
这篇关于为什么`equal'用于C ++中的const char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!