c++ - 如何在 std::string 中存储 const char*?

标签 c++ string pointers stl

    char       *buffer1 = "abc";

    const char *buffer2 = (const char*) buffer;

    std :: string str (buffer2);

这行得通,但我想声明 std::string 对象,即 str一次 并多次使用它来存储不同的常量字符*。

出路在哪里?

最佳答案

您可以重新分配:

const char *buf1 = "abc";
const char *buf2 = "def";

std::string str(buf1);

str = buf2; // Calls str.operator=(const char *)

关于c++ - 如何在 std::string 中存储 const char*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6214160/

相关文章:

c++ - 程序是如何通过链接器的?

c++ - 默认复制移动构造函数效率不同

java - 如何复制二维字符串数组?

python - 在 Python 中修改字符串

pointers - Fortran 解除分配

c - C 中的锯齿状数组

c++ - open-std.org 是 C++11 标准文档的官方网站吗?

c++ - Valgrind 在 ATI 上运行 opengl 程序时报告很多错误 - 我应该担心吗?

python - 在 Python 中将字符串拆分为列表(但不分隔相邻数字)

c++ - 为什么这个 sizeof 运算符会出错?