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

Joh*_*ing 6 c++ std visual-c++ c++11 visual-studio-2012

我正在尝试编译从这里获取的代码

// 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;
}
Run Code Online (Sandbox Code Playgroud)

与MSVC2012,但我收到了

错误C2143:语法错误:在'{'之前缺少')'

在代码行上

stringmap second ( {{"apple","red"},{"lemon","yellow"}} );       // init list
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

bas*_*h.d 5

Visual Studio 2012缺少许多现代C++功能,其中包括initialiser lists.请参阅此处以获取概述.

  • 然而,暂时它是真的.我没有说它永远不会 (3认同)