C++无法在堆栈中使用peek()函数

Sal*_*zel 2 c++ stack peek

我试图peek在Visual Studio 2010中使用这些库中的函数:

#include "stdafx.h"
#include <string>
#include <string.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <vector>
#include <stack>
Run Code Online (Sandbox Code Playgroud)

但是,我不能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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

错误1错误C2039:'peek':不是'std :: stack <_Ty>'的成员

我究竟做错了什么?

Jar*_*łka 6

我想你想用

s.top();
Run Code Online (Sandbox Code Playgroud)

而不是高峰.


Luc*_*ore 5

没有任何peek功能std::stack.

你在找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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)