开发者

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. 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:

  • 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:

  • 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.

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 vec开发者_开发问答tors 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 is a bit similar to that but it is not available in the standard.

Is there something like this in Boost or so?


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.


Yes, it's called std::vector. It's O(1) time push_back which is almost always faster than std::list. (Yeah, and it's also memory efficient)

The most important feature of std::list is constant time deletion/insertion from the middle. If you don't need it choose std::vector.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜