queue |
A queue
is an adapter that can use any container supporting push_back()
and pop_front() as a
first-in, first-out data structure. Following is an overview of queue
.
Because adapters do not support
iteration, a queue has no associated iterator.
A queue
is implemented as a class comprised of inline functions converting push()
and pop()
messages into push_back()
and pop_front() messages. The only STL containers
that support these functions are list and deque
. Of these two containers, deque yields a better
performance.
A queue
is useful for implementing strictly first-in, first-out data structures when
direct indexed access to the items should not be performed.
A queue
does not introduce any new functions, but an additional parameter is required if
you are using an older compiler and its implementation of pop()
operates differently from that of stack .
The following example illustrates
the first-in, first-out nature of a queue
.
#include <iostream>
#include <list>
#include <queue>
void
main()
{
queue< int, list< int > > q;
q.push( 42 );
q.push( 101 );
q.push( 69 );
while ( !q.empty() )
{
cout << q.front() << "\n";
q.pop();
}
}
42
101
69
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.