I'm trying to put a 2 dimensional array in a queue... How could I do that?

#include<iostream>
#include<queue>

using namespace std;

struct state
{
    int puzzle[2][2];
};

int main()
{
    state p;        
    p.puzzle[0][0]=1;
    p.puzzle[0][1]=2;
    p.puzzle[0][2]=3;
    p.puzzle[1][0]=8;
    p.puzzle[1][1]=0;
    p.puzzle[1][2]=4;
    p.puzzle[2][0]=7;
    p.puzzle[2][1]=6;
    p.puzzle[2][2]=5;

    queue<int> Queue;
    Queue[front] = p;


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

Edited 3 Years Ago by Dani: Formatting fixed

As you have declared Queue as follows:

queue<int> Queue;

You cannot simply assign p to it as you are attempting. Looking at your code I see no reason why you don't just assign the values of p directly to the queue (thus removing the need for your struct).
e.g.

Queue[0] = 1;
Queue[1] = 2;
Queue[2] = 3;
Queue[3] = 8;

etc.

Hi sam,

When assigning the value to queue use push in loop:

#include <iostream>
#include <queue>

using namespace std;

struct state
{
int puzzle[3][3];  //Changed the size to 3 from 2
};

int main(int argc, char *argv[])
{
state p;
p.puzzle[0][0]=1;
p.puzzle[0][1]=2;
p.puzzle[0][2]=3;
p.puzzle[1][0]=8;
p.puzzle[1][1]=0;
p.puzzle[1][2]=4;
p.puzzle[2][0]=7;
p.puzzle[2][1]=6;
p.puzzle[2][2]=5;

queue<int> Queue;
for(int i = 0; i < 3; i++)  //Needs loop for assigning the values
{
	for(int j = 0; j < 3; j++)
	{
            Queue.push(p.puzzle[i][j]); // Using push and cannot directly use p
	}
}


while(!Queue.empty())
{
cout<<" "<<Queue.front()<<endl;
Queue.pop();
}
getchar();
return 0;
}

Edited 6 Years Ago by mayyubiradar: spelling mistake :D

This article has been dead for over six months. Start a new discussion instead.