在向量映射上使用initializer_list

Hoo*_*ked 7 c++ stl c++11

我一直在尝试初始化<ints, vector<ints> >使用新的0X标准的地图,但我似乎无法使语法正确.我想用一个带有键的单个条目创建一个映射:value = 1:<3,4>

#include <initializer_list>
#include <map>
#include <vector>
using namespace std;

map<int, vector<int> > A = {1,{3,4}};

....
Run Code Online (Sandbox Code Playgroud)

使用gcc 4.4.3它会因以下错误而死:

error: no matching function for call to std::map<int,std::vector<int,std::allocator<int> >,std::less<int>,std::allocator<std::pair<const int,std::vector<int,std::allocator<int> > > > >::map(<brace-enclosed initializer list>)

编辑

按照Cogwheel的建议并添加额外的支撑,它现在编译了一个警告,可以摆脱使用-fno-deduce-init-list标志.这样做有危险吗?

Buğ*_*dik 3

正如上面的评论所提到的,{1,{3,4}}是映射中的单个元素,其中键是1,值是{3,4}。所以,你需要的是{ {1,{3,4}} }.

简化错误:

error: no matching function for call to map<int,vector<int>>::map(<brace-enclosed initializer list>)
Run Code Online (Sandbox Code Playgroud)

虽然不是一个精确的错误,但还是有帮助的。