简体   繁体   English

对char *进行相等测试std :: string,operator ==()始终安全吗?

[英]Equality-test std::string against char*, is operator==() always safe?

STL运算符和std::string重载是否意味着可以安全地使用operator==char*std::string进行比较,而没有限制,即LHS / RHS?

No, it is not safe without restrictions . 不, 没有限制是不安全的。 The restrictions are: 限制是:

  • the char* must not be a nullpointer. char*不能为空指针。
  • the character sequence pointed to by the char* has to be zero-delimited (ie end with a \\0 ) char*指向的字符序列必须以零分隔(即以\\0结尾)

But it is not important which one you put left and which one right - it gives the same result. 但是,哪一个放在左边和哪一个放在右边并不重要-它给出相同的结果。

But there's a caveat: std::string s may contain \\0 characters that are not at the end. 但有一个警告: std::string s可能包含\\0不在结尾的字符。 Comparing one of those against a char* character sequence will always give false, because the comparison will stop at the first \\0 encountered in the char* . 将其中一个与char*字符序列进行比较将始终为false,因为比较将在char*遇到的第一个\\0处停止。

Example: 例:

char c[] = "Hello\0 World!";
std::string s(c, sizeof(c));

std::cout << ((s == c) ? "ok" : "meh") << '\n';  //meh    - compares only until the first \0
std::cout << c << '\n';                          //Hello  - cout also stops at first \0
std::cout << s << '\n';                          //Hello World!

是的,只要您确定char *不是空指针,并且它以空终止,就可以安全使用。

A std::string can contain multiple null characters. 一个std::string可以包含多个空字符。 However operator== for std::string and char* is defined as 但是, std::stringchar* operator==定义为

Compares the contents of a string with another string or a null-terminated array of CharT. 将字符串的内容与另一个字符串或CharT的以null终止的数组进行比较。

Example of that problem: 该问题的示例:

std::string a = "hello";
char* b = "hello\0fellow\0";
bool equals = (a == b); // will give true, though a and b are not the same

Another issue may arise if you char* string is not null-terminated. 如果您的char*字符串不是空值终止,则可能会出现另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串类中的相等测试运算符 - Equality Test Operator in String Classes 重载char *和std :: string是否安全? - Is it safe to overload char* and std::string? 对于std :: string使用operator []是否安全 - Is it safe to use operator [] for std::string 以安全的方式从char *创建std :: string - create std::string from char* in a safe way std::string::operator=(char): 哪个编译器是对的? 以及如何测试这种错误? - std::string::operator=(char): Which compiler is right? And how to test for this kind of bug? ostream和运算符std :: basic_string <char, std::char_traits<char> ,std :: allocator <char> &gt;? - ostream and operator std::basic_string<char, std::char_traits<char>, std::allocator<char>>? 将函数参数 `const char*` 转换为 `std::string_view` 是否安全? - Is it safe to convert function parameter `const char*` to `std::string_view`? 比较std :: vector的指针以检查相等性是否安全? - Is it safe to compare to pointer of std::vector to check equality? 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 希望std :: string :: operator []可以返回未签名的char - Hope std::string::operator[] could return unsigned char
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM