鱼C论坛

 找回密码
 立即注册
查看: 1048|回复: 6

[已解决]为什么用不了stl库里的栈 啊

[复制链接]
发表于 2023-7-7 15:05:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
用了说找不到这个头文件,但是这不是c++自带的吗



  1. #include "Stack.h"
  2. #include <iostream>
  3. #include <stdlib.h>                //exit函数要用到
  4. using namespace std;
  5. struct Box        //将每个迷宫格子看成是一个结构体
  6. {
  7.     int x, y, di;
  8.     //x和y分别代表迷宫格子的横纵坐标,di为当前的方向
  9. };
  10. struct Direction        //构建结构体
  11. {
  12.     int incX, incY;
  13. };


  14. typedef struct Node                //这是栈节点
  15. {
  16.     Box data;
  17.     struct Node* pNext;
  18. }Node, *PNODE;

  19. class Stack
  20. {
  21.     private:
  22.     PNODE pTop;
  23.     PNODE pBottom;

  24.     public:
  25.         Stack();
  26.         void initStack(Stack*);
  27.         void pushStack(Stack*, Box);
  28.         
  29.         //将栈中的栈底元素返回并移除,用递归来实现栈逆序遍历的
  30.         Box getElement(Stack&, Box&);
  31.         void reverse(Stack&, Box&);
  32.         
  33.         void traverse(Stack*);
  34.         void pop(Stack*, Box&);
  35.         bool isEmpty(Stack*);
  36. };



  37. Stack::Stack()
  38. {
  39.     //ctor
  40. }

  41. void Stack::initStack(Stack* pS)
  42. {
  43.     pS->pTop = new Node;
  44.     if(nullptr == pS->pTop){
  45.         cout << "动态内存分配失败!" << endl;
  46.         exit(-1);
  47.     }
  48.     else{
  49.         pS->pBottom = pS->pTop;
  50.         pS->pTop->pNext = nullptr;
  51.     }
  52.     return;
  53. }

  54. void Stack::pushStack(Stack* pS, Box temp) //temp是将一个结构体整体入栈
  55. {
  56.     PNODE pNew = new Node;
  57.     pNew->data = temp;
  58.     pNew->pNext = pS->pTop;
  59.     pS->pTop = pNew;
  60. }
  61. /** 这里开始是逆序遍历栈的关键代码,等会详细讲解 */
  62. Box Stack::getElement(Stack& s, Box& temp)
  63. {
  64.     s.pop(&s, temp);
  65.     Box res = temp;
  66.     if(s.isEmpty(&s)) return res;
  67.     else{
  68.         Box last = getElement(s, temp);
  69.         s.pushStack(&s, res);
  70.         return last;
  71.     }
  72. }

  73. void Stack::reverse(Stack& s, Box& temp)
  74. {
  75.     if(s.isEmpty(&s)) return;
  76.     Box i  = getElement(s, temp);
  77.     s.reverse(s, temp);
  78.     s.pushStack(&s, i);
  79. }
  80. /****************************************/
  81. void Stack::traverse(Stack* pS)
  82. {
  83.     PNODE p = pS->pTop;                //p是移动指针,在栈顶和栈底间上下移动的
  84.     while(p != pS->pBottom)
  85.     {
  86.         cout << "(" << p->data.x << ", " << p->data.y << ")" << endl;
  87.         p = p->pNext;
  88.     }
  89.     cout << endl;
  90.     return;
  91. }

  92. void Stack::pop(Stack* pS, Box& temp)        //这里的temp一定要 采用引用
  93. {
  94.     if(isEmpty(pS)) return;
  95.     else{
  96.         PNODE r = pS->pTop;
  97.         temp = r->data;
  98.         pS->pTop = r->pNext;
  99.         free(r);
  100.         r = NULL;
  101.         return;
  102.     }
  103. }

  104. bool Stack::isEmpty(Stack* pS)
  105. {
  106.     if(pS->pTop == pS->pBottom)
  107.         return true;
  108.     else
  109.         return false;
  110. }


  111. bool findPath(Stack& s, int M, int N)
  112. {
  113.     int **maze = new int*[M+2];                //动态构造二维数组
  114.     for(int i = 0; i < 10; i++){
  115.         maze[i] = new int[N+2];
  116.     }

  117.     for(int i = 0; i < M+2; i++){        //动态给定迷宫
  118.         for(int j = 0; j < N+2; j++){
  119.             if(i == 0 || i == M+1)
  120.                 maze[i][j] = 1;
  121.             else if(j == 0 || j == N+1)
  122.                 maze[i][j] = 1;
  123.             else
  124.                 cin >> maze[i][j];
  125.         }
  126.     }

  127.     Direction direct[4];                //方向数组(文章上面有图有说到)
  128.     direct[0].incX = 0; direct[0].incY = 1;
  129.     direct[1].incX = 1; direct[1].incY = 0;
  130.     direct[2].incX = 0; direct[2].incY = -1;
  131.     direct[3].incX = -1; direct[3].incY = 0;

  132.     Box temp;
  133.     int x, y, di;      //当前正在处理的迷宫格子
  134.     int line, col;    //迷宫格子预移方向后,下一格子的行坐标、列坐标
  135.     maze[1][1] = -1;
  136.     temp.x = 1;
  137.     temp.y = 1;
  138.     temp.di = -1;                //起始点直接设置为-1代表该格子已访问过了
  139.     s.pushStack(&s, temp);
  140.     while(!s.isEmpty(&s))
  141.     {
  142.         s.pop(&s, temp);        //这里对遇到走不通的格子进行回退时起到关键作用
  143.         x = temp.x; y = temp.y; di = temp.di+1;
  144.         while(di < 4){  //走不通时,四个方向都尝试一遍
  145.             line = x + direct[di].incX;
  146.             col = y + direct[di].incY;
  147.             if(maze[line][col] == 0){        //代表 预走 的格子可以走
  148.                 temp.x = x; temp.y = y; temp.di = di;
  149.                 s.pushStack(&s, temp);
  150.                 x = line; y = col; maze[line][col] = -1;        //标为-1是为了表明该格子已经走过,回溯时不再处理
  151.                 if(x == M && y == N){
  152.                         s.reverse(s, temp);
  153.                         return true;   //迷宫有路
  154.                     }
  155.                 else di = 0;
  156.             }
  157.             else di++;
  158.         }
  159.     }
  160.     return false;       //迷宫无路
  161. }

  162. int main()
  163. {
  164.     int M, N;
  165.     bool res;
  166.     cout << "请输入迷宫数组行数和列数:";
  167.     cin >> M >> N;
  168.     Stack s;
  169.     s.initStack(&s);
  170.     res = findPath(s, M, N);
  171.     cout << boolalpha << res << endl;        //将bool转换为true/false显示
  172.     cout << endl;
  173.     if(res)
  174.         s.traverse(&s);
  175.     else
  176.         cout << "你被困在迷宫中了,等着受死吧!" << endl;
  177.     return 0;
  178. }

复制代码
最佳答案
2023-7-7 15:07:00
从你的代码中,我看到你自己实现了一个栈,而没有使用C++标准模板库(STL)中的 std::stack 。如果你想使用STL中的栈,你需要包含 <stack> 头文件,并使用 std::stack 。下面是一个使用STL中的 std::stack 的简化版代码:
  1. #include <iostream>
  2. #include <stack>
  3. #include <vector>
  4. using namespace std;
  5. struct Box {
  6.     int x, y, di;
  7. };
  8. struct Direction {
  9.     int incX, incY;
  10. };
  11. bool findPath(stack<Box>& s, int M, int N, const vector<vector<int>>& maze) {
  12.     Direction direct[4] = {
  13.         {0, 1},
  14.         {1, 0},
  15.         {0, -1},
  16.         {-1, 0}
  17.     };
  18.     int x, y, di;
  19.     vector<vector<int>> visited = maze;
  20.     visited[1][1] = -1;
  21.     s.push({1, 1, -1});
  22.     while (!s.empty()) {
  23.         Box temp = s.top();
  24.         s.pop();
  25.         x = temp.x; y = temp.y; di = temp.di + 1;
  26.         while (di < 4) {
  27.             int line = x + direct[di].incX;
  28.             int col = y + direct[di].incY;
  29.             if (visited[line][col] == 0) {
  30.                 s.push({x, y, di});
  31.                 x = line; y = col; visited[line][col] = -1;
  32.                 if (x == M && y == N) {
  33.                     return true;
  34.                 } else {
  35.                     di = 0;
  36.                 }
  37.             } else {
  38.                 di++;
  39.             }
  40.         }
  41.     }
  42.     return false;
  43. }
  44. int main() {
  45.     int M, N;
  46.     cout << "请输入迷宫数组行数和列数:";
  47.     cin >> M >> N;
  48.     vector<vector<int>> maze(M + 2, vector<int>(N + 2, 1));
  49.     for (int i = 1; i <= M; ++i) {
  50.         for (int j = 1; j <= N; ++j) {
  51.             cin >> maze[i][j];
  52.         }
  53.     }
  54.     stack<Box> s;
  55.     bool res = findPath(s, M, N, maze);
  56.     cout << boolalpha << res << endl;
  57.     if (res) {
  58.         while (!s.empty()) {
  59.             Box temp = s.top();
  60.             s.pop();
  61.             cout << "(" << temp.x << ", " << temp.y << ")" << endl;
  62.         }
  63.     } else {
  64.         cout << "你被困在迷宫中了,等着受死吧!" << endl;
  65.     }
  66.     return 0;
  67. }
复制代码

请注意,我已经删除了你自定义的栈类,并用 std::stack 替换了它。同时,我还将原来的二维数组替换为 vector<vector<int>> ,这是一个更现代且易于使用的数据结构。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-7 15:07:00 | 显示全部楼层    本楼为最佳答案   
从你的代码中,我看到你自己实现了一个栈,而没有使用C++标准模板库(STL)中的 std::stack 。如果你想使用STL中的栈,你需要包含 <stack> 头文件,并使用 std::stack 。下面是一个使用STL中的 std::stack 的简化版代码:
  1. #include <iostream>
  2. #include <stack>
  3. #include <vector>
  4. using namespace std;
  5. struct Box {
  6.     int x, y, di;
  7. };
  8. struct Direction {
  9.     int incX, incY;
  10. };
  11. bool findPath(stack<Box>& s, int M, int N, const vector<vector<int>>& maze) {
  12.     Direction direct[4] = {
  13.         {0, 1},
  14.         {1, 0},
  15.         {0, -1},
  16.         {-1, 0}
  17.     };
  18.     int x, y, di;
  19.     vector<vector<int>> visited = maze;
  20.     visited[1][1] = -1;
  21.     s.push({1, 1, -1});
  22.     while (!s.empty()) {
  23.         Box temp = s.top();
  24.         s.pop();
  25.         x = temp.x; y = temp.y; di = temp.di + 1;
  26.         while (di < 4) {
  27.             int line = x + direct[di].incX;
  28.             int col = y + direct[di].incY;
  29.             if (visited[line][col] == 0) {
  30.                 s.push({x, y, di});
  31.                 x = line; y = col; visited[line][col] = -1;
  32.                 if (x == M && y == N) {
  33.                     return true;
  34.                 } else {
  35.                     di = 0;
  36.                 }
  37.             } else {
  38.                 di++;
  39.             }
  40.         }
  41.     }
  42.     return false;
  43. }
  44. int main() {
  45.     int M, N;
  46.     cout << "请输入迷宫数组行数和列数:";
  47.     cin >> M >> N;
  48.     vector<vector<int>> maze(M + 2, vector<int>(N + 2, 1));
  49.     for (int i = 1; i <= M; ++i) {
  50.         for (int j = 1; j <= N; ++j) {
  51.             cin >> maze[i][j];
  52.         }
  53.     }
  54.     stack<Box> s;
  55.     bool res = findPath(s, M, N, maze);
  56.     cout << boolalpha << res << endl;
  57.     if (res) {
  58.         while (!s.empty()) {
  59.             Box temp = s.top();
  60.             s.pop();
  61.             cout << "(" << temp.x << ", " << temp.y << ")" << endl;
  62.         }
  63.     } else {
  64.         cout << "你被困在迷宫中了,等着受死吧!" << endl;
  65.     }
  66.     return 0;
  67. }
复制代码

请注意,我已经删除了你自定义的栈类,并用 std::stack 替换了它。同时,我还将原来的二维数组替换为 vector<vector<int>> ,这是一个更现代且易于使用的数据结构。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-7 15:09:45 | 显示全部楼层
根据你给出的代码,你已经实现了一个自定义的 Stack 类,而这个 Stack 类并不是 C++ Standard Template Library (STL) 中的标准库。实际上,你的 Stack 类是基于链表的数据结构实现的。

如果你想使用 C++ STL 中的 stack ,你应该使用如下的方式包含头文件:

  1. #include <stack>
复制代码


然后你就可以像这样使用 STL 中的 stack:

  1. std::stack<int> s;  // 创建一个整数类型的栈
  2. s.push(10);         // 把一个整数压入栈
  3. int top = s.top();  // 获取栈顶元素
  4. s.pop();            // 弹出栈顶元素
复制代码


所以如果你看到 "找不到这个头文件" 的错误,可能是因为你错误地包含了 "Stack.h",而不是  <stack> 。确保你在代码中使用正确的库。

另外,值得注意的是,STL 中的  stack  是一个容器适配器,实际的底层数据结构可以是  deque (默认)或者  vector , list  等其他序列容器。如果你想用链表实现的栈,也可以选择 STL 中的  list 。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-7 15:10:28 | 显示全部楼层
陶远航 发表于 2023-7-7 15:07
从你的代码中,我看到你自己实现了一个栈,而没有使用C++标准模板库(STL)中的 std::stack 。如果你想使用ST ...

谢谢,但是有个小问题就是 vector<vector<int>>中间少了空格应该是 vector<vector<int> >
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-7 15:20:06 | 显示全部楼层
isdkz 发表于 2023-7-7 15:09
根据你给出的代码,你已经实现了一个自定义的 Stack 类,而这个 Stack 类并不是 C++ Standard Template Lib ...

算了,我也加入gpt大军吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-7 15:20:27 | 显示全部楼层
歌者文明清理员 发表于 2023-7-7 15:20
算了,我也加入gpt大军吧

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-7 22:10:30 | 显示全部楼层
歌者文明清理员 发表于 2023-7-7 15:20
算了,我也加入gpt大军吧

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 09:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表