2012-03-30 79 views
2

我試圖在Visual Studio 2010中使用這些庫使用peek功能:C++無法使用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>'

我在做什麼錯?

+1

'stack'參考:http://en.cppreference.com/w/cpp/container/stack – hmjd 2012-03-30 21:19:06

回答

5

中沒有peek函數。

您是在查找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(); 
     } 
    } 
} 
+0

非常感謝你。在msdn中,當我看到引用peek時,我想我是真實的。但你是對的,我錯了。再次感謝你的回覆。 – 2012-03-30 22:07:58

5

我想你想使用

s.top(); 

,而不是高峯。

1

std :: stack中沒有peek函數。如需參考,請參閱stack

看起來好像您使用的功能是top。有關頂部的參考,請查看this reference

1

您的代碼有stack,但實際上您想要使用Stack。他們是兩個不同的東西。

+0

以下是您需要的堆棧文件:http://msdn.microsoft.com/en-us/library/1w32446f.aspx#Y0 – TJD 2012-03-30 21:23:00