本文介绍了C ++ std :: string附加vs push_back()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这确实是一个问题,只是出于我个人的利益,我无法通过文档确定。

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

我在附加的内容很复杂:

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和rope实现是被禁止。您应该期望每个添加的字符具有固定的摊销时间,或者为添加到末尾的字符串而增加的线性字符数具有线性的摊销时间。实现者可能仍然会对字符串做相对疯狂的事情(与例如 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 追加时, push_back 剥夺了底层实现的潜在有用的长度信息,该长度信息可能用于预分配空间。另一方面, 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附加vs push_back()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:47