• <i id='vCra7'><tr id='vCra7'><dt id='vCra7'><q id='vCra7'><span id='vCra7'><b id='vCra7'><form id='vCra7'><ins id='vCra7'></ins><ul id='vCra7'></ul><sub id='vCra7'></sub></form><legend id='vCra7'></legend><bdo id='vCra7'><pre id='vCra7'><center id='vCra7'></center></pre></bdo></b><th id='vCra7'></th></span></q></dt></tr></i><div id='vCra7'><tfoot id='vCra7'></tfoot><dl id='vCra7'><fieldset id='vCra7'></fieldset></dl></div>
  • <legend id='vCra7'><style id='vCra7'><dir id='vCra7'><q id='vCra7'></q></dir></style></legend>
    • <bdo id='vCra7'></bdo><ul id='vCra7'></ul>

    <small id='vCra7'></small><noframes id='vCra7'>

      <tfoot id='vCra7'></tfoot>

        时间:2019-05-06 标签:c++std::ostringstreamvsstd::string::append

        c++ std::ostringstream vs std::string::append(时间:2019-05-06 标签:c++std::ostringstreamvsstd::string::append)

                <tbody id='bFRHP'></tbody>
                <bdo id='bFRHP'></bdo><ul id='bFRHP'></ul>
              • <tfoot id='bFRHP'></tfoot>
                • <small id='bFRHP'></small><noframes id='bFRHP'>

                • <i id='bFRHP'><tr id='bFRHP'><dt id='bFRHP'><q id='bFRHP'><span id='bFRHP'><b id='bFRHP'><form id='bFRHP'><ins id='bFRHP'></ins><ul id='bFRHP'></ul><sub id='bFRHP'></sub></form><legend id='bFRHP'></legend><bdo id='bFRHP'><pre id='bFRHP'><center id='bFRHP'></center></pre></bdo></b><th id='bFRHP'></th></span></q></dt></tr></i><div id='bFRHP'><tfoot id='bFRHP'></tfoot><dl id='bFRHP'><fieldset id='bFRHP'></fieldset></dl></div>
                  <legend id='bFRHP'><style id='bFRHP'><dir id='bFRHP'><q id='bFRHP'></q></dir></style></legend>

                • 本文介绍了时间: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

                      <tbody id='RsN4C'></tbody>
                  • <i id='RsN4C'><tr id='RsN4C'><dt id='RsN4C'><q id='RsN4C'><span id='RsN4C'><b id='RsN4C'><form id='RsN4C'><ins id='RsN4C'></ins><ul id='RsN4C'></ul><sub id='RsN4C'></sub></form><legend id='RsN4C'></legend><bdo id='RsN4C'><pre id='RsN4C'><center id='RsN4C'></center></pre></bdo></b><th id='RsN4C'></th></span></q></dt></tr></i><div id='RsN4C'><tfoot id='RsN4C'></tfoot><dl id='RsN4C'><fieldset id='RsN4C'></fieldset></dl></div>

                    1. <legend id='RsN4C'><style id='RsN4C'><dir id='RsN4C'><q id='RsN4C'></q></dir></style></legend>
                      • <bdo id='RsN4C'></bdo><ul id='RsN4C'></ul>

                          <small id='RsN4C'></small><noframes id='RsN4C'>

                          <tfoot id='RsN4C'></tfoot>