Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which STL C++ container to use for a fixed size list?

Tags:

c++

stl

I am having a consuming application which needs to store a maximum of 100 objects in a list to feed to a callback for processing, since it will be redundant to keep old data if the consumer does not catch up. As new data is arrived, it can simply overwrite the oldest element.

I was thinking of using circular buffer container and guessed that it would be deque , but found that it does not use circular list, as well as does not have option to set fixed maximum size.

There is a max_size method in dequeue, but documentation says "This is the maximum potential size the container can reach due to system or library implementation limitations."

Is there some other container I can use?

PS : I am using Visual C++ 2010 express

like image 945
paseena Avatar asked Apr 05 '11 23:04

paseena


People also ask

What are the various types of STL containers?

Types of STL Container in C++ In C++, there are generally 3 kinds of STL containers: Sequential Containers. Associative Containers. Unordered Associative Containers.

Which of the following STL containers keeps the elements in sorted order?

Associative containers implement sorted data structures that can be quickly searched (O(log n) complexity). Map: Collection of key-value pairs, sorted by keys, keys are unique (class template).


2 Answers

There is no standard library container that does what you want directly. However, you should take a look at Boost's Circular Buffer Container. If you can't use Boost, you can at least view its source and redo it.

like image 89
GManNickG Avatar answered Sep 23 '22 00:09

GManNickG


There's boost::circular_buffer. Then there's

std::vector<T> vec(size);
vec[i % size] = newelem;
like image 30
wilhelmtell Avatar answered Sep 25 '22 00:09

wilhelmtell