C ++中的有效字符串连接 [英] Efficient string concatenation in C++
问题描述
我听说有几个人对std :: string中的+运算符表示担心,以及各种解决方法来加速连接。这些是真的有必要吗?如果是这样,在C ++中连接字符串的最好方法是什么?
额外的工作可能不值得,除非你真的需要效率。你可能会有更好的效率,只需使用operator + =代替。
现在放弃免责声明后,我将回答您的实际问题...
STL字符串的效率
您可以保证效率和拥有更强的控制通过c内置函数手动进行连接。
为什么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)
您可以看到每个+后都返回一个新对象。这意味着每次使用一个新的缓冲区。如果你做了一吨额外的+操作,这是不高效的。
为什么可以提高效率:
实施注意事项:
- $
- 保留一个指向字符串结尾和开始的指针,或者只是开始,并使用start +长度作为字符串的长度。 offset来查找字符串的结尾。
- 确保您要存储字符串的缓冲区足够大,因此您不需要重新分配数据 。
- 使用strcpy的strcat,所以你不需要遍历字符串的长度来找到字符串的结尾。
Rope数据结构:
如果您需要真正快速连接,请考虑使用绳索数据结构。
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...
The efficiency of the STL string class depends on the implementation of STL you are using.
You could guarantee efficiency and have greater control yourself by doing concatenation manually via c built-in functions.
Why operator+ is not efficient:
Take a look at this interface:
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.
Why you can make it more efficient:
- You are guaranteeing efficiency instead of trusting a delegate to do it efficiently for you
- the std::string class knows nothing about the max size of your string, nor how often you will be concatenating to it. You may have this knowledge and can do things based on having this information. This will lead to less re-allocations.
- You will be controlling the buffers manually so you can be sure that you won't copy the whole string into new buffers when you don't want that to happen.
- You can use the stack for your buffers instead of the heap which is much more efficient.
- string + operator will create a new string object and return it hence using a new buffer.
Considerations for implementation:
- Keep track of the string length.
- Keep a pointer to the end of the string and the start, or just the start and use the start + the length as an offset to find the end of the string.
- Make sure the buffer you are storing your string in, is big enough so you don't need to re-allocate data
- Use strcpy instead of strcat so you don't need to iterate over the length of the string to find the end of the string.
Rope data structure:
If you need really fast concatenations consider using a rope data structure.
这篇关于C ++中的有效字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!