使用矢量<焦炭>因为没有初始化它调整大小的缓冲区() [英] Using vector<char> as a buffer without initializing it on resize()

查看:123
本文介绍了使用矢量<焦炭>因为没有初始化它调整大小的缓冲区()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用矢量<&字符GT; 作为缓冲。接口非常适合我的需求,但调整其大小,当超过其目前的规模,因为内存初始化有性能损失。我并不需要初始化,因为数据将在任何情况下,由一些第三方C函数被覆盖。有没有一种方法或特定的分配,以避免初始化步骤?请注意,我想使用调整(),而不是其他的小把戏,比如储备()容量(),因为我需要尺寸()来随时重新present我的缓冲在任何时刻的有意义的大小,而容量()可能会比其规模更大的后调整(),所以,再一次,我可以不依赖容量()为我的应用程序有意义的信息。 Furthemore,矢量的(新)的大小是永远不会预先知道,所以我不能使用的std ::阵列。如果向量不能这样配置的,我想知道我可以使用,而不是矢&lt什么样的容器或分配器;焦炭,性病:: ALLOC> 。唯一的要求是,替代矢量必须至多可以基于STL或升压。我有机会获得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.

推荐答案

有什么在能满足你需求的标准库中,并没有我在升压知道任。

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 ::矢量坚持现在,留在code注释,然后回来,如果这个不断产生的瓶颈在应用程序中。

  • 使用了自定义的分配器空结构 / 摧毁方法 - 并希望您优化器将是足够聪明的去除他们的呼叫。

  • 创建围绕一个动态分配的数组的包装,只实现您需要的最少的功能。

  • 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.

这篇关于使用矢量&lt;焦炭&GT;因为没有初始化它调整大小的缓冲区()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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