Hello,

Is there any way of checking all elements in a queue without calling pop().

while(!q.empty()){
cout << q.front() <<  endl;
q.pop()
}

this is essentially what I have done.
There is a bug in my program and I'm trying to check what is wrong. I don't want to remove elements. just want to check the content of the queue.

Appreciate any help you could lend me.

drjay

Recommended Answers

All 7 Replies

Depends on the c++ class. If the queue object is protected or private, then I'm afraid not.

std::queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);
    int k;
    for (int i = 0, n = q.size(); i < n; ++i) {
        std::cout << (k = q.front()) << '\n';
        q.pop(); q.push(k);
    }

That's why I don't like STL container adaptors ;)...

1

#include <iostream.h>
2 #include "Queue.h"
3
4 // Simple test program for queues
5
6 main( )
7 {
8 Queue<int> Q;
9
10 for( int i = 0; i < 5; i++ )
11 Q.Enqueue( i );
12
13 cout << "Contents:";
14 do
15 {
16 cout << ' ' << Q.Front( );
17 Q.Dequeue( );
18 } while( !Q.IsEmpty( ) );
19 cout << '\n';
20
21 return 0;
22 }
Sample queue program; output is
Contents:0 1 2 3 4

> Is there any way of checking all elements in a queue without calling pop().

Yes, if you have used a std::queue<T> with the default std::deque<> for the second template parameter.

template< typename T > void foobar( const std::queue<T>& queue )
{
    typename std::deque<T>::const_pointer begin = &queue.front() ;
    typename std::deque<T>::const_pointer end = begin + queue.size() ;
    // [begin,end) iterates over elements of the queue. for example,
    std::copy( begin, end, std::ostream_iterator<T>( std::cout, "\n" ) ) ;
}

> Is there any way of checking all elements in a queue without calling pop().

Yes, if you have used a std::queue<T> with the default std::deque<> for the second template parameter.

template< typename T > void foobar( const std::queue<T>& queue )
{
    typename std::deque<T>::const_pointer begin = &queue.front() ;
    typename std::deque<T>::const_pointer end = begin + queue.size() ;
    // [begin,end) iterates over elements of the queue. for example,
    std::copy( begin, end, std::ostream_iterator<T>( std::cout, "\n" ) ) ;
}

Is that a good idea?

@OP: Do you think you need queue for your problem? Will a list do? How about a vector?
Think about the data structure you need.

I think this is basically what magacin's code is doing, but you could if you HAVE to use a queue for whatever reason make a tempary queue with the same size the the one you need to read, save the poped values into it as you read then just loop back poping them back from the temp queue to the original one?

IF you dont need a queue then i agree with firstPerson and think you should change your data structure to a list if it changes allot in the middle of a vector if you want random access and rarely delete or insert items into the middle.

> Is that a good idea?

Yes, if the situation is as the OP posted:

>> There is a bug in my program and I'm trying to check what is wrong.
>> I don't want to remove elements. just want to check the content of the queue.

That is, what is really needed by the design is a queue, but for debugging purposes we need to spy into it's contents. Either when it is convenient for the debugging scaffold to use standard algorithms like std::find_if and getting iterators would be handy. Or the error occurs only when the queue is very big, and using a secondary queue every time we want to examine it is somewhat impractical on the very big queue.

It would be wrong if the const was missing in template< typename T > void foobar( const std::queue<T>& queue ) ; . It would also be wrong if the design called for a queue, and we use a list instead just because we want to debug our code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.