2013-04-04 81 views
1

我得到了下面的示例代碼編譯時錯誤,而使用unordered_map

// unordered_map::find 
#include <iostream> 
#include <string> 
#include <unordered_map> 

int main() 
{ 
    std::unordered_map<std::string,double> mymap = { 
    {"mom",5.4}, 
    {"dad",6.1}, 
    {"bro",5.9} }; 

    std::string input; 
    std::cout << "who? "; 
    getline (std::cin,input); 

    std::unordered_map<std::string,double>::const_iterator got = mymap.find (input); 

    if (got == mymap.end()) 
    std::cout << "not found"; 
    else 
    std::cout << got->first << " is " << got->second; 

    std::cout << std::endl; 

    return 0; 

當我試着使用VS 2010編譯它在Windows 7上,我得到編譯時錯誤(儘管它看起來OK我)

1>\testing.cpp(13): error C2552: 'mymap' : non-aggregates cannot be initialized with initializer list 
1>   'std::tr1::unordered_map<_Kty,_Ty>' : Types with a base are not aggregate 
1>   with 
1>   [ 
1>    _Kty=std::string, 
1>    _Ty=double 
1>   ] 
1>\testing.cpp(14): error C2078: too many initializers 
1>\testing.cpp(15): fatal error C1903: unable to recover from previous error(s); stopping compilation 

回答

7

您的編譯器(VC10)不支持統一初始化。您的程序compiles fine on a conforming compiler

+3

請注意,截至目前,VC的版本還沒有庫初始化器列表構造函數。 – chris 2013-04-04 17:29:27

+0

@chris:謝謝你指出 – 2013-04-04 17:30:48