使用boost :: assign和一个std :: set嵌套在一个std :: map [英] using boost::assign with a std::set nested inside a std::map

查看:264
本文介绍了使用boost :: assign和一个std :: set嵌套在一个std :: map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 boost :: assign 来模拟 std :: map的C ++ 11初始化包含 std :: set

I am trying to use boost::assign to emulate C++11 initialization of a std::map containing a std::set.

#include <set>
#include <map>
#include <stdint.h>

#include <boost/assign/list_of.hpp>

typedef std::map< uint32_t, std::set< uint32_t> > the_map_t;

the_map_t data = boost::assign::map_list_of( 1, boost::assign::list_of(10)(20)(30) )
                                           ( 2, boost::assign::list_of(12)(22)(32) )
                                           ( 3, boost::assign::list_of(13)(23)(33) )
                                           ( 4, boost::assign::list_of(14)(24)(34) );

初始化 std :: set code> boost :: assign :: list_of 在自己使用时可以正常工作,但是当我尝试上面的代码时,赋值是不明确的,在 std :: set 的构造函数被调用:

Initialisation of std::set using boost::assign::list_of works as expected when used on its own, but when I try the above code the assignment is ambiguous at the point where the std::set's constructor is called:

map-assign.cpp:16:   instantiated from here
include/c++/4.4.6/bits/stl_pair.h:101: error: call of overloaded set(const   boost::assign_detail::generic_list<int>&) is ambiguous
include/c++/4.4.6/bits/stl_set.h:188: note: candidates are: 
    std::set<_Key, _Compare, _Alloc>::set(
        const std::set<_Key, _Compare, _Alloc>&) 
        [with _Key = unsigned int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]

include/c++/4.4.6/bits/stl_set.h:145: note:                 
    std::set<_Key, _Compare, _Alloc>::set(
        const _Compare&, const _Alloc&) 
        [with _Key = unsigned int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]

如何解决此歧义错误?

推荐答案

c> boost :: assign :: map_list_of 需要第二个模板参数的提示 - < uint32_t,std :: set< uint32_t> > 。因此行

In this case boost::assign::map_list_of needs a hint for second template argument - <uint32_t, std::set< uint32_t> >. Therefore line

the_map_t data = boost::assign::map_list_of(...);

成为

the_map_t data = boost::assign::map_list_of<uint32_t, std::set< uint32_t> >(...);

这篇关于使用boost :: assign和一个std :: set嵌套在一个std :: map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆