.\" ident @(#)queue.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH queue 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2queue\fP \ - A container adaptor that behaves like a queue (first in, first out). .SH SYNOPSIS .br #include .br template > .RE .RS 1 class queue ; .SH DESCRIPTION The queue container adaptor lets a container act 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 \f2front()\fP, \f2back()\fP, \f2push_back()\fP, and \f2pop_front()\fP operations. In particular, deque and list can be used. .SH INTERFACE .RE .RS 0 template > .br class queue { .br .br public: .br .br // typedefs .br .RE .RS 2 typedef typename Container::value_type value_type; .br typedef typename Container::size_type size_type; .br typedef Container container_type; .RE .RS 0 .br // Construct/Copy/Destroy .RE .RS 2 explicit queue (const Container& = Container()); .RE .RS 0 .br // Accessors .br .RE .RS 2 bool empty () const; .br size_type size () const; .br value_type& front (); .br const value_type& front () const; .br value_type& back (); .br const value_type& back () const; .br void push (const value_type&); .br void pop (); .RE .RS 0 }; .br .br // Non-member Operators .br .br template .br bool operator== (const queue&, .RE .RS 17 const queue&); .RE .RS 0 .br template .br bool operator!= (const queue&, .RE .RS 17 const queue&); .RE .RS 0 .br template .br bool operator< (const queue&, .RE .RS 16 const queue&); .RE .RS 0 .br template .br bool operator> (const queue&, .RE .RS 16 const queue&); .RE .RS 0 .br template .br bool operator<= (const queue&, .RE .RS 16 const queue&); .RE .RS 0 .br template .br bool operator>= (const queue&, .RE .RS 16 const queue&); .SH CONSTRUCTORS .RE .RS 0 explicit queue (const Container& = Container()); .RE .RS 3 Creates a queue of zero elements. The queue uses the allocator \f2Allocator()\fP for all storage management. .RE .SH MEMBER FUNCTIONS .br value_type& .br back (); .RE .RS 3 Returns a reference to the item at the back of the queue (the last item pushed into the queue). .RE .br const value_type& .br back() const; .RE .RS 3 Returns a constant reference to the item at the back of the queue as a \f2const_value_type\fP. .RE .br bool .br empty () const; .RE .RS 3 Returns \f2true\fP if the queue is empty, otherwise \f2false\fP. .RE .br value_type& .br front (); .RE .RS 3 Returns a reference to the item at the front of the queue. This is the first item pushed onto the queue unless \f2pop()\fP has been called since then. .RE .br const value_type& .br front () const; .RE .RS 3 Returns a constant reference to the item at the front of the queue as a \f2const_value_type\fP. .RE .br void .br pop (); .RE .RS 3 Removes the item at the front of the queue. .RE .br void .br push (const value_type& x); .RE .RS 3 Pushes \f2x\fP onto the back of the queue. .RE .br size_type .br size () const; .RE .RS 3 Returns the number of elements on the queue. .RE .SH NON-MEMBER OPERATORS .br template .RE .RS 1 bool operator== (const queue& x, .RE .RS 18 const queue& y); .RE .RS 3 Returns \f2true\fP if \f2x\fP is the same as \f2y\fP. .RE .RE .RS 0 template .RE .RS 1 bool operator!= (const queue& x, .RE .RS 18 const queue& y); .RE .RS 3 Returns \f2!(x==y)\fP. .RE .RE .RS 0 template .RE .RS 1 bool operator< (const queue& x, .RE .RS 17 const queue& y); .RE .RS 3 Returns \f2true\fP if the queue defined by the elements contained in \f2x\fP is lexicographically less than the queue defined by the elements contained in \f2y\fP. .RE .RE .RS 0 template .RE .RS 1 bool operator> (const queue& x, .RE .RS 17 const queue& y); .RE .RS 3 Returns \f2y < x\fP. .RE .RE .RS 0 template .RE .RS 1 bool operator< (const queue& x, .RE .RS 17 const queue& y); .RE .RS 3 Returns \f2!(y < x)\fP. .RE .RE .RS 0 template .RE .RS 1 bool operator< (const queue& x, .RE .RS 17 const queue& y); .RE .RS 3 Returns \f2!(x < y)\fP. .RE .SH EXAMPLE .RE .RS 0 // .br // queue.cpp .br // .RE .RS 1 #include .br #include .br #include .br #include .br #include .RE .RS 0 using namespace std; .br .br int main(void) .RE .RS 1 { .RE .RS 3 // Make a queue using a list container .br queue> q; .RE .RS 0 .RE .RS 3 // Push a couple of values on then pop them off .RE .RS 2 q.push(1); .br q.push(2); .br cout << q.front() << endl; .br q.pop(); .br cout << q.front() << endl; .br q.pop(); .RE .RS 0 .RE .RS 3 // Make a queue of strings using a deque container .br queue> qs; .RE .RS 0 .RE .RS 3 // Push on a few strings then pop them back off .RE .RS 2 int i; .br for (i = 0; i < 10; i++) .RE .RS 3 { .RE .RS 4 qs.push(string(i+1,'a')); .br cout << qs.front() << endl; .RE .RS 3 } .RE .RS 2 for (i = 0; i < 10; i++) .RE .RS 3 { .RE .RS 4 cout << qs.front() << endl; .br qs.pop(); .RE .RS 3 } .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br 1 .br 2 .br a .br a .br a .br a .br a .br a .br a .br a .br a .br a .br a .br aa .br aaa .br aaaa .br aaaaa .br aaaaaa .br aaaaaaa .br aaaaaaaa .br aaaaaaaaa .br aaaaaaaaaa .SH WARNINGS If your compiler does not support default template parameters, you must always include a \f2Container\fP template parameter. For example you would not be able to write: \f2queue var;\fP rather, you would have to write, \f2queue > var;\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP. .SH SEE ALSO allocator, Containers, priority_queue