问题描述
这确实是一个出于我个人兴趣的问题,我无法通过文档确定.
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"的大部分文档都是为此编写的)中,复杂性未指定,因为库实现者被允许这样做字符串的 Copy-On-Write 或绳索式"内部表示.例如,如果一个字符被修改并且正在进行共享,则 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_back
和append
时,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.
这篇关于C++ std::string append vs push_back()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!