Queue
Home Up

 

Same level pages:
Vector
Deque
List
Set
Multiset
Map
Multimap
String
Bitset
Stack
Queue
Priority Queue

Parent level pages:
Containers
Classes
Iterators
Algorithms

The template class queue<T, Seq> is an adaptor that stores data in a  sequence Seq (a deque<T> by default> to provide a restricted subset of container functionality. It is assignable and default constructible. T must be assignable. Seq must be a back insertion sequence as well as a front insertion sequence whose value type is T. It may also be required that T be equality comparable and/or less-than comparable depending on the implementation of the Seq being used.

The STL queue template is found in <queue>. A simplified declaration of the queue template would look something like:

template<class T>
class queue
{
public:
    queue();
    bool empty() const;
    unsigned size() const;
    T& top();
    const T& top() const;
    void push(const T& x);
    void pop();
protected:
    deque<T> c;
};

Note that top() is used instead of the front() to access the next element to be removed from the queue. Similarly, push(T) and pop() are used instead of enqueue(T) and dequeue().

In reality, the queue class is an adapter class that can be implemented by containers other than the deque container. In particular the list<> class is appropriate. The container, in turn, may use a custom allocator other than ::new(). The container must supply the following members where T0 is a suitable integral type such as unsigned or unsigned long:

    typedef T value_type;
    typedef T0 size_type;
    Cont(const allocator_type& al);
    bool empty() const;
    size_type size() const;
    allocator_type get_allocator() const;
    value_type& front();
    const value_type& front() const;
    value_type& back();
    const value_type& back() const;
    void push_back(const value_type& x);
    void pop_front();

The full declaration of a queue looks something like:

template<class T, class Cont = deque<T> >
class queue
{
public:
    typedef Cont::allocator_type allocator_type;
    typedef Cont::value_type value_type;
    typedef Cont::size_type size_type;
    explicit queue(const allocator_type& al = allocator_type()) const;
    bool empty() const;
    size_type size() const;
    allocator_type get_allocator() const;
    value_type& top();
    const value_type& top() const;
    void push(const value_type& x);
    void pop();
protected:
    Cont c;
};
ŠPaul Buis & Ball State University Author: Paul Buis (peb@bsu.edu) Last Modified:10/26/00 03:31 PM

NOTICE: The information presented on this page represents the personal views, ideas, and opinions of the author. This is not an official Ball State University web page. Links contained at this web site to other organizations, are presented as a service and neither constitute nor imply university endorsement or warranty.