c++ - 当我们在 C++ 中更改字符串的值时会发生什么

标签 c++

#include <iostream>
#include <string>
using namespace std;
int main () {
  string str = "Hello";
  str = "Hello World";
  cout<<str<<endl;
}

字符串是如何处理内存的?

最佳答案

回复:

How is the memory handled by the string?

自动地。

除其他事项外,这意味着无法为 std::string 提供一个外部创建的缓冲区。所以效率有点低。从好的方面来说,std::string(相对于 std::vector)的 swap 要求意味着它可以使用 < strong>小缓冲区优化,短字符串存储,无需动态分配,有助于提高效率。

关于c++ - 当我们在 C++ 中更改字符串的值时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41531152/

相关文章:

c++ - 如何正确解除分配 C++ vector ?

c++ - 运算符新字段值在多重继承中有错误的偏移量

c++ - 使用文件描述符移动对象

c++ - C++ 中的 "new"关键字有什么问题?

c++ - 试图在 C++ 中对一个简单的字符串进行排序

Android 和 Crypto++ AES 128 位加密结果不匹配

c++ - CX0030 : Error: Expression cannot be evaluated

c++ - 使用 gdb 跟踪所有用户定义的函数调用

c++ - 我可以实例化一个 std::reference_wrapper<T> 吗,其中 T 是一个不完整的类型?

c++ - 如何访问类成员