Pushing a 2d array onto a C++ STL stack?
int test[5][5];
stack<int**> mystack;
mystack.push(test);
I get the error:
no matching function for call to 'std::stack > >::push(int [5][5])' /usr/include/c++/4.4/bits/stl_stack.h:182: note: candidates are: void std::stack<_Tp, _Sequence>::push(const typename _Sequence::value_type&) [with _Tp = int**, _Sequence = std::deque >]
I've never really used stacks before so I would appreciate any help. If I declare test as a one-dimensional array and stack as int*, it works fine.
Edit: I'm trying to implement backtracing for a sudokusolver. I have the sudoku grid as a 9x9 array of set objects (objects that hold the solution or possible solutions). I have to push the current state of the puzzle onto the stack, and then from there try guess and check. If a guess creates a contradiction (ie violates the rules of a sudoku), then I would pop from the stack to recover the puzzle before the invalid guess.
In your example, test
is not of type int**
.
If you want a two-dimensional array, I would recommend using std::vector
. This would certainly save your confusion with arrays and pointers...
typedef std::vector<std::vector<int> > two_d_vector;
two_d_vector test;
stack<two_d_vector> mystack;
mystack.push(test);
An int **
is not the same as a 2D array. A pointer to int test[5][5]
would be int (*)[5]
, so you need a stack<int (*)[5]>
. There's a good explanation of this here: Arrays and pointers in C.
上一篇: 通过引用数组来传递堆栈分配的参数
下一篇: 将2d数组推入C ++ STL堆栈?