I notice the thread of similar question: Limit size of Queue<T> in .NET?
That's exactly what I want to do, but I am not using .net but GNU C++. I have no reference to the base class in GNU C++, so java like super.***()
or .net like base.***()
will not work. I have been trying to inherit from queue class but it turns out in vain.
What I want to do: specify the size of the queue, and automatically dequeue when the queue is full. To be specific: if the maximum size of my queue is 2, when I push the 3rd item, the 1st item will be automatically popped out before pushing the new item.
How to implement such a queue?
Thanks.
Assuming that by
Queue<T>
you meanstd::queue<T>
: A queue is just an adapter for some underlying container that's passed at compile-time. You could use a container that already does what you want. The best fit seems to be a circular buffer, if you can find one that supports the operations necessary forstd::queue
(I think that'spush_back()
,pop_front()
, andsize()
, but I haven't checked).I know you said "automatic", but, to keep things simple: Encapsulate just the
Enqueue()
ing in a local function (no, not clean OO, but it works):It sounds like boost::circuclar_buffer does what you're looking for:
Make a new class that encapsulates the queue and enforce a size limit in the new class.