0

来自愚蠢的文档

Small strings (<= 23 chars) are stored in-situ without memory allocation.
Medium strings (24 - 255 chars) are stored in malloc-allocated memory and copied eagerly.
Large strings (> 255 chars) are stored in malloc-allocated memory and copied lazily.

这些“小字符串”存储在哪里?

4

1 回答 1

1

这意味着成本std::string已经包括 23 个字符。更多需要额外的分配。

这意味着内部结构看起来大致如下:

struct string {
  // ...
  char internal[23];
  char* external;
};

这大概是为了使复制短字符串非常便宜,因为不必执行堆操作,而且它不需要任何delete清除调用。

于 2020-05-26T03:07:06.303 回答