3

我正在尝试将peekVisual Studio 2010 中的函数与这些库一起使用:

#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>' 的成员

我究竟做错了什么?

4

4 回答 4

6

我想你想用

s.top();

而不是高峰。

于 2012-03-30T21:17:43.133 回答
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();
        }
    }
}
于 2012-03-30T21:17:53.630 回答
2

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

看起来好像您正在使用该功能top。有关顶部的参考,请查看此参考

于 2012-03-30T21:18:37.853 回答
1

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

于 2012-03-30T21:20:53.800 回答