displaying the contents of a queue

J

JC

How would I display the content of a queue.

Example, Queue A contains (23, 78, 56, 33,4,5)

Thanks
JC
 
R

Russell Hanneken

JC said:
How would I display the content of a queue.

Example, Queue A contains (23, 78, 56, 33,4,5)

How's this?

#include <iostream>
#include <ostream>
#include <queue>

int main()
{
typedef std::queue<int> IntQueue;
IntQueue A;

// Fill the queue
A.push(23);
A.push(78);
A.push(56);
A.push(33);
A.push(4);
A.push(5);

// Display the queue's contents
while (!A.empty())
{
std::cout << A.front() << '\n';
A.pop();
}
}
 
R

Russell Hanneken

osmium said:
I would expect a well-designed queue class to have an iterator.

std::queue doesn't. I guess the thinking is that the point of having a
queue is to enforce the "first in, first out" rule for element access.
If you want a container that's similar to a queue, but lets you
iterate, use a deque.

(On the other hand, std::queue does let you look at the last element.
Go figure.)

Regards,

Russell Hanneken
(e-mail address removed)
Remove the 'g' from my address to send me mail.
 
O

osmium

JC said:
How would I display the content of a queue.

Example, Queue A contains (23, 78, 56, 33,4,5)

I would expect a well-designed queue class to have an iterator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top