[英]boost interprocess unordered_map string
I am trying to create a unordered_map in shared memory using Boost Interprocess library. 我试图使用Boost Interprocess库在共享内存中创建一个unordered_map。 Here, is the code, which I'm trying to use (taking examples from Boost Interprocess documentation): 这里是代码,我正在尝试使用(从Boost Interprocess文档中获取示例):
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <functional>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <iostream>
#include <string>
#include <boost/interprocess/containers/string.hpp>
namespace bipc = boost::interprocess;
typedef bipc::allocator<char, bipc::managed_shared_memory::segment_manager> CharAllocator;
typedef bipc::basic_string<char, std::char_traits<char>, CharAllocator> ShmemString;
struct Person
{
int age;
ShmemString name;
double salary;
Person(int i,
double sal,
const char* s,
const char_allocator& a)
: age(i),
name(s, a),
salary(sal)
{
}
void print() {}
}
typedef ShmemString KeyType;
typedef Person MappedType;
typedef std::pair< KeyType, MappedType > MapPersonType;
typedef bipc::allocator< MapPersonType,
bipc::managed_shared_memory::segment_manager >
ShMemAllocator;
typedef boost::unordered_map< KeyType,
MappedType,
boost::hash<KeyType>,
std::equal_to<KeyType>,
ShMemAllocator >
PersonMap;
This is what I'm trying to do in the main program: 这就是我在主程序中要做的事情:
int main()
{
bipc::managed_shared_memory segment(bipc::create_only, "MySharedMemory", 65536);
PersonMap *persons = segment.construct<PersonMap>("MyHashMap")
( 3, boost::hash<ShmemString>(), std::equal_to<ShmemString>()
, segment.get_allocator<MapPersonType>());
char_allocator alloc(segment.get_allocator<char>());
Person p1(20, 10000, "ABC", alloc);
persons->insert(MapPersonType(ShmemString("Person1", alloc), p1));
}
Using the above code, I'm able to create an unordered_map in shared memory. 使用上面的代码,我可以在共享内存中创建一个unordered_map。 However, when I am trying to access the map, I need to use a syntax like 但是,当我尝试访问地图时,我需要使用类似的语法
persons->at(ShmemString("H", segment.get_allocator<char>())).print();
However, I would prefer to do this with a std::string, which results in compilation errors: 但是,我更喜欢使用std :: string来执行此操作,这会导致编译错误:
persons->at(std::string("H")).print();
Is it possible to write the above statement, ie accessing the map allocated in shared memory with std::string? 是否可以编写上述语句,即使用std :: string访问共享内存中分配的映射?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.