提升中含糊不清的map.insert

时间:2014-10-16 16:38:00

标签: c++ boost

我正在尝试将值插入到boost map中,但对boost语句插入中插入语句的调用是不明确的。

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <utility>

using namespace boost::interprocess;

    int main ()
    {

    // remove earlier existing SHM
    shared_memory_object::remove("SharedMemoryName");

    // create new
    managed_shared_memory segment(create_only,"SharedMemoryName",65536);

    //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
    //so the allocator must allocate that pair.
    typedef std::string KeyType;
    typedef map<std::string, int>  MappedType;
    typedef std::pair<const KeyType, MappedType> ValueType;

    //allocator of for the map.
    typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;


    //Initialize the shared memory STL-compatible allocator
    ShmemAllocator alloc_inst (segment.get_segment_manager());

    //third parameter argument is the ordering function is used to compare the keys.
    typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MySHMMap;

    //offset ptr within SHM for map
    offset_ptr<MySHMMap> m_pmap = segment.construct<MySHMMap>("MySHMMapName")(std::less<std::string>(), alloc_inst);

    //Insert data in the map
           std::string my_string = "test";
            m_pmap[0].insert(std::make_pair( my_string, 0) );


    return 0;
}

被调用的boost API是:

    std::pair<iterator,bool> insert(const nonconst_value_type& x)
    std::pair<iterator,bool> insert(const value_type& x)
    typedef typename tree_t::value_type             value_type;
    typedef std::pair<key_type, mapped_type>        nonconst_value_type;

错误日志:

/home/user/droy/src/quotes/shmmutimap/src/writer.cxx:39: error:  call of overloaded 'insert(std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>)' is ambiguous
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/map.hpp:410: note: candidates are: std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::iterator, bool> boost::container::map<Key, T, Pred, Alloc>::insert(const typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::value_type&) [with Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T = boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >, Pred = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Alloc = boost::interprocess::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
/home/dev/build/third_party/64-rhel5/boost_1_47_0/include/boost/interprocess/containers/container/map.hpp:421: note:                 std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::iterator, bool> boost::container::map<Key, T, Pred, Alloc>::insert(const std::pair<typename boost::container::containers_detail::rbtree<Key, std::pair<const Key, T>, boost::container::containers_detail::select1st<std::pair<const Key, T> >, Pred, Alloc>::key_type, T>&) [with Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, T = boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >, Pred = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, Alloc = boost::interprocess::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::container::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]

insert语句的参数最终是同一类型,我怎么能解决这个问题,任何提示?

1 个答案:

答案 0 :(得分:1)

现在,您创建了一个map<string, map<string, int>>。我想你的意图是创建一个map<string, int>。要做到这一点,您需要对代码进行非常简单的修复:

// typedef map<std::string, int>  MappedType;
typedef int MappedType;

另外,您还可以将代码的最后一行写为:

// m_pmap[0].insert(std::make_pair( my_string, 0) );
m_pmap[0][my_string] = 0;

更新

由于我的直觉似乎是错误的,并且确实想要创建map<string, map<string, int>>,因此回复略有不同。

请注意,您的通话基本上是尝试这样做:

map<string, int> x(0);
m_pmap[0][my_string] = x;

请注意,map<string, int>没有构造函数采用整数。相反,你可以做任何一个:

m_pmap[0].insert(std::make_pair( my_string, map<std::string, int>()));
m_pmap[0].insert(std::make_pair( my_string, MappedType()));
m_pmap[0][my_string] = map<std::string, int>();
m_pmap[0][my_string] = MappedType();

您还可以简单地利用map的工作原理,并执行:

m_pmap[0]["us"]["abc"] = 2030;
m_pmap[0]["us"]["def"] = 1230;