我正在尝试将 Visual Studio 2010 中的 peek
函数与这些库一起使用:
#include "stdafx.h"
#include <string>
#include <string.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <vector>
#include <stack>
但是,我无法在堆栈中使用 peek
函数:
void dfs(){
stack<Node> s;
s.push(nodeArr[root]);
nodeArr[root].setVisited();
nodeArr[root].print();
while(!s.empty()){
//peek yok?!
Node n=s.peek();
if(!n.below->isVisited()){
n.below->setVisited();
n.below->print();
s.push(*n.below);
}
else{
s.pop();
}
}
}
我得到错误:
Error 1 error C2039: 'peek' : is not a member of 'std::stack<_Ty>'
我做错了什么?
最佳答案
我想你想用
s.top();
而不是峰值。
关于C++ 无法在堆栈中使用 peek() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9950535/