C++ Queue?

Whats the best way to display contents of a STL Queue ?? But without removing any of the Queue's elements?

Because I have seen:

while(!Queue.empty())

{

cout << Queue.front() << " " ;

Queue.pop()

}

But then I cant display the queue again or re add after displaying.

Any help is appreciated.

Update:

Kenshin: in your example where inside the for loop what does where it says "type item " what type are you refering to?

Comments

  • I don't think there are any methods or iterators that can be used for std::queue.

    Try using std::list.

    In essence you are trying to use a queue for a purpose that it is not designed to be used for.

    "Whether or not im using it for what it was made for doesnt really matter in this case because my assignment requires that I create a program that can add, remove and display the elements in a Queue."

    Then it can not be done since you gave the condition

    "But without removing any of the Queue's elements?"

    The ONLY way to read elements in a std::queue is to remove them and then push them back on to the queue when you are done.

    i.e.

    int length = queue.size();

    for(int i = 0; i < length; i++) {

    type item = queue.pop();

    print item;

    queue.push(item);

    }

Sign In or Register to comment.