增强进程间向量,创建多个向量指针
我想使用以下代码将共享向量插入到共享地图中:
managed_shared_memory segment(create_only ,"MySharedMemory" ,65536);
typedef allocator<int, managed_shared_memory::segment_manager> vecAllocator;
typedef vector<int, vecAllocator> vec;
typedef std::pair<int, vec* > ValueType;
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
typedef multimap<int, vec*, std::less<int>, ShmemAllocator> MyMap;
ShmemAllocator alloc_inst (segment.get_segment_manager());
vecAllocator vectorallocator (segment.get_segment_manager());
MyMap *mymap = segment.construct<MyMap>("MyMap")(std::less<int>(),alloc_inst);
vec *myvec = segment.construct<vec>("myvec")(vectorallocator);
vec *myvec1 = segment.construct<vec>("myvec1")(vectorallocator);
myvec->push_back(10);
myvec->push_back(9);
myvec->push_back(8);
myvec1->push_back(987);
myvec1->push_back(123);
myvec1->push_back(456);
for(int i = 0; i < 6; ++i){
for(int j = 0; j<6; j++)
mymap->insert(std::pair<int, vec*>(i, myvec));
}
for(int i = 0; i < 6; ++i){
for(int j = 0; j<6; j++)
mymap->insert(std::pair<int, vec*>(i, myvec1));
}
代码有效..但我想要做的是构造一个没有名称的向量。就是做这样的事情,
vec *myvec = segment.construct<vec>(vectorallocator);
vec *myvec1 = segment.construct<vec>(vectorallocator);
这样如果我把它放在一个循环中,并且每个循环都以不同的方式命名它们就没有问题了。有办法这样做吗? 或者至少有一种自动方法可以在每个循环中以不同的方式命名它们?
I want to insert a shared vector into a shared map using the following code:
managed_shared_memory segment(create_only ,"MySharedMemory" ,65536);
typedef allocator<int, managed_shared_memory::segment_manager> vecAllocator;
typedef vector<int, vecAllocator> vec;
typedef std::pair<int, vec* > ValueType;
typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator;
typedef multimap<int, vec*, std::less<int>, ShmemAllocator> MyMap;
ShmemAllocator alloc_inst (segment.get_segment_manager());
vecAllocator vectorallocator (segment.get_segment_manager());
MyMap *mymap = segment.construct<MyMap>("MyMap")(std::less<int>(),alloc_inst);
vec *myvec = segment.construct<vec>("myvec")(vectorallocator);
vec *myvec1 = segment.construct<vec>("myvec1")(vectorallocator);
myvec->push_back(10);
myvec->push_back(9);
myvec->push_back(8);
myvec1->push_back(987);
myvec1->push_back(123);
myvec1->push_back(456);
for(int i = 0; i < 6; ++i){
for(int j = 0; j<6; j++)
mymap->insert(std::pair<int, vec*>(i, myvec));
}
for(int i = 0; i < 6; ++i){
for(int j = 0; j<6; j++)
mymap->insert(std::pair<int, vec*>(i, myvec1));
}
the code works.. but what i want to do is construct a vector without a name. that is do something like this
vec *myvec = segment.construct<vec>(vectorallocator);
vec *myvec1 = segment.construct<vec>(vectorallocator);
so that if i put it inside a loop and there is no problem with naming them differently every loop. Is there a way to do so?
Or atleast is there an automatic way to name them differently every loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我之前的答案很愚蠢..所以这是正确的答案
我们必须使用匿名实例来获得我想要的东西。这是使用
它完成的,解释如下
http://www.boost.org/doc/libs/1_48_0/doc/html/interprocess/driven_memory_segments.html#interprocess.management_memory_segments.driven_memory_segment_features.anonymous
它还解释了其他一些好的功能。
Well my previous answer was STUPID.. so here is the correct answer
We have to use anonymous instance to get what i want. It is done using
it is explained here
http://www.boost.org/doc/libs/1_48_0/doc/html/interprocess/managed_memory_segments.html#interprocess.managed_memory_segments.managed_memory_segment_features.anonymous
It also explains some other good features.
因为我不需要向量的名称,所以这就是我所做的。
我现在可以拥有很多 vec* 而不必考虑名称..:)
这只是一个微不足道的答案。
since i dont need the name of the vector, here is what i did.
and i can now have lots of vec* without having to consider for the name.. :)
this is just a trivial answer.