在向量映射上使用initializer_list

发布于 2024-09-02 08:16:46 字数 835 浏览 3 评论 0 原文

我一直在尝试初始化 的映射> 使用新的 0X 标准,但我似乎无法获得正确的语法。我想制作一个包含 key:value = 1:<3,4> 的单个条目的地图

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

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

....

使用 gcc 4.4.3 时,它因以下错误而终止:

error: nomatching function for call to std::map; >,std::less,std::allocator; > > > >::map()

Edit

按照 Cogwheel 的建议并添加额外的大括号,它现在编译时带有一个警告,可以使用它来消除该警告-fno-deruce-init-list 标志。这样做有危险吗?

I've been trying to initialize a map of <ints, vector<ints> > using the new 0X standard, but I cannot seem to get the syntax correct. I'd like to make a map with a single entry with key:value = 1:<3,4>

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

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

....

It dies with the following error using 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>)

Edit

Following the suggestion by Cogwheel and adding the extra brace it now compiles with a warning that can be gotten rid of using the -fno-deduce-init-list flag. Is there any danger in doing so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生殊途 2024-09-09 08:16:46

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

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

As the comment above has mentioned, {1,{3,4}} is a single element in the map, where the key is 1 and the value is {3,4}. So, what you would need is { {1,{3,4}} }.

Simplifying the error:

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

Not a precise error, but somewhat helpful nonetheless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文