boost :: interprocess :: map - 如何使用basic_string作为类型更新值

时间:2012-05-17 14:53:14

标签: c++ map allocation boost-interprocess stdstring

我有以下代码:

    typedef managed_shared_memory::segment_manager segment_manager_t;
    typedef allocator<void, segment_manager_t> void_allocator;
    typedef allocator<char, segment_manager_t> char_allocator;
    typedef basic_string<char, std::char_traits<char>, char_allocator> char_string;
    typedef std::pair<const char_string, char_string> map_value_type;
    typedef allocator<map_value_type, segment_manager_t>  map_value_type_allocator;
    typedef map< char_string, char_string, std::less<char_string>, map_value_type_allocator>  complex_map_type;

........

           managed_shared_memory segment(open_only,shm_name.c_str());

           //An allocator convertible to any allocator<T, segment_manager_t> type
           void_allocator alloc_inst (segment.get_segment_manager());

               complex_map_type *myMap = segment.find_or_construct<complex_map_type>("myMap")(std::less<char_string>(), alloc_inst);

              //Here I am trying to update value. Key exists in the map.
               (*myMap)[char_string(key.c_str(), alloc_inst)] = char_string(val.c_str(), alloc_inst);

有人能为我提供最后一行的正确语法吗? 最后一行给出了编译错误。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我得到了答案。

char_string key(var.c_str(), alloc_inst);
complex_map_type::iterator it = myMap->find(key);

if( it != myMap->end() )
 (*it).second = char_string(new_value.c_str(), alloc_inst);