954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to put a 2-dimensional array in a queue

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

#include
#include

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 Queue;
Queue[front] = p;


while(!Queue.empty())
{
cout<<" "<

1989sam
Newbie Poster
2 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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.

caged_fire
Junior Poster
128 posts since Mar 2009
Reputation Points: 16
Solved Threads: 9
 

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;
}
mayyubiradar
Newbie Poster
19 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

Do this

queue<state> stateQueue;
state s; 
//initialize s
stateQueue.push( s );
//...blah blah a blah
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: