gpt4 book ai didi

c++ - 为什么 std::string::find() 可以处理 '\0' ?

转载 作者:行者123 更新时间:2023-12-02 00:52:51 24 4
gpt4 key购买 nike

strstr()std::string::find() 的实现有什么区别?

例如:

char* str = "abc\0hijk@#$%";
char* temp;
std::string str1;

for (int i=0; i <=12; i++) {
str1.push_back(str[i]);
}

strstr(temp, "@#");// can not handle'\0'
str1.find("@#");// success

最佳答案

std::string class 是一个 C++ 类,表示可以包含空字符的字符串。它的成员函数(例如 find)旨在处理这些嵌入的空值。

strstr (来自 C 的函数)使用 char* 指针,该指针指向 C 样式字符串。由于 C 样式字符串以 null 结尾,因此它们无法处理嵌入的 null。为此,strstr 记录如下:

Locate substring
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.

The matching process does not include the terminating null-characters, but it stops there.

斜体部分与此处相关。

关于c++ - 为什么 std::string::find() 可以处理 '\0' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61412956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com