1

我有以下代码:

    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);

谁能给我最后一行的正确语法?最后一行给了我一个编译错误。

在此先感谢您的帮助。

4

1 回答 1

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);
于 2012-05-18T14:47:54.183 回答