增强进程间向量,创建多个向量指针

发布于 2024-12-23 06:29:59 字数 1518 浏览 1 评论 0原文

我想使用以下代码将共享向量插入到共享地图中:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡紫姑娘! 2024-12-30 06:29:59

好吧,我之前的答案很愚蠢..所以这是正确的答案
我们必须使用匿名实例来获得我想要的东西。这是使用

vec *myvec = segment.construct<vec>(anonymous_instance)(vectorallocator);

它完成的,解释如下
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

vec *myvec = segment.construct<vec>(anonymous_instance)(vectorallocator);

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.

昔梦 2024-12-30 06:29:59

因为我不需要向量的名称,所以这就是我所做的。

unsigned int seconds;
srand ( time(NULL) );
seconds = rand() % 1000000;
std::stringstream buf;
buf << seconds;
vec *myvec = segment.construct<vec>((buf.str()).c_str())(vectorallocator);

我现在可以拥有很多 vec* 而不必考虑名称..:)

这只是一个微不足道的答案。

since i dont need the name of the vector, here is what i did.

unsigned int seconds;
srand ( time(NULL) );
seconds = rand() % 1000000;
std::stringstream buf;
buf << seconds;
vec *myvec = segment.construct<vec>((buf.str()).c_str())(vectorallocator);

and i can now have lots of vec* without having to consider for the name.. :)

this is just a trivial answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文