andre9875
andre9875

Reputation: 31

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.

Upvotes: 1

Views: 834

Answers (1)

andre9875
andre9875

Reputation: 31

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

Upvotes: 1

Related Questions