向量储备c ++ [英] vector reserve c++

查看:184
本文介绍了向量储备c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的多维向量,随时改变大小。
当我只知道一个很好的近似的尺寸,有没有任何要使用vector.reserve()函数。

I have a very large multidimensional vector that changes in size all the time. Is there any point to use the vector.reserve() function when I only know a good approximation of the sizes.

所以基本上我有一个向量

So basically I have a vector

A [256 * 256] [x] [y]

其中,对于程序中的每次迭代,x从0到50,然后再次回到0。 y值可以每次不同,这意味着对于$ ​​b $ b [256 * 256] [y] 元素中的每一个,向量y可以具有不同的大小但仍小于256;

where x goes from 0 to 50 for every iteration in the program and then back to 0 again. The y values can differ every time, which means that for each of the [256*256][y] elements the vector y can be of a different size but still smaller than 256;

为了说明我的问题,这是我有:

So to clarify my problem this is what I have:

vector<vector<vector<int>>> A;
for(int i =0;i<256*256;i++){
  A.push_back(vector<vector<int>>());
  A[i].push_back(vector<int>());
  A[i][0].push_back(SOME_VALUE);
}

向向量中添加元素...

Add elements to the vector...

A.clear();

然后,我再次从顶部做同样的事情。

And after this I do the same thing again from the top.

何时以及如何为向量保留空间。
如果我正确理解了这一点,我会节省很多时间,如果我使用储备,因为我一直在改变大小?

When and how should I reserve space for the vectors. If I have understood this correctly I would save a lot of time if I would use reserve as I change the sizes all the time?

在某些情况下,保留 [256 * 256] [50] [256]

What would be the negative/positive sides of reserving the maximum size my vector can have which would be [256*256][50][256] in some cases.

BTW。我知道不同的矩阵模板和Boost,但已决定在这一个向量...

BTW. I am aware of different Matrix Templates and Boost, but have decided to go with vectors on this one...

编辑:
我也想知道如何使用多维数组中的保留函数。
如果我只在二维中保留向量,那么如果我在第三维中超出了它的容量,那么它会复制整个事物。

I was also wondering how to use the reserve function in multidimensional arrays. If I only reserve the vector in two dimensions will it then copy the whole thing if I exceed its capacity in the third dimension?

推荐答案

为帮助讨论,您可以考虑以下typedef:

To help with discussion you can consider the following typedefs:

typedef std::vector<int> int_t;   // internal vector
typedef std::vector<int_t> mid_t; // intermediate
typedef std::vector<mid_t> ext_t; // external

增长的成本(向量容量增加) int_t 将只影响这个特定向量的内容,不会影响任何其他元素。生成 mid_t 的成本需要复制该向量中所有存储的元素,也就是说,它需要所有 int_t 向量,这是相当昂贵的。生长 ext_t 的成本是巨大的:它将需要复制已经存储在容器中的元素

The cost of growing (vector capacity increase) int_t will only affect the contents of this particular vector and will not affect any other element. The cost of growing mid_t requires copying of all the stored elements in that vector, that is it will require all of the int_t vector, which is quite more costly. The cost of growing ext_t is huge: it will require copying all the elements already stored in the container.

现在,为了提高性能,更重要的是获得正确的 ext_t 大小(它似乎固定256 * 256在你的问题)。然后得到中间的 mid_t 大小正确,以便昂贵的重新分配是罕见的。

Now, to increase performance, it would be much more important to get the correct ext_t size (it seems fixed 256*256 in your question). Then get the intermediate mid_t size correct so that expensive reallocations are rare.

是巨大的,所以你可能想考虑更少的标准方法来解决你的问题。首先想到的是添加和额外的间接级别。如果不是保持实际向量,而是将智能指针保存到向量中,可以降低生成 mid_t ext_t vector(如果 ext_t size是固定的,只需使用 mid_t 的向量)。现在,这将意味着使用您的数据结构的代码将更复杂(或者更好地添加一个包装器来处理内联)。每个 int_t 向量将在内存中分配一次,并且不会在 mid_t ext_t 重新分配。重新分配 mid_t 的成本与分配的 int_t 向量的数量成正比,而不是实际插入的整数数。 / p>

The amount of memory you are talking about is huge, so you might want to consider less standard ways to solve your problem. The first thing that comes to mind is adding and extra level of indirection. If instead of holding the actual vectors you hold smart pointers into the vectors you can reduce the cost of growing the mid_t and ext_t vectors (if ext_t size is fixed, just use a vector of mid_t). Now, this will imply that code that uses your data structure will be more complex (or better add a wrapper that takes care of the indirections). Each int_t vector will be allocated once in memory and will never move in either mid_t or ext_t reallocations. The cost of reallocating mid_t is proportional to the number of allocated int_t vectors, not the actual number of inserted integers.

using std::tr1::shared_ptr; // or boost::shared_ptr
typedef std::vector<int> int_t;
typedef std::vector< shared_ptr<int_t> > mid_t;
typedef std::vector< shared_ptr<mid_t> > ext_t;

另一件你应该考虑的事情是 std :: vector: :clear()不释放向量中分配的内部空间,只销毁包含的对象,并将大小设置为0.也就是调用 clear()永远不会释放内存。在向量中实际释放所分配的存储器的模式是:

Another thing that you should take into account is that std::vector::clear() does not free the allocated internal space in the vector, only destroys the contained objects and sets the size to 0. That is, calling clear() will never release memory. The pattern for actually releasing the allocated memory in a vector is:

typedef std::vector<...> myvector_type;
myvector_type myvector;
...
myvector.swap( myvector_type() ); // swap with a default constructed vector

这篇关于向量储备c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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