문제

I am trying to use the peek function in Visual Studio 2010 with these libraries:

#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:

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>'

What am I doing wrong?

도움이 되었습니까?

해결책

There's no peek function in std::stack.

Are you looking for 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();
        }
    }
}

다른 팁

I think you want to use

s.top();

instead of peak.

There is no peek function in std::stack. For a reference, please see stack

It looks as if you are using the functionality as top would be. For a reference on top, take a look at this reference.

Your code has stack, but you actually wanted to use Stack. They are two different things.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top