strstr()和std :: string :: find()有什么区别?

为什么std :: string :: find()可以处理'\ 0'? 例如:

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
评论
  • Leaco
    Leaco 回复

    The std::string class is a C++ class that represents a string which can contain a null character. Almost all of its member functions handle those nulls as part of the string with no problem.

    strstr (a function from C) works with char* pointers, which point to C-style strings. Because C-style strings are null-terminated, they cannot handle embedded nulls. To this effect, strstr is documented as follows:

    找到子字符串   返回指向str1中第一次出现的str2的指针,如果str2不属于str1,则返回空指针。      匹配过程不包括终止的空字符,但在此停止。

    斜体部分与此处相关。