时间:2019-05-06 标签:c++std::ostringstreamvsstd::string::append [英] c++ std::ostringstream vs std::string::append

查看:15
本文介绍了时间:2019-05-06 标签:c++std::ostringstreamvsstd::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).

我知道的一个区别是,您可以将不同类型输出到输出流(如整数)中,而不是 string::append 接受的有限类型.

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 << "
";
std::string header = os.str();

对比

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

显然使用流更短,但我认为 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("
");

使用输出流,您可以:

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

但是内存使用和速度呢?特别是在大循环中使用时.

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 通过添加 Dietmar Kü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 使用适当的 std::string::reserve 没有理由比 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.

附言Pre-C++11 std::string 也不需要是顺序的,而几乎所有的库都将它实现为顺序的.您可以冒险或使用 std::vector 代替.您需要使用以下内容进行附加:

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 .纯粹是出于逻辑原因;vector 似乎更适合这份工作.您实际上并不想要字符串操作等.此外,我后来做的基准测试证明它的性能更好,或者可能只是因为我没有用 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.

这篇关于时间:2019-05-06 标签:c++std::ostringstreamvsstd::string::append的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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