在向量映射上使用initializer_list

时间:2010-05-17 20:14:18

标签: c++ stl c++11

我一直在尝试使用新的0X标准初始化<ints, vector<ints> >的地图,但我似乎无法使语法正确。我想制作一个带有键的单个条目的地图:value = 1:&lt; 3,4&gt;

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

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

....

使用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标志。这样做有危险吗?

1 个答案:

答案 0 :(得分:1)

正如上面的评论所提到的,{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>)

不是一个确切的错误,但仍然有些帮助。

相关问题