C ++:向量和列表之间的混合:像std :: rope? [英] C++: mixture between vector and list: something like std::rope?
问题描述
当存储一堆项目,我不需要随机访问容器,我使用 std :: list
这是很好。然而,有时(特别是当我把条目推回到后面,从来没有删除中间某处),我希望我有一些更好的性能结构来添加条目。
When storing a bunch of items and I don't need random access to the container, I am using an std::list
which is mostly fine. However, sometimes (esp. when I just push back entries to the back and never delete somewhere in the middle), I wish I had some structure with better performance for adding entries.
std :: vector
是错误的,因为:
- 必须重新分配
- 它不适用于大量的数据(因为你不能总是获得非常大的连续可用内存)。
std :: list
是错误的,因为:
- 它对每个push_back进行分配。
- It makes an allocation on every single push_back. That is slow and leads to a lot of memory fragmentation.
因此,中间的东西就是我想要的东西。
So, something in between is what I want.
基本上,我想要像 std :: list< boost :: array< T,100> >
左右。或者可以代替 100
,让它 4096 / sizeof(T)
。也许 std :: list< std :: vector< T> >
,并且第一向量可以小,然后进一步向量可以增长。实际上我想要隐藏的使用,所以我可以做一个 mycontainer.push_back(x)
。
Basically, I want something like std::list< boost::array<T, 100> >
or so. Or maybe instead of 100
, let it be 4096/sizeof(T)
. Maybe also std::list< std::vector<T> >
and the first vectors can be small and then further ones can grow. Actually I want to have that hidden from the usage, so I can just do a mycontainer.push_back(x)
.
std :: rope
有点类似,但它在标准中不可用。
std::rope
is a bit similar to that but it is not available in the standard.
有这样的东西
推荐答案
您是否考虑过使用 std :: deque
?它的元素不是连续存储的,但它允许随机访问元素;如果你只是在序列的开头或结尾插入元素,它可能会比 std :: vector
更好的性能。
Have you considered using std::deque
? Its elements are not stored contiguously but it does allow random access to elements; if you are only inserting elements at the beginning or end of the sequence, it may give better performance than a std::vector
.
这篇关于C ++:向量和列表之间的混合:像std :: rope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!