繁体   English   中英

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

[英]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();
        }
    }
}

std :: stack中没有peek函数。 有关参考,请参阅堆栈

看起来好像您正在使用top功能。 有关参考,请参阅此参考

你的代码有stack ,但你实际上想要使用Stack 他们是两个不同的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM