C++ 字符串文字之间的比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2469160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comparison between string literal
提问by Ruggero Turra
This very simple code:
这个非常简单的代码:
#include <iostream>
using namespace std;
void exec(char* option)
{
cout << "option is " << option << endl;
if (option == "foo")
cout << "option foo";
else if (option == "bar")
cout << "opzion bar";
else
cout << "???";
cout << endl;
}
int main()
{
char opt[] = "foo";
exec(opt);
return 0;
}
generate two warning: comparison with string literal results in unspecified behaviour.
生成两个警告:与字符串文字比较会导致未指定的行为。
Can you explain why exactly this code doesn't work, but if I change
你能解释一下为什么这段代码不起作用,但如果我改变
char opt[]
to
到
char *opt
it works, but generates the warning? Is it related to the \0 termination? What is the difference between the two declaration of opt? What if I use const qualifier? The solution is to use std::string?
它有效,但会产生警告?它与 \0 终止有关吗?opt的两个声明有什么区别?如果我使用 const 限定符怎么办?解决方案是使用 std::string?
回答by John Knoeller
char arrays or char pointers aren't really the same thing as string class objects in C++, so this
char 数组或 char 指针与 C++ 中的字符串类对象实际上并不是一回事,所以这
if (option == "foo")
Doesn't compare the string option
to the string literal "foo" it compares the addressof option
with the address of the string literal "foo". You need to use one of the many string comparison functions if you want to know if the option is the same as "foo". strcmp
is the obvious way to do this, or you can use std::string
instead of char*
不比较字符串option
到字符串“富”它比较地址的option
使用字符串“富”的地址。如果您想知道该选项是否与“foo”相同,则需要使用众多字符串比较函数之一。 strcmp
是执行此操作的明显方法,或者您可以使用std::string
代替char*
回答by Laurynas Biveinis
You can use ==
operator to compare strings only if you use std::string
(which is a good practice). If you use C-style char*/char[] strings, you need to use C functions strcmp
or strncmp
.
==
只有在使用时才可以使用运算符来比较字符串std::string
(这是一个很好的做法)。如果使用 C 风格的 char*/char[] 字符串,则需要使用 C 函数strcmp
或strncmp
.
You can also use the std::string::operator ==
to compare std::string
with a C string:
您还可以使用与 C 字符串std::string::operator ==
进行比较std::string
:
std string foo = "foo";
const char *bar = "bar";
if (foo == bar)
...
回答by Renze de Waal
The reason why it doesn't work is because the comparison does not compare strings, but character pointers.
它不起作用的原因是因为比较不是比较字符串,而是字符指针。
The reason why it maywork when you use char* is because the compiler may decide to store the literal string "opt" once and reuse it for both references (I am sure I have seen a compiler setting somewhere that indicates whether the compiler does this).
当您使用 char* 时它可能起作用的原因是因为编译器可能决定将文字字符串“opt”存储一次并将其重用于两个引用(我确信我在某处看到了一个编译器设置,指示编译器是否这样做)。
In the case of char opt[], the compiler copies the string literal to the storage area reserved for the opt array (probably on the stack), which causes the pointers to be different.
在 char opt[] 的情况下,编译器将字符串文字复制到为 opt 数组保留的存储区域(可能在堆栈上),这会导致指针不同。
Renze
任泽
回答by Bill
For C++ I would use the std::string solution:
对于 C++,我会使用 std::string解决方案:
#include <iostream>
#include <string>
using namespace std;
void exec(string option)
{
cout << "option is " << option << endl;
if (option == "foo")
cout << "option foo";
else if (option == "bar")
cout << "option bar";
else
cout << "???";
cout << endl;
}
int main()
{
string opt = "foo";
exec(opt);
exec("bar");
char array[] = "other";
exec(array);
return 0;
}
std::string knows how to create itself out of char[], char*, etc, so you can still call the function in these ways, too.
std::string 知道如何从 char[]、char* 等中创建自己,因此您仍然可以通过这些方式调用该函数。
回答by Andrey
Looks like you came from Java/C# :) In C++ string is just a pointer to memory where characters are stored and with null char at the end. If strings "look" equal they may point to different areas in memory and will not be equal. to check equality either use C++'s class std::string or C function strcmp .
看起来你来自 Java/C# :) 在 C++ 中,字符串只是一个指向存储字符的内存的指针,最后是空字符。如果字符串“看起来”相等,它们可能指向内存中的不同区域,并且不会相等。要检查相等性,请使用 C++ 的类 std::string 或 C 函数 strcmp 。