C++ std::string 追加 vs push_back()

2022-01-22 00:00:00 string append c++ push-back

这确实是一个仅出于我自己的兴趣的问题,我无法通过文档确定.

This really is a question just for my own interest I haven't been able to determine through the documentation.

我在 http://www.cplusplus.com/reference/string/string/ 上看到append 具有复杂性:

I see on http://www.cplusplus.com/reference/string/string/ that append has complexity:

未指定,但通常在新字符串长度中达到线性."

"Unspecified, but generally up to linear in the new string length."

而 push_back() 具有复杂性:

while push_back() has complexity:

未指定;一般为摊销常数,但在新字符串长度上可达线性."

"Unspecified; Generally amortized constant, but up to linear in the new string length."

作为一个玩具示例,假设我想将字符foo"附加到字符串中.会

As a toy example, suppose I wanted to append the characters "foo" to a string. Would

myString.push_back('f');
myString.push_back('o');
myString.push_back('o');

myString.append("foo");

完全一样吗?或者有什么不同吗?您可能认为 append 会更有效,因为编译器会知道将字符串扩展至指定字符数需要多少内存,而 push_back 可能需要在每次调用时保护内存?

amount to exactly the same thing? Or is there any difference? You might figure that append would be more efficient because the compiler would know how much memory is required to extend the string the specified number of characters, while push_back may need to secure memory each call?

推荐答案

在 C++03 中(cplusplus.com"的大部分文档都是为此编写的),由于允许库实现者这样做,因此未指定复杂性字符串的写时复制或绳索式"内部表示.例如,如果一个字符被修改并且正在进行共享,则 COW 实现可能需要复制整个字符串.

In C++03 (for which most of "cplusplus.com"'s documentation is written), the complexities were unspecified because library implementers were allowed to do Copy-On-Write or "rope-style" internal representations for strings. For instance, a COW implementation might require copying the entire string if a character is modified and there is sharing going on.

在 C++11 中,COW 和绳索实现被禁止.您应该期望每个添加的字符的固定摊销时间或添加到最后附加到字符串的字符数的线性摊销时间.实现者可能仍然对字符串做相对疯狂的事情(与 std::vector 相比),但大多数实现将仅限于小字符串优化"之类的事情.

In C++11, COW and rope implementations are banned. You should expect constant amortized time per character added or linear amortized time in the number of characters added for appending to a string at the end. Implementers may still do relatively crazy things with strings (in comparison to, say std::vector), but most implementations are going to be limited to things like the "small string optimization".

在比较 push_backappend 时,push_back 剥夺了可能用于预分配空间的潜在有用长度信息的底层实现.另一方面,append 要求实现遍历输入两次才能找到该长度,因此性能增益或损失将取决于许多未知因素,例如尝试追加之前的字符串.也就是说,差异可能非常非常非常小.为此请使用 append ―― 它的可读性要高得多.

In comparing push_back and append, push_back deprives the underlying implementation of potentially useful length information which it might use to preallocate space. On the other hand, append requires that an implementation walk over the input twice in order to find that length, so the performance gain or loss is going to depend on a number of unknowable factors such as the length of the string before you attempt the append. That said, the difference is probably extremely Extremely EXTREMELY small. Go with append for this -- it is far more readable.

相关文章