gpt4 book ai didi

c++ - 使用 unordered_map 时编译时错误

转载 作者:行者123 更新时间:2023-11-30 00:36:33 25 4
gpt4 key购买 nike

我得到了下面的示例代码

// 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;

当我尝试在 Windows 7 上使用 VS 2010 编译它时,出现编译时错误(尽管对我来说看起来没问题)

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

最佳答案

您的编译器 (VC10) 不支持统一初始化。你的程序compiles fine on a conforming compiler .

关于c++ - 使用 unordered_map 时编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15817764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com