3

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?

Chris
  • 44,602
  • 16
  • 137
  • 156
Saliha Uzel
  • 141
  • 2
  • 5
  • 17

4 Answers4

6

I think you want to use

s.top();

instead of peak.

Jarosław Gomułka
  • 4,935
  • 18
  • 24
5

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();
        }
    }
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Thank you for all so much. in msdn, when i saw reference to peek, i guess i am in the true way. but you are right, i was wrong. thank you again for replies. – Saliha Uzel Mar 30 '12 at 22:07
2

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.

josephthomas
  • 3,256
  • 15
  • 20
1

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

TJD
  • 11,800
  • 1
  • 26
  • 34
  • Here's the doc on the Stack you wanted: http://msdn.microsoft.com/en-us/library/1w32446f.aspx#Y0 – TJD Mar 30 '12 at 21:23