C++ 使用int和string增强进程间映射

C++ 使用int和string增强进程间映射,c++,boost,map,boost-interprocess,C++,Boost,Map,Boost Interprocess,下面的代码使用boost interprocess将映射保存到共享内存中 using namespace boost::interprocess; //Shared memory front-end that is able to construct objects //associated with a c-string. Erase previous shared memory with the name //to be used and create the memory segm

下面的代码使用boost interprocess将映射保存到共享内存中

    using namespace boost::interprocess;
//Shared memory front-end that is able to construct objects
//associated with a c-string. Erase previous shared memory with the name
//to be used and create the memory segment at the specified address and initialize resources
shared_memory_object::remove("MySharedMemory");

try{
    managed_shared_memory segment
        (create_only
         ,"MySharedMemory" //segment name
         ,655360);          //segment size in bytes

    //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>,
    //so the allocator must allocate that pair.
    typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
    typedef basic_string<char, std::char_traits<char> ,CharAllocator> MyShmString;
    typedef allocator<MyShmString, managed_shared_memory::segment_manager> StringAllocator;

    typedef int    KeyType;
    typedef std::pair<const int, StringAllocator> ValueType;
    typedef StringAllocator MappedType;

    typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;

    typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap;

    //Initialize the shared memory STL-compatible allocator
    ShmemAllocator alloc_inst (segment.get_segment_manager());
    CharAllocator charallocator  (segment.get_segment_manager());


    //Construct a shared memory map.
    //Note that the first parameter is the comparison function,
    //and the second one the allocator.
    //This the same signature as std::map's constructor taking an allocator
    MyMap *mymap =
        segment.construct<MyMap>("MyMap")      //object name
        (std::less<int>() //first  ctor parameter
         ,alloc_inst);     //second ctor parameter

    //Insert data in the map
    MyShmString mystring(charallocator);
    mystring = "this is my text";
    for(int i = 0; i < 100; ++i){
        //mymap[i] = mystring;
        mymap->insert(std::pair<const int, MappedType>(i, mystring));
    }
}
所以我猜

            mymap->insert(std::pair<const int, MappedType>(i, mystring));
mymap->insert(std::pair(i,mystring));

这不是正确的方法。。那么,如果存在错误,我应该如何插入到地图中。。否则会出现什么错误?

值类型肯定是:

typedef std::pair<const int, MyShmString > ValueType;
不是


你的类型不同,这是故意的吗
MyShmString
MappedType
不同-可能您应该更改以下行:

typedef StringAllocator MappedType;


你的typedef很混乱。你应该把它清理干净。例如,如果
map
已经有这样的typedef(
std::map::value\u type
),为什么要定义
ValueType
typedef std::pair<const int, MyShmString > ValueType;
typedef std::pair<const int, StringAllocator> ValueType;
typedef MyShmString MappedType;
typedef StringAllocator MappedType;
typedef StringAllocator MappedType;
typedef MyShmString MappedType;