queue within a queue

how can i store a queue of type integers within another queue of type queue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <queue>

using namespace std;

int main()
{
queue<queue> myqueue;
queue<int> myqueue2;
myqueue2.push(27);
myqueue.push(myqueue2);

return 0;
}


didn't work but not sure why. the idea is to replicate the effects of an 2d array but i'm not allowed to use 2d arrays.
It's queue<queue<int> >...
Another thing that's nice to do is typedef:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <queue>
typedef std::queue<int> InnerQueue;
typedef std::queue<InnerQueue> OuterQueue;

int main()
{
  OuterQueue myqueue;
  InnterQueue temp;

  temp.push(27);
  myqueue.push(temp);

  return 0;
}
Last edited on
thanks athar it works but now how do i show/modify them? the central queue i mean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <queue>

using namespace std;

int main()
{
queue<queue<int> > myqueue;
queue<int>myqueue2;
myqueue2.push(123);
myqueue2.push(345);
myqueue.push(myqueue2);

cout<<myqueue.front(myqueue2.front())<<endl;//from here doesn't work
//myqueue2.pop();
//cout<<myqueue.front()<<endl;
return 0;
}
myqueue.front() returns the first myqueue2 element. So you would do it this way:

cout << myqueue.front().front() << endl;

Remember, it takes no arguments are returns whatever is held by the queue:
http://www.cplusplus.com/reference/stl/queue/front/
Last edited on
ok thanks but now how would i pop the internal queue so the second cout displays 456?
myqueue.front().pop();
Topic archived. No new replies allowed.