<legend id='Emcx3'><style id='Emcx3'><dir id='Emcx3'><q id='Emcx3'></q></dir></style></legend>
  • <small id='Emcx3'></small><noframes id='Emcx3'>

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

      1. 使用向量<char>作为缓冲区而不在 resize() 上初始化它

        时间:2023-06-29

        • <small id='3rnyQ'></small><noframes id='3rnyQ'>

            <bdo id='3rnyQ'></bdo><ul id='3rnyQ'></ul>

              1. <legend id='3rnyQ'><style id='3rnyQ'><dir id='3rnyQ'><q id='3rnyQ'></q></dir></style></legend>

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

                  <tbody id='3rnyQ'></tbody>
                <tfoot id='3rnyQ'></tfoot>
                • 本文介绍了使用向量<char>作为缓冲区而不在 resize() 上初始化它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 vector 作为缓冲区.该界面非常适合我的需求,但在将其调整为超出当前大小时会降低性能,因为内存已初始化.我不需要初始化,因为在任何情况下数据都会被某些第三方 C 函数覆盖.有没有办法或特定的分配器来避免初始化步骤?请注意,我确实想使用 resize(),而不是像 reserve()capacity() 之类的其他技巧,因为我需要 size() 始终表示我的缓冲区"的有效大小,而 capacity()resize() 之后可能大于其大小>,所以,我不能依赖 capacity() 作为我的应用程序的重要信息.此外,向量的(新)大小永远不会提前知道,所以我不能使用 std::array.如果无法以这种方式配置矢量,我想知道我可以使用哪种容器或分配器来代替 vector.唯一的要求是,vector 的替代方案最多必须基于 STL 或 Boost.我可以访问 C++11.

                  I want to use vector<char> as a buffer. The interface is perfect for my needs, but there's a performance penalty when resizing it beyond its current size, since the memory is initialized. I don't need the initialization, since the data will be overwritten in any case by some third-party C functions. Is there a way or a specific allocator to avoid the initialization step? Note that I do want to use resize(), not other tricks like reserve() and capacity(), because I need size() to always represent the significative size of my "buffer" at any moment, while capacity() might be greater than its size after a resize(), so, again, I cannot rely on capacity() as a significative information for my application. Furthemore, the (new) size of the vector is never known in advance, so I cannot use std::array. If vector cannot be configured that way, I'd like to know what kind of container or allocator I could use instead of vector<char, std::alloc>. The only requirement is that the alternative to vector must at most be based on STL or Boost. I have access to C++11.

                  推荐答案

                  标准库中没有任何内容可以满足您的要求,我也不知道 boost 中的任何内容.

                  There's nothing in the standard library that meets your requirements, and nothing I know of in boost either.

                  我能想到三个合理的选择:

                  There are three reasonable options I can think of:

                  • 暂时坚持使用 std::vector,在代码中留下注释,如果这会导致您的应用程序出现瓶颈,请返回.
                  • 使用带有空 construct/destroy 方法的自定义分配器 - 并希望您的优化器足够聪明以删除对它们的任何调用.
                  • 围绕动态分配的数组创建一个包装器,仅实现您需要的最少功能.
                  • Stick with std::vector for now, leave a comment in the code and come back to it if this ever causes a bottleneck in your application.
                  • Use a custom allocator with empty construct/destroy methods - and hope your optimiser will be smart enough to remove any calls to them.
                  • Create a wrapper around a a dynamically allocated array, implementing only the minimal functionality that you require.

                  这篇关于使用向量<char>作为缓冲区而不在 resize() 上初始化它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何从 boost::shared_ptr 释放指针? 下一篇:提升 ASIO 流缓冲

                  相关文章

                • <legend id='uhc2d'><style id='uhc2d'><dir id='uhc2d'><q id='uhc2d'></q></dir></style></legend>

                    <i id='uhc2d'><tr id='uhc2d'><dt id='uhc2d'><q id='uhc2d'><span id='uhc2d'><b id='uhc2d'><form id='uhc2d'><ins id='uhc2d'></ins><ul id='uhc2d'></ul><sub id='uhc2d'></sub></form><legend id='uhc2d'></legend><bdo id='uhc2d'><pre id='uhc2d'><center id='uhc2d'></center></pre></bdo></b><th id='uhc2d'></th></span></q></dt></tr></i><div id='uhc2d'><tfoot id='uhc2d'></tfoot><dl id='uhc2d'><fieldset id='uhc2d'></fieldset></dl></div>
                  1. <small id='uhc2d'></small><noframes id='uhc2d'>

                    • <bdo id='uhc2d'></bdo><ul id='uhc2d'></ul>
                  2. <tfoot id='uhc2d'></tfoot>