C ++ std :: ostringstream vs std :: string :: append [英] c++ std::ostringstream vs std::string::append

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

问题描述

在所有使用某种缓冲的示例中,我看到它们使用流而不是字符串。 std :: ostringstream和<<运算符与使用string.append不同。哪一个更快,哪一个使用更少的资源(内存)。

In all examples that use some kind of buffering I see they use stream instead of string. How is std::ostringstream and << operator different than using string.append. Which one is faster and which one uses less resourses (memory).

我知道的一个区别是,您可以将不同的类型输出到输出流(例如整数)中,而不是

One difference I know is that you can output different types into output stream (like integer) rather than the limited types that string::append accepts.

这里是一个示例:

std::ostringstream os;
os << "Content-Type: " << contentType << ";charset=" << charset << "\r\n";
std::string header = os.str();

vs

std::string header("Content-Type: ");
header.append(contentType);
header.append(";charset=");
header.append(charset);
header.append("\r\n");

显然使用流更短,但我认为append返回对字符串的引用,因此可以这样写

Obviously using stream is shorter, but I think append returns reference to the string so it can be written like this:

std::string header("Content-Type: ");
header.append(contentType)
  .append(";charset=")
  .append(charset)
  .append("\r\n");

通过输出流,您可以执行以下操作:

And with output stream you can do:

std::string content;
...
os << "Content-Length: " << content.length() << "\r\n";

但是内存使用和速度如何?

But what about memory usage and speed? Especially when used in a big loop.

更新:

要更多明确的问题是:我应该使用哪个?为什么? 为了性能和内存……我认为基准测试是唯一的方法,因为每种实现方法都可能不同。

To be more clear the question is: Which one should I use and why? Is there situations when one is preferred or the other? For performance and memory ... well I think benchmark is the only way since every implementation could be different.

更新2:

好吧,我不清楚我应该从答案中使用什么,这意味着它们中的任何一个都可以工作,再加上向量。 Cubbi 做得很好,使用DietmarKühl进行了基准测试,最大的不同是这些对象的构造。如果您正在寻找答案,也应该进行检查。我会再等一些其他答案(请看以前的更新),如果我没有得到答案,我想我会接受Tolga的答案,因为在使用vector之前,他的建议已经完成,这意味着vector应该减少了对资源的需求。

Well I don't get clear idea what should I use from the answers which means that any of them will do the job, plus vector. Cubbi did nice benchmark with the addition of Dietmar Kühl that the biggest difference is construction of those objects. If you are looking for an answer you should check that too. I'll wait a bit more for other answers (look previous update) and if I don't get one I think I'll accept Tolga's answer because his suggestion to use vector is already done before which means vector should be less resource hungry.

推荐答案

std :: ostringstream 不一定存储为顺序数组内存中的字符数。实际上,在发送这些HTTP标头时,您实际上需要连续的字符数组,并且可能会复制/修改内部缓冲区以使其具有顺序性。

std::ostringstream is not necessarily stored as a sequential array of characters in memory. You would actually need to have continuous array of characters while sending those HTTP headers and that might copy/modify the internal buffer to make it sequential.

使用适当的 std :: string :: reserve 的std :: string 没有理由比 std :: ostringstream慢

std::string using appropriate std::string::reserve has no reason to act slower than std::ostringstream in this situation.

但是, std :: ostringstream 可能会更快地添加如果您绝对不知道必须保留的大小。如果使用 std :: string 且字符串增大,则最终需要重新分配和复制整个缓冲区。最好使用一个 std :: ostringstream :: str()一次使数据顺序化,而不是采用多种重新分配方式。

However, std::ostringstream is probably faster for appending if you absolutely have no idea about the size you have to reserve. If you use std::string and your string grows, it eventually requires reallocation and copying of whole buffer. It would be better to use one std::ostringstream::str() to make the data sequential at once compared to multiple re-allocations that would happen otherwise.

PS C ++ 11之前的 std :: string 也不要求是顺序的,尽管几乎所有库都将其实现为顺序的。您可以冒险,也可以使用 std :: vector< char> 。您将需要使用以下内容进行追加:

P.S. Pre-C++11 std::string is not required to be sequential either, whilst almost all libraries implement it as sequential. You could risk it or use std::vector<char> instead. You would need to use the following to do appending:

char str[] = ";charset=";
vector.insert(vector.end(), str, str + sizeof(str) - 1);

std :: vector< char> 性能最好,因为它可能更便宜,但是与 std :: string 以及它们实际花费的时间相比,它可能并不重要。我做过与您尝试的操作类似的操作,并且之前使用了 std :: vector< char> 。纯粹是出于逻辑原因;向量似乎更适合这份工作。您实际上并不需要字符串操作等。另外,我后来所做的基准测试证明了它的性能更好,或者可能只是因为我使用 std :: string 不能很好地实现操作。

std::vector<char> would be best for performance because it is most probably cheaper to construct, but it is probably not of importance compared to std::string and the actual time they take to construct. I have done something similar to what you are trying and went with std::vector<char> before. Purely because of logical reasons; vector seemed to fit the job better. You do not actually want string manipulations or such. Also, benchmarks I did later proved it to perform better or maybe it was only because I did not implement operations well enough with std::string.

在选择时,具有您的需求要求和最少附加功能的容器通常效果最好。

While choosing, the container that has requirements for your needs and minimal extra features usually does the job best.

这篇关于C ++ std :: ostringstream vs std :: string :: append的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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