0

I am trying to print my queue which has values in it.

Right now i have this.

            for(int i = 1; i<ReadyQueue.size(); i++)
            {

            }

But i am stuck now because i have no idea how to print a queue without popping it off the front. So now i am stuck.

How do I print a queue without emptying it? This is my queue

queue<int> ReadyQueue;

Edited by mystycs

3
Contributors
10
Replies
21
Views
3 Years
Discussion Span
Last Post by David W
0

So would i pop off the front and push on the back to read it?

0

You know .. the joy of having access to your own PC and compiler ... is that you can fairly quickly 'try things out for yourself ... and see.'

Enjoy :)

P.S. Your idea seems logical ... yes? ... But ... some 'queues' may offer random access?

Click Here

Edited by David W: added comments

0

But i dont see that there is a pop_front and push_back for queue. I only see front to see the first value, and pop to take off the front.

So lost on how to do that part.

0

Do you know what an iterator is? (Or a constant iterator? ... to traverse each address in a deque ... so ... you can access the values at each address ... an iterator is just an elegant pointer ... ++it from begin ... check that NOT == end ... and you have access to each 'it' as you traverse from dq.begin() ... to != dq.end()

deque< int >::const_iterator it;

for( it = dq.begin(); it != dq.end(); ++ it ) 
    cout << *it << " ";

If ... rather that a deque of int ... you had a deque of some struct ...

then ... first define overloaded operator << for that struct to print it out as per above.

Edited by David W: added code

0

This is what i have but it doesnt work.

                for(int i = 1; i<ReadyQueue.size(); i++)
                {
                    queue<int> tempQueue;

                    cout << ReadyQueue.front() << endl;
                    tempQueue.push(ReadyQueue.front());
                    ReadyQueue.pop();

                    ReadyQueue.push(tempQueue.back());

                }
0

Your code makes NO sense ... THINK what you are doing!

Try what I (edited and) posted above ... for deque.

// dequeExample.cpp //

#include <iostream>
#include <deque>

int main()
{
    std::deque < int > dq;
    dq.push_back( 2 );
    dq.push_back( 1 );
    dq.push_back( 3 );
    dq.push_back( 5 );

    std::deque < int >::const_iterator it;
    for( it = dq.begin(); it != dq.end(); ++it )
        std::cout << *it << ' ';
}

Edited by David W: added comments

0

Not really needed ...

Just use the C++ overloaded operator << to define how you wish to print a struct (or class)... when using iterators

Can use a friend function, if you wish.

See this edit ... (of your above attempt to print a queue).

// dequeExample.cpp //

#include <iostream>
#include <deque>

#include <queue>

int main()
{
    std::deque < int > dq;
    dq.push_back( 2 );
    dq.push_back( 1 );
    dq.push_back( 3 );
    dq.push_back( 5 );

    std::queue < int > q;

    std::deque < int >::const_iterator it;
    for( it = dq.begin(); it != dq.end(); ++it )
    {
        std::cout << *it << ' ';
        q.push( *it ); // make copy
    }

    std::cout << std::endl;

    int size = q.size();
    while( size )
    {
        int tmp = q.front();
        std::cout << tmp << ' ';

        q.pop();
        q.push( tmp );

        --size;
    }

    std::cout << "q.size() = " << q.size() << std::endl;

}
0

See this (reworked) example ... (now with a struct)

that has a friend ...

an overloaded operator <<

// dequeExample.cpp //

#include <iostream>
#include <deque>

#include <queue>

struct Contact
{
    std::string name;
    int id;

    Contact( std::string n="", int i=0 ) : name(n), id(i) {}


    friend std::ostream& operator << ( std::ostream& os, const Contact& c )
    {
        return os << c.name << '[' << c.id << ']';
    }
} ;

int main()
{
    std::deque < Contact > dq;
    dq.push_back( Contact( "Fran", 2 ) );
    dq.push_back( Contact( "Joe", 1 ) );
    dq.push_back( Contact( "Moe", 3 ) );
    dq.push_back( Contact( "Jan", 5 ) );

    std::queue < int > q;

    std::deque < Contact >::const_iterator it;
    for( it = dq.begin(); it != dq.end(); ++it )
    {
        std::cout << *it << ' ';
        q.push( it->id ); // make copy
    }

    std::cout << std::endl;

    int size = q.size();
    while( size )
    {
        int tmp = q.front();
        std::cout << tmp << ' ';

        q.pop();
        q.push( tmp );

        --size;
    }

    std::cout << "q.size() = " << q.size() << std::endl;

}

Edited by David W: fixed comment

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.