Reputation: 492
i know how to create a stack of vectors or int ,etc. But i dont know how to create a stack of a multidimentional arrays t[n][n] . Whats the way to implement it? this is my actual implementation which its not working.
char map[20][20];
stack<map> soluciones;
Edit:
I think due my english most of you didnt undestand my question. Imagine i got some kind of a Game map. I am saving each multidimentional array on the stack. Thats my objective saving the map on a stack
Edit 2: im using Visual Studio 2010 Windows form application
Upvotes: 1
Views: 141
Reputation: 21
Ddefine a wrapper class for multidimentional arrays like this:
template <class T, int X, int Y>
class Wrapper
{
public:
T array[X][Y];
}
Then use stack<Wrapper<char, 20, 20> >
Upvotes: 2
Reputation: 11398
In your code example, you use map
(the name of your variable) in place of where a type name must stand (in stack<map>
template instantiation). However, it won't work, not even if you use the proper type name (in this case you'd have to use typedef, e.g. typedef char TwoDimCharArray[20][20]
and then try std::stack<TwoDimCharArray>
:
There is still the problem that arrays don't have a default constructor (which std::stack
expects); therefore, std::stack
cannot be made to directly contain arrays; you'd have to wrap the array inside a class or struct (which can have a default constructor), e.g.:
class TwoDimCharArray
{
public:
// omit if there's nothing to initialize in map, then the
// compiler-generated constructor will be used
TwoDimCharArray()
{
// ... initalize map values
}
// ... provide access methods to map
private:
char map[20][20];
};
std::stack<TwoDimCharArray> soluciones;
Or use Boost.Array
or C++11 std::array
stuff! If these are available, they are definitely the better and easier choice!
Upvotes: 3
Reputation: 4515
I guess you should define a proper class for the game map. Then the stack of game maps is not a problem.
class GameMap {
public:
...
private:
char map_[1000][1000];
};
Then it wont matter for the stack how you allocate and manage the map data. E.g.
typedef std::stack<GameMap> GameMapStack;
Upvotes: 3
Reputation: 101
First create structure and then define an empty multidimensional char array. then close structure and after thar write push and pop operations.
Upvotes: 1