Top of document
©Copyright 1999 Rogue Wave Software

queue


     Container Adaptor

Summary

A container adaptor that behaves like a queue (first in, first out).


Contents

Synopsis

#include <queue>
template <class T, class Container = deque<T>, 
          class Allocator = allocator> class queue ;

Description

The queue container adaptor lets a container function as a queue. In a queue, items are pushed into the back of the container and removed from the front. The first items pushed into the queue are the first items to be popped off of the queue (first in, first out, or "FIFO").

queue can adapt any container that supports the front(), back(), push_back() and pop_front() operations. In particular, deque, list, and vector can be used.

Interface

template <class T, class Container = deque<T>,
     class Allocator = allocator>
 class queue {
public:
// typedefs
   typedef typename Container::value_type value_type;
   typedef typename Container::size_type size_type;
   typedef Allocator allocator_type;
// Construct/Copy/Destroy
   explicit queue (const Allocator& = Allocator());
   allocator_type get_allocator () const;
// Accessors
   bool empty () const;
   size_type size () const;
   value_type& front ();
   const value_type& front () const;
   value_type& back ();
   const value_type& back () const;
   void push (const value_type&);
   void pop ();
};
// Non-member Operators
template <class T, class Container, class Allocator>
 bool operator== (const queue<T, Container, Allocator>&,
                  const queue<T, Container, Allocator>&);
template <class T, class Container, class Allocator>
 bool operator< (const queue<T, Container, Allocator>&,
                 const queue<T, Container, Allocator>&);

Constructors

explicit queue (const Allocator& alloc= Allocator());

Allocator

allocator_type get_allocator () const;

Member Functions

value_type& 
back ();
const value_type& 
back() const;
bool 
empty () const;
value_type& 
front ();
const value_type& 
front () const;
void 
pop ();
void 
push (const value_type& x);
size_type 
size () const;

Non-member Operators

template <class T, class Container, class Allocator>
  bool operator== (const queue<T, Container, Allocator>& x,
                   const queue<T, Container, Allocator>& y);
template <class T, class Container, class Allocator>
  bool operator< (const queue<T, Container, Allocator>& x,
                  const queue<T, Container, Allocator>& y);

Example

//
// queue.cpp
//
 #include <queue>
 #include <string>
 #include <deque>
 #include <list>
 #include <iostream.h>
 int main(void)
 {
   // Make a queue using a list container
   queue<int, list<int>, allocator> q;
   // Push a couple of values on then pop them off 
   q.push(1);
   q.push(2);
   cout << q.front() << endl;
   q.pop();
   cout << q.front() << endl;
   q.pop();
   // Make a queue of strings using a deque container
   queue<string,deque<string>, allocator> qs;
   // Push on a few strings then pop them back off
   int i;
   for (i = 0; i < 10; i++)
   {
     qs.push(string(i+1,'a'));
     cout << qs.front() << endl;
   }
   for (i = 0; i < 10; i++)
   {
     cout << qs.front() << endl;
     qs.pop();
   }
   return 0;
 }
Output :
1
2
a
a
a
a
a
a
a
a
a
a
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa

Warnings

If your compiler does not support default template parameters, you must always provide a Container template parameter and an Allocator template parameter. For example you would not be able to write:

queue<int> var;

rather, you would have to write,

queue<int, deque<int>, allocator> var;


See Also

allocator, Containers, priority_queue


Top of document