问题描述
我想用矢量<&字符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;因为没有初始化它调整大小的缓冲区()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!