简体   繁体   English

如何在施工时为std :: vector保留内存?

[英]How do I reserve memory for a std::vector at construction time?

Normally I call reserve on a std::vector immediately after constructing it. 通常我在构造它之后立即调用std::vector上的reserve Wouldn't this typically cause the std::vector 's existing heap allocation to be destroyed and replaced with a new one? 这通常不会导致std::vector的现有堆分配被销毁并替换为新的堆分配吗? Is there a way to reserve the memory at construction time rather than allocate heap space and then immediately destroy it? 有没有办法在构建时保留内存而不是分配堆空间然后立即销毁它? Or is there an implemenatation trick within the std::vector to ensure this is not an issue? 或者是否在std::vector有一个实现技巧来确保这不是问题?

The available constructors only seem to be able to be useful for filling the std::vector with values, rather than reserving space explicitly. 可用的构造函数似乎只能用于使用值填充std::vector ,而不是显式保留空间。

Your question is based on a false premise, namely that a default-constructed std::vector<T> will perform a [zero-length] allocation. 您的问题基于一个错误的前提,即默认构造的std::vector<T>将执行[零长度]分配。

There is literally no reason for it to do so. 实际上没有理由这样做。 A fresh vector should have capacity zero (though this is required by sanity, not by the standard). 一个新的向量应该具有容量(虽然这是理智所要求的,而不是标准所要求的)。

As such, your goal is already inherently satisfied. 因此,您的目标已经天生就满足了。

To be blunt, the standard library is not quite that stupid. 说实话,标准库并不是那么愚蠢。

If the number of elements is known at compile-time by you, you can list-initialize the vector with such elements: 如果您在编译时知道元素的数量,则可以使用以下元素列出初始化向量:

 class MyClass 
 {
      int x, y, z;
      std::vector<int> v;

  public:
         MyClass(int X, int Y, int Z) : x(X), y(Y), z(Z), v{x, y, z}
         {}
 };

But this isn't very nice to maintain. 但这不是很好维护。 There are more advanced techniques, such as custom allocators that you can make std::vector to use that could take memory from a pre-allocated memory pool, for instance: I doubt you really need that, however. 有更多高级技术,例如自定义分配器,你可以使std::vector使用它可以从预先分配的内存池中获取内存,例如:我怀疑你真的需要它,但是。 Modern implementations would optimize a so simple problem easily. 现代实现可以轻松优化这么简单的问题。

The reason may be ironic, we are running out of function signature. 原因可能是具有讽刺意味的,我们的功能签名已经用完了。

The requirement comes from the using scenario that we exactly know how many elements we will save into vector but we really really don't like the n-duplicated elements constructor: 要求来自使用场景,我们确切地知道我们将保存到向量中的元素数量,但我们真的不喜欢n重复元素构造函数:

std::vector( size_type count, const T& value = T())

Unfortunately the signature is occupied by the above constructor. 不幸的是,签名被上面的构造函数占用。 Any other possible signature may cause issue. 任何其他可能的签名都可能导致问题 Eg 例如

  1. vector(size_type count, size_type reserve_count) will conflicts with above n-duplicated elements constructor for vector of T(size_type) . vector(size_type count, size_type reserve_count)将与T(size_type)向量的上述n重复元素构造函数冲突。

  2. vector(size_type count, const T& value = T(), size_type reserve_count) is a possible solution but it is too long and still boring. vector(size_type count, const T& value = T(), size_type reserve_count)是一种可能的解决方案,但它太长而且仍然很无聊。 We need to construct a default value we never use while calling auto v = vector<T>(0, T(), reserve_count) 我们需要在调用auto v = vector<T>(0, T(), reserve_count)构造一个我们从不使用的默认值

Other feasible solutions: 其他可行方案:

  1. Provides a function like make_pair/make_unique. 提供类似make_pair / make_unique的函数。

  2. Defines a Reserver which derived from Allocator, so we can use the constructor vector( const Allocator& alloc ) like 定义一个从Allocator派生的Reserver,所以我们可以使用构造函数向量(const Allocator&alloc)之类的

auto v = vector<Type>(new Reserver(reserve_count));

The object std::vector and its array of elements don't exist on the same contiguous memory block, otherwise its address would be changing every time you resize the array, making it impossible to keep a reliable reference of it. 对象std::vector及其元素数组不存在于同一个连续的内存块中,否则每次调整数组大小时其地址都会发生变化,从而无法保持对它的可靠引用。 The object's main body only contains control variables and a pointer to the actual array, and it will be instanced in the stack (assuming you are using it as a local variable) along with the other local variables. 对象的主体仅包含控制变量和指向实际数组的指针,它将在堆栈中实例化(假设您将其用作局部变量)以及其他局部变量。 And at this time the array will be empty, and probably represented by a pointer to nullptr . 此时数组将为空,可能由指向nullptr的指针表示。 So it doesn't matter if you reserve at construct time or right after it, there will be no significant optimization happening. 因此,如果您在构造时间或之后reserve ,则无关紧要,将不会发生重大优化。

If you want a std::vector with static size that is reserved instantly, you can just use a regular C array instead of std::vector . 如果你想要一个静态保留的静态大小的std::vector ,你可以使用常规的C数组而不是std::vector Just make sure it fits in the stack. 只要确保它适合堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM