我听到一些人对 std::string 中的+"运算符和各种加速连接的解决方法表示担忧.这些真的有必要吗?如果是这样,在 C++ 中连接字符串的最佳方法是什么?
I heard a few people expressing worries about "+" operator in std::string and various workarounds to speed up concatenation. Are any of these really necessary? If so, what is the best way to concatenate strings in C++?
额外的工作可能不值得,除非你真的很需要效率.你可能会因为使用运算符 += 代替.
The extra work is probably not worth it, unless you really really need efficiency. You probably will have much better efficiency simply by using operator += instead.
现在在免责声明之后,我将回答您的实际问题......
Now after that disclaimer, I will answer your actual question...
STL 字符串类的效率取决于您使用的 STL 实现.
The efficiency of the STL string class depends on the implementation of STL you are using.
您可以保证效率并更好地控制自己通过c内置函数手动进行连接.
You could guarantee efficiency and have greater control yourself by doing concatenation manually via c built-in functions.
为什么 operator+ 效率不高:
看看这个界面:
template <class charT, class traits, class Alloc>
basic_string<charT, traits, Alloc>
operator+(const basic_string<charT, traits, Alloc>& s1,
const basic_string<charT, traits, Alloc>& s2)
可以看到在每个+之后返回了一个新对象.这意味着每次都使用一个新的缓冲区.如果您正在执行大量额外的 + 操作,则效率不高.
You can see that a new object is returned after each +. That means that a new buffer is used each time. If you are doing a ton of extra + operations it is not efficient.
为什么可以提高效率:
实施注意事项:
绳索数据结构:
如果您需要非常快速的连接,请考虑使用 绳索数据结构.
If you need really fast concatenations consider using a rope data structure.
这篇关于C++ 中的高效字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!