gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:01 25 4
gpt4 key购买 nike

我有以下代码:

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

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

预先感谢您的帮助。

最佳答案

我得到了答案。

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

关于c++ - boost::interprocess::map - 如何使用 basic_string 作为类型更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10638134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com