[英]C++ unable to use peek() function in stack
我试图在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();
}
}
}
我收到错误:
错误1错误C2039:'peek':不是'std :: stack <_Ty>'的成员
我究竟做错了什么?
我想你想用
s.top();
而不是高峰。
std::stack
没有peek
函数。
你在寻找top()
吗?
void dfs(){
stack<Node> s;
s.push(nodeArr[root]);
nodeArr[root].setVisited();
nodeArr[root].print();
while(!s.empty()){
//peek yok?!
Node n=s.top(); // <-- top here
if(!n.below->isVisited()){
n.below->setVisited();
n.below->print();
s.push(*n.below);
}
else{
s.pop();
}
}
}
你的代码有stack
,但你实际上想要使用Stack
。 他们是两个不同的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.