简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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