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

标签 c++ stack peek

我正在尝试将 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();
        }
    }
}

我得到错误:

Error 1 error C2039: 'peek' : is not a member of 'std::stack<_Ty>'

我做错了什么?

最佳答案

我想你想用

s.top();

而不是峰值。

关于C++ 无法在堆栈中使用 peek() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9950535/

相关文章:

c++ - 带有 vsprintf (C++) 的查询助手的 MySQL 动态字符串大小

c++ - 提取整数的数字元素

java - peek() 或不 peek()

qt - 查看 QTextStream

java - 同步链表 - peek

c++ - 人们如何想出按位问题的解决方案?

c++ - 操作数据成员 (C++)

c - 使用动态数组实现堆栈

c# - 在 C# 中使用 ref 参数在堆栈上会发生什么?

java - 为什么在ArrayDeque和Stack中使用push()方法时输出不同?