Can a std::string contain embedded nulls?

前端 未结 4 1520
臣服心动
臣服心动 2020-12-01 15:24

For regular C strings, a null character \'\\0\' signifies the end of data.

What about std::string, can I have a string with embedded null c

4条回答
  •  时光取名叫无心
    2020-12-01 16:07

    Yep this is valid.

    You can have a null character in the middle of the string.

    However, if you use a std::string with a null character in the middle with a c string function your in undefined behaviour town - and nobody wants to be there!!!:

     int n = strlen( strWithNullInMiddle.c_str() ); // Boom!!!
    

提交回复
热议问题