Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limited size queue

Tags:

c++

std

queue

boost

I need a queue on n items where insertion of (n+1)th item removes the 0th item and insertion can only be made on the "back".
Is there any such structure already there in boost or standard library?

like image 668
Dipro Sen Avatar asked Jun 15 '12 14:06

Dipro Sen


People also ask

Can we define size of queue?

The queue. size() function in Java is used to get the current size of a queue. queue. size() returns the number of elements in the queue.

What is fixed size queue?

Fixed Size/Capacity Queue It is a queue, and the size of the queue is fixed, it means that the queue cannot hold more than specified limit of number of data.

Does a queue have a limited number of items?

A queue is a container that enables you to hold an unlimited number of items. Queue items can store multiple types of data, such as invoice information or customer details.

What is the queue capacity?

The capacity of a Queue is the number of elements the Queue can hold. As elements are added to a Queue, the capacity is automatically increased as required through reallocation.


1 Answers

You can use a boost::circular_buffer wrapped by a std::queue, something like this:

#include <queue>
#include <boost/circular_buffer.hpp>

typedef std::queue<my_type, boost::circular_buffer<my_type>> my_queue;
const int n = 3;
...
my_queue q(boost::circular_buffer<my_type>(n));
q.push(1);
q.push(2);
q.push(3);
q.push(4); // queue now contains 2,3,4
like image 80
Steve M Avatar answered Oct 23 '22 18:10

Steve M