Boost logo

Boost :

Subject: Re: [boost] [interprocess] map of vectors
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2008-11-25 15:04:54

Jan Stetka wrote:
> Hi,
> Is something like this likely to work?
> typedef interprocess::vector<int, ShmemAllocator> MyVector
> typedef std::pair<const long, MyVector> ValueType;
> typedef allocator<ValueType, managed_shared_memory::segment_manager>
> ShmemAllocator;
> typedef interprocess::map<long, MyVector, std::less<long>,
> ShmemAllocator> MyMap;

Yes, it should:

#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/map.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>

using namespace boost::interprocess;

typedef managed_shared_memory::segment_manager Mngr;

typedef allocator<int, Mngr> ShmemIntAllocator;
typedef vector<int, ShmemIntAllocator> MyVector;
typedef std::pair<const long, MyVector>ValueType;
typedef allocator<ValueType, Mngr> ShmemValueTypeAllocator;
typedef map<long, MyVector
    , std::less<long>, ShmemValueTypeAllocator> MyMap;

int main()
    managed_shared_memory shmem(open_or_create, "name", 65536);

    MyMap *m = shmem.construct<MyMap>("Map name")
       (std::less<long>(), shmem.get_segment_manager());

    //This is the usual way
    ValueType v(0, MyVector(shmem.get_segment_manager()));
    //This is more efficient(data is moved instead of copied)
    //Note that "long" is not "const long"
    std::pair<long, MyVector> mv
       (1, MyVector(shmem.get_segment_manager()));

    //Even more efficient, in-place construction:
    //the first argument is for the key (long),
    //the rest are for the mapped type
    m->emplace(2, shmem.get_segment_manager());
    return 0;

Boost list run by bdawes at, gregod at, cpdaniel at, john at