简体   繁体   English

将2D数组推入C ++ STL堆栈?

[英]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 >] 没有匹配的函数来调用'std :: stack>> :: push(int [5] [5])'/usr/include/c++/4.4/bits/stl_stack.h:182:注意:候选对象是:void std :: stack <_Tp,_Sequence> :: push(常量类型名_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. 如果我将test声明为一维数组,并将stack声明为int *,则它可以正常工作。

Edit: I'm trying to implement backtracing for a sudokusolver. 编辑:我正在尝试实现sudokusolver的回溯。 I have the sudoku grid as a 9x9 array of set objects (objects that hold the solution or possible solutions). 我将数独网格设置为9x9的一组设置对象(保存解决方案或可能的解决方案的对象)的数组。 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** . 在您的示例中, test的类型不是 int**

If you want a two-dimensional array, I would recommend using std::vector . 如果您想要一个二维数组,我建议使用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. int **与2D数组不同。 A pointer to int test[5][5] would be int (*)[5] , so you need a stack<int (*)[5]> . 指向int test[5][5]的指针将是int (*)[5] ,因此您需要stack<int (*)[5]> There's a good explanation of this here: Arrays and pointers in C . 这里有一个很好的解释: C中的数组和指针

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM