gpt4 book ai didi

c++ - 将二维数组插入 C++ STL 堆栈?

转载 作者:行者123 更新时间:2023-11-28 01:08:41 27 4
gpt4 key购买 nike

int test[5][5];
stack<int**> mystack;
mystack.push(test);

我得到错误:

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 >]

我以前从未真正使用过堆栈,所以我将不胜感激任何帮助。如果我将 test 声明为一维数组并将堆栈声明为 int*,它可以正常工作。

编辑:我正在尝试为数独求解器实现回溯。我将数独网格作为一组对象的 9x9 数组(包含解决方案或可能解决方案的对象)。我必须将拼图的当前状态压入堆栈,然后从那里尝试猜测和检查。如果猜测产生矛盾(即违反了数独的规则),那么我将从堆栈中弹出以恢复无效猜测之前的谜题。

最佳答案

int **与二维数组不同。指向 int test[5][5] 的指针将是 int (*)[5] , 所以你需要一个 stack<int (*)[5]> .这里有一个很好的解释:Arrays and pointers in C .

关于c++ - 将二维数组插入 C++ STL 堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4771926/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com