13

I am trying to print a queue below. I have tried the idea of creating a temp queue and writing into it then writing it back.

But it's not working.

Or what am I missing here?

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

    cout << myQueue.front() << endl;
    MytempQueue.push(myQueue.front());
    myQueue.pop();

    myQueue.push(myTempQueue.back());
}

My queue is queue<int> myQueue;

Essentially, I want to print this queue without emptying it. But I am stuck here.

Erik
  • 2,500
  • 2
  • 13
  • 26
tyrone 251
  • 355
  • 1
  • 4
  • 14
  • 5
    Just a hint: if printing is a frequent, important operation in your code, then perhaps a queue is just the wrong choice for the data type, and you may be better of with a different kind of container altogether. – Christian Hackl Mar 09 '14 at 09:11
  • @ChristianHackl What would be that container? – simplename Oct 26 '21 at 00:59

2 Answers2

16

There is no efficient way to do this*. But you can do the following:

  1. Make a copy of the queue.
  2. Iterate over the copy, printing the front, then popping it.

For example:

#include <queue>
#include <iostream>
    
void print_queue(std::queue<int> q)
{
  while (!q.empty())
  {
    std::cout << q.front() << " ";
    q.pop();
  }
  std::cout << std::endl;
}

int main()
{
  std::queue<int> q;
  for (auto i : {1,2,3,7,4,9,7,2,4}) q.push(i);
  print_queue(q);
}

* There's a hack using inheritance. std::queue has a protected member C which is the underlying container holding the data. You could inherit from std::queue and add methods to do the printing using C. But you have to be fully aware of the implications of inheriting from a type that is not necessarily designed for that.

Konrad
  • 355
  • 6
  • 18
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • How do i make a copy of the queue? Thats what im trying to do. Pseudo code help would be awesome! – tyrone 251 Mar 09 '14 at 09:08
  • 2
    `queue copy = myQueue;` – jrok Mar 09 '14 at 09:08
  • @user3398034 [`std::queue`](http://en.cppreference.com/w/cpp/container/queue) have both a [copy constructor](http://en.cppreference.com/w/cpp/container/queue/queue) and an [copy assignment operator](http://en.cppreference.com/w/cpp/container/queue/operator%3D). – Some programmer dude Mar 09 '14 at 09:10
  • @user3398034 I added an example – juanchopanza Mar 09 '14 at 09:12
  • If you have custom class type like queue then we can use q.front().yourMethod() to get or set values – Shriram Panchal Jul 24 '17 at 04:10
  • @juanchopanza *"There's a hack using inheritance. `std::queue` has a protected member C which is ..."* Could you please share that hack or redirect me to some resources where I can read more about them? Thank you so much in advance! – Milan Mar 18 '22 at 18:57
0

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

Loop should start with 0, or check condition should be <=

queue<int> myQueue;
myQueue.push(1);
myQueue.push(2);
myQueue.push(3);
myQueue.push(4);
myQueue.push(5);

for (size_t i = 0; i < myQueue.size(); i++)
{
    cout << myQueue.front() << endl;
    myQueue.push(myQueue.front());
    myQueue.pop();
}