c++ - std::string 的 c_str() 函数如何返回空终止字符串?

标签 c++ string stdstring null-terminated c-str

<分区>

我不明白如果从 string.c_str() 返回的 char * 指向相同的缓冲区(并且不制作任何拷贝)它是如何以 null 终止的?该函数是否在字符串末尾添加空终止符?如果缓冲区没有任何额外的字节来存储它会发生什么?

谢谢。

最佳答案

自 C++11 起,字符串在内部以 null 结尾,并且 both c_str() and data() return the same thing .

关于c++ - std::string 的 c_str() 函数如何返回空终止字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50833602/

相关文章:

java - 使用 BufferedReader 从文件中读取 Long 和 String

c++ - 字符串反转,std::string 不允许字符赋值?

带有 u8、char8_t 和 std::string 的 C++20

c++ - Libtorch C++ 和 pytorch 的不同输出

c++ - c++相关查询中的类原型(prototype)

android - 在构建时下载 Android 字符串资源

android - 将 TextView 转换为字符串(某种)Android

c++ - 没有空闲存储内存分配的 std::string

c++ - 重载运算符

c++ - 获取成员变量值设置然后传递给函数c++