2013-07-02 19 views
-1

我正在學習stl功能的第一次,所以這段代碼是從dietel,我想實現在dev-C++ orwell 5.4,但代碼不運行,是什麼問題。 stl庫不包含在dev-cpp中嗎?stl庫在開發cpp

它顯示錯誤 - 地圖不命名一個類型?

#include <iostream> 
#include <map> 

typedef map< int , double , less<int> > Mid ; 
using namespace std ; 

int main() 
{ 
Mid pairs ; 

pairs.insert(Mid::value_type(15 , 2.7)) ; 
pairs.insert(Mid::value_type(30 , 111.11)) ; 
pairs.insert(Mid::value_type(5 , 1010.1)) ; 
pairs.insert(Mid::value_type(10 , 22.22)) ; 
pairs.insert(Mid::value_type(25 , 33.333)) ; 
pairs.insert(Mid::value_type(5 , 77.54)) ; 
pairs.insert(Mid::value_type(20 , 9.345)) ; 
pairs.insert(Mid::value_type(15 , 99.3)) ; 

cout << "pairs contains:\nKey\tValue\n" ; 

for(Mid::const_iterator iter = pairs.begin() ; 
    iter != pairs.end() ; ++iter) 
    cout << iter->first << '\t' << iter->second << '\n' ; 

pairs[ 25 ] = 9999.99 ; 
pairs[ 40 ] = 8765.43 ; 
cout << endl ; 
cout << "After subscript operations: " ; 
cout << endl ; 
for(Mid::const_iterator iter = pairs.begin() ; 
    iter != pairs.end() ; ++iter) 
    cout << iter->first << '\t' << iter->second << '\n' ; 

cout << endl ; 
return 0 ; 

} 
+0

請包括您收到的_exact_錯誤,包含行號信息及其原始文本。 –

+0

由於答案沒有提到這一點,請參閱[爲什麼使用命名空間std認爲不好的做法?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice ) – juanchopanza

+0

好吧,我明白爲什麼它不被使用! – AbKDs

回答

1
#include <iostream> 
#include <map> 

typedef map< int , double , less<int> > Mid ; 
using namespace std ; 

在此代碼段,您使用map告訴您正在使用THTè命名空間之前std。因此,你的編譯器不知道在哪裏尋找map,並告訴你它從來沒有聽說過它。只寫:

using namespace std ; 
typedef map< int , double , less<int> > Mid ; 

它應該解決這個問題。

1

使用空間std之前你typedefing地圖;,這就是爲什麼編譯器無法看到它