C ++中的有效字符串连接 [英] Efficient string concatenation in C++

查看:251
本文介绍了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)

您可以看到每个+后都返回一个新对象。这意味着每次使用一个新的缓冲区。如果你做了一吨额外的+操作,这是不高效的。



为什么可以提高效率



  • std :: string类不了解字符串的最大大小,也不了解您的字符串的最大大小连接到它。你可能有这个知识,可以做的事情,基于拥有这些信息。这将导致较少的重新分配。

  • 您将手动控制缓冲区,以确保在不希望发生这种情况时,不会将整个字符串复制到新缓冲区中。

  • 您可以将堆栈用于缓冲​​区,而不是堆,效率更高。

  • string +运算符将创建一个新的字符串对象并使用新的缓冲区返回它。



实施注意事项



    $
  • 保留一个指向字符串结尾和开始的指针,或者只是开始,并使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆