简体   繁体   English

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

[英]C++ unable to use peek() function in stack

I am trying to use the peek function in Visual Studio 2010 with these libraries: 我试图在Visual Studio 2010中使用peek函数与这些库:

#include "stdafx.h"
#include <string>
#include <string.h>
#include <fstream>
#include <iostream>
#include <string.h>
#include <vector>
#include <stack>

However, I cannot use the peek function in the 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();
        }
    }
}

I get the error: 我收到错误:

Error 1 error C2039: 'peek' : is not a member of 'std::stack<_Ty>' 错误1错误C2039:'peek':不是'std :: stack <_Ty>'的成员

What am I doing wrong? 我究竟做错了什么?

I think you want to use 我想你想用

s.top();

instead of peak. 而不是高峰。

There's no peek function in std::stack . std::stack没有peek函数。

Are you looking for top() ? 你在寻找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();
        }
    }
}

There is no peek function in std::stack. std :: stack中没有peek函数。 For a reference, please see stack 有关参考,请参阅堆栈

It looks as if you are using the functionality as top would be. 看起来好像您正在使用top功能。 For a reference on top, take a look at this reference . 有关参考,请参阅此参考

Your code has stack , but you actually wanted to use Stack . 你的代码有stack ,但你实际上想要使用Stack They are two different things. 他们是两个不同的东西。

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

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