gpt4 book ai didi

c++ - unordered_map - {{key,value},{key,value}} 语法无效

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:38 25 4
gpt4 key购买 nike

我正在尝试编译 the code taken from here

// constructing unordered_maps
#include <iostream>
#include <string>
#include <unordered_map>

typedef std::unordered_map<std::string,std::string> stringmap;

stringmap merge (stringmap a,stringmap b) {
stringmap temp(a); temp.insert(b.begin(),b.end()); return temp;
}

int main ()
{
stringmap first; // empty
stringmap second ( {{"apple","red"},{"lemon","yellow"}} ); // init list
stringmap third ( {{"orange","orange"},{"strawberry","red"}} ); // init list
stringmap fourth (second); // copy
stringmap fifth (merge(third,fourth)); // move
stringmap sixth (fifth.begin(),fifth.end()); // range

std::cout << "sixth contains:";
for (auto& x: sixth) std::cout << " " << x.first << ":" << x.second;
std::cout << std::endl;

return 0;
}

使用 MSVC2012 但我正在接收

error C2143: syntax error : missing ')' before '{'

在代码行上

stringmap second ( {{"apple","red"},{"lemon","yellow"}} );       // init list

我错过了什么吗?

最佳答案

Visual Studio 2012 缺少许多现代 C++ 功能,其中包括初始化程序列表。参见 here概览。

关于c++ - unordered_map - {{key,value},{key,value}} 语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15766237/

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