为什么即使字符串相同,此代码也会将输出视为false? [英] Why this code is giving output as false even when strings are same?
本文介绍了为什么即使字符串相同,此代码也会将输出视为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我给了两个字符串相同的值,但它仍然说条件是假的。
我尝试了什么:
I have given two strings the same values but still it's saying the condition is false.
What I have tried:
char s[] = "hello", t[] = "hello";
std::cout << "s: " << s << std::endl;
std::cout << "t: " << t << std::endl;
std::cout << (s == t ? "true" : "false") << std::endl;
推荐答案
您正在比较两个字符数组的地址,而不是它们的内容。切换到std :: string或使用strcmp,请参阅此处的讨论:
比较C ++中char数组的值 - Stack Overflow [ ^ ]
You're comparing the address of the two character arrays, not their contents. Either switch to std::string or use strcmp, see discussion here:
Comparing the values of char arrays in C++ - Stack Overflow[^]
这是因为字符串不是怎么样的比较。
ins == t
,你要比较两个字符串的地址,它们总是不同。
This is because is not how string are compared.
ins == t
, you are comparing the addresses of both string, and they are always different.
这篇关于为什么即使字符串相同,此代码也会将输出视为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文