简体   繁体   English

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

[英]boost::interprocess::map - how to update value with basic_string as type

I have the following codes: 我有以下代码:

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

Can anyone give me correct syntax for the last line? 谁能给我最后一行的正确语法? The last line gives me a compilation error. 最后一行给我一个编译错误。

Thanks in advance for your help. 在此先感谢您的帮助。

I've got the answer. 我有答案。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM