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;

Did you read the link I gave you on your other post? (Please do not have multiple theads ... on the same subject.)

Click Here

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

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

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.

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.

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());

                }

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 << ' ';
}

You could always write your own class, or derive a class to implement a print function...

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;

}

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;

}
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.