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

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

Do this

queue<state> stateQueue;
state s; 
//initialize s
stateQueue.push( s );
//...blah blah a blah
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.