gpt4 book ai didi

c++ - 在 vector 图上使用 initializer_list

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

我一直在尝试初始化 <ints, vector<ints> > 的 map 使用新的 0X 标准,但我似乎无法获得正确的语法。我想制作一个带有 key:value = 1:<3,4>

的单个条目的 map
#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,{3,4}}是映射中的单个元素,其中键为1,值为{3,4}。因此,您需要的是 { {1,{3,4}} }

简化错误:

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

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

关于c++ - 在 vector 图上使用 initializer_list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2852400/

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