将 const char* 添加到 string_view

Adding const char* to string_view

我有一个 string_view:

std::string_view view;

如何向它附加诸如 const char* 之类的内容?例如:

std::string_view view = "hello";
view += " world"; // Doesn't work

此外,如何创建具有指定大小的 string_view? 例如:

std::string_view view(100); // Creates a string_view with an initial size of 100 bytes

std::string_view 是一个 read-only 查看存储在内存中其他地方的现有 char[] 缓冲区,或 chars可由一系列迭代器访问。您不能向 std::string_view.

添加新数据

对于您的尝试,您需要使用 std::string 来代替,例如:

std::string s = "hello";
s += " world";
std::string s(100, '[=11=]');