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

标签 c++ visual-c++ c++11 visual-studio-2012 std

我正在尝试编译 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/

相关文章:

多个输入的 C++ 大写函数失败

c++ - 在 visual studio 中使用 libuv 编译简单的 c++ 项目

windows - 最小化/最大化框在 MFC 对话框编辑器中变灰

c++ - 二维数组在退出循环后移除其元素

c++ - Derived对象调用Base方法的模板推导

c++ - 使用 std::mutex 数组时,条件变量不会触发

c++ - 无法为arm-none-linux-gnueabi-g++编译器找到-lpaho-mqtt3c

c++ - select() 和 recv 与 MSG_PEEK 之间的效率。异步

c++ - 初始化 const 类成员,错误 : declaration does not declare anything

c++ - 当 OpenGL 相机处于低高度(低 Z 坐标)时,为什么将窗口坐标映射到球体很困难?