<legend id='idAJ8'><style id='idAJ8'><dir id='idAJ8'><q id='idAJ8'></q></dir></style></legend>

    • <bdo id='idAJ8'></bdo><ul id='idAJ8'></ul>
    <tfoot id='idAJ8'></tfoot>

    1. <small id='idAJ8'></small><noframes id='idAJ8'>

    2. <i id='idAJ8'><tr id='idAJ8'><dt id='idAJ8'><q id='idAJ8'><span id='idAJ8'><b id='idAJ8'><form id='idAJ8'><ins id='idAJ8'></ins><ul id='idAJ8'></ul><sub id='idAJ8'></sub></form><legend id='idAJ8'></legend><bdo id='idAJ8'><pre id='idAJ8'><center id='idAJ8'></center></pre></bdo></b><th id='idAJ8'></th></span></q></dt></tr></i><div id='idAJ8'><tfoot id='idAJ8'></tfoot><dl id='idAJ8'><fieldset id='idAJ8'></fieldset></dl></div>
    3. 如何重用 ostringstream?

      How to reuse an ostringstream?(如何重用 ostringstream?)

        <small id='3otNa'></small><noframes id='3otNa'>

          <legend id='3otNa'><style id='3otNa'><dir id='3otNa'><q id='3otNa'></q></dir></style></legend>

            <tbody id='3otNa'></tbody>

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

                <tfoot id='3otNa'></tfoot>
                本文介绍了如何重用 ostringstream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想清除并重用一个 ostringstream(和底层缓冲区),这样我的应用程序就不必进行那么多的分配.如何将对象重置为其初始状态?

                I'd like to clear out and reuse an ostringstream (and the underlying buffer) so that my app doesn't have to do as many allocations. How do I reset the object to its initial state?

                推荐答案

                我以前用过 clear 和 str 的序列:

                I've used a sequence of clear and str in the past:

                // clear, because eof or other bits may be still set. 
                s.clear();
                s.str("");
                

                这对输入和输出字符串流都完成了.或者,您可以手动清除,然后寻找合适的顺序开始:

                Which has done the thing for both input and output stringstreams. Alternatively, you can manually clear, then seek the appropriate sequence to the begin:

                s.clear();
                s.seekp(0); // for outputs: seek put ptr to start
                s.seekg(0); // for inputs: seek get ptr to start
                

                这将通过覆盖当前输出缓冲区中的任何内容来防止 str 进行一些重新分配.结果是这样的:

                That will prevent some reallocations done by str by overwriting whatever is in the output buffer currently instead. Results are like this:

                std::ostringstream s;
                s << "hello";
                s.seekp(0);
                s << "b";
                assert(s.str() == "bello");
                

                如果你想在 c 函数中使用字符串,你可以使用 std::ends,像这样放置一个终止空值:

                If you want to use the string for c-functions, you can use std::ends, putting a terminating null like this:

                std::ostringstream s;
                s << "hello";
                s.seekp(0);
                s << "b" << std::ends;
                assert(s.str().size() == 5 && std::strlen(s.str().data()) == 1);
                

                std::ends 是弃用的 std::strstream 的产物,它能够直接写入您在堆栈上分配的字符数组.您必须手动插入终止空值.但是,std::ends 并没有被弃用,我认为因为它在上述情况下仍然很有用.

                std::ends is a relict of the deprecated std::strstream, which was able to write directly to a char array you allocated on the stack. You had to insert a terminating null manually. However, std::ends is not deprecated, i think because it's still useful as in the above cases.

                这篇关于如何重用 ostringstream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                • <tfoot id='WQxhy'></tfoot>

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

                      • <bdo id='WQxhy'></bdo><ul id='WQxhy'></ul>
                      • <small id='WQxhy'></small><noframes id='WQxhy'>

                          <legend id='WQxhy'><style id='WQxhy'><dir id='WQxhy'><q id='WQxhy'></q></dir></style></legend>