简体   繁体   English

C ++:vector和list之间的混合:类似于std :: rope?

[英]C++: mixture between vector and list: something like std::rope?

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. 当存储一堆项目并且我不需要随机访问容器时,我使用的是std::list ,这很好。 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 is bad because: std::vector很糟糕,因为:

  • It must reallocate if it doesn't fit anymore. 如果它不再适合,它必须重新分配。
  • It doesn't really work for huge amounts of data (because you just cannot always get very big chunks of continuous free memory). 它并不适用于大量数据(因为你不能总是获得非常大的连续空闲内存)。

std::list is bad because: std::list很糟糕,因为:

  • It makes an allocation on every single push_back. 它在每个push_back上进行分配。 That is slow and leads to a lot of memory fragmentation. 这很慢并导致大量内存碎片。

So, something in between is what I want. 所以,介于两者之间的是我想要的东西。

Basically, I want something like std::list< boost::array<T, 100> > or so. 基本上,我想要像std::list< boost::array<T, 100> >那样的东西。 Or maybe instead of 100 , let it be 4096/sizeof(T) . 或者也许代替100 ,让它为4096/sizeof(T) Maybe also std::list< std::vector<T> > and the first vectors can be small and then further ones can grow. 也许std::list< std::vector<T> >并且第一个向量可以很小,然后可以进一步增长。 Actually I want to have that hidden from the usage, so I can just do a mycontainer.push_back(x) . 实际上我想从使用中隐藏它,所以我可以做一个mycontainer.push_back(x)

std::rope is a bit similar to that but it is not available in the standard. std::rope有点类似,但标准中没有。

Is there something like this in Boost or so? 在Boost中有这样的东西吗?

Have you considered using std::deque ? 你考虑过使用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 . 如果你只是在序列的开头或结尾插入元素,它可能会比std::vector提供更好的性能。

Yes, it's called std::vector. 是的,它叫做std :: vector。 It's O(1) time push_back which is almost always faster than std::list. 这是O(1)时间push_back,几乎总是比std :: list快。 (Yeah, and it's also memory efficient) (是的,它也有记忆效率)

The most important feature of std::list is constant time deletion/insertion from the middle. std :: list最重要的特性是从中间删除/插入常量时间。 If you don't need it choose std::vector. 如果你不需要它,请选择std :: vector。

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

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