如何在两个维度中使用具有大范围大小的std :: vectors管理结构的std :: vector? [英] How would you manage a std::vector of structs with std::vectors with large range of sizes in both dimentions?

查看:112
本文介绍了如何在两个维度中使用具有大范围大小的std :: vectors管理结构的std :: vector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组类似的结构:

typedef struct {
   int a;
   int b;
} ITEM;

typedef struct {
   int orderID;
   std::vector<ITEM> items;
} ORDER;

typedef struct {
   int orderSetID;
   std::vector<ORDER> Orders;
} ORDER_SET;

问题在于订单数量在100,000到10,000,000之间,ORDER中的ITEMS数量在1到500之间.

The problem is that the number of orders ranges between 100,000 to 10,000,000 and the number of ITEMS in an ORDER ranges from 1 to 500.

问题是,当我构建ORDER_SET时,我不知道会有多少个ORDER.我确实知道当我添加ORDER时将有多少个ITEMS. 这里有一些问题:

The issue is, as I build the ORDER_SET, I don't know how many ORDERs there will be. I do know when I add an ORDER how many ITEMS there will be. Here are some problems:

1)理想的情况是,一旦我使用Orders.resize()为所有ORDER分配了内存,就可以重用该内存,但是看来Orders.clear()确实删除了所有内存.

1) Ideally once I allocate the memory for all of the ORDERs using Orders.resize(), I'd be able to reuse the memory, but it appears that Orders.clear() does delete it all.

2)我从一个稍微合理的Orders大小开始,比如说Orders.resize(500,000),但是问题是当我命中大量订单时,重新调整Orders向量的大小会花费很多时间,因为它必须复制所有向量.

2) I start with a somewhat reasonable size for Orders, say Orders.resize(500,000), but the problem is that when I hit large numbers of orders the resize of the Orders vector takes forever because it has to copy all of the ORDER.item vectors.

好像与存储备用矩阵类似的问题,问题是在创建结构之前我也不知道结构的大小.

Seems like a similar issue to storing spare matrixes, the issue being that I also don't know how large the structure is going to be before I create it.

其他信息:
1)使用Visual Studio 2008 2)如以下评论中所述,我可以通过在以下两个字段中替换items来改善ORDER_SET的构造,该ORDER_SET在合理的时间内包含10000000个订单:

Additional information:
1) Using Visual Studio 2008 2) As posted in the comments below I was able to improve the construction of an ORDER_SET which contains 10000000 Orders in a reasonable time by replacing items with two fields leaving:

typedef struct {
    int orderID;
   ITEM singleItem;
   std::vector<ITEM> *pItems;
} ORDER;

我将所有pItem放入一个单独的向量中,以后再用于删除.

I put all of the pItems into a separate vector which I use for deleting later.

现在剩下的大问题是,调用ORDER_SET.Orders.clear()似乎需要花费一些时间.我不确定为什么.

Now the big issue left is that it appears that calling ORDER_SET.Orders.clear() takes quite some time. I'm not sure why though.

是否有像clear()这样的调用不会释放内存,而只是将end设置为begin?一旦向量变大,实际上没有任何理由释放内存,因为我可能会再次需要它.

Is there a call like clear() which doesn't release the memory, but just sets end to begin? Once the vector has gotten that large, there really isn't any reason to free the memory, since I might need it again.

推荐答案

第二个问题可以通过使用具有vector s移动语义的C ++ 11来解决,因此可以移动ORDER,而不仅仅是深度复制.

Second issue can be fixed by using C++11 with move semantics of vectors, so ORDER can be moved, not just deep copied.

ORDER.Orders.clear()需要一些时间,因为它必须在所有元素上调用析构函数.

ORDER.Orders.clear() takes some time because it must call destructors on all elements.

这篇关于如何在两个维度中使用具有大范围大小的std :: vectors管理结构的std :: vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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