在内存映射文件中存储矢量 [英] Storing vector in memory mapped file

查看:168
本文介绍了在内存映射文件中存储矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储任意元素的内存映射文件的向量(现在我想用整数向量成功,但它应该与任意对象的矢量工作)。我发现与共享内存这样大量的文档,但无法与内存映射正确的文件。既然我已经成功地制造和使用的R-tree在内存映射文件(如在<一个href=\"http://www.boost.org/doc/libs/1_57_0/libs/geometry/doc/html/geometry/spatial_indexes/rtree_examples/index_stored_in_mapped_file_using_boost_interprocess.html\"相对=nofollow>这个例子),我试着用向量复制的过程,但我想我错过了一些关键要素,因为它不工作。这里是我的code:

I am attempting to store a vector of arbitrary elements in a memory mapped file (for now I'm trying to succeed with a vector of ints but it should work with vector of arbitrary objects). I have found plenty of documentation on doing so with shared memory, but not with memory mapped files proper. Since I have successfully made and used R-trees in memory mapped file (like in that example), I tried to replicate the process with vectors, but I guess I am missing some crucial element because it doesn't work. Here is my code:

namespace bi = boost::interprocess;
typedef bi::allocator<std::vector<int>, bi::managed_mapped_file::segment_manager> allocator_vec;
std::string vecFile = "/path/to/my/file/vector.dat";
bi::managed_mapped_file file_vec(bi::open_or_create,vecFile.c_str(), 1000);
allocator_vec alloc_vec(file_vec.get_segment_manager());
std::vector<int> * vecptr = file_vec.find_or_construct<std::vector<int> >("myvector")(alloc_vec);

可能是我最后一行是错误的,因为alloc_vec作为参数向量的构造,它不希望它被传递
(我得到在其他错误 / usr / include目录/ C ++ / 4.8 /位/ stl_vector.h:248:7:注意:考生预计0参数,提供1 )。
但是,我不知道再如何通过分配器find_or_construc(),我认为是至关重要的在内存映射文件正确创建的载体。删除(alloc_vec)在最后一行的末尾导致另一个错误,我有更多的麻烦来解决:

Probably my last line is wrong, because "alloc_vec" is passed as an argument to the vector constructor, which doesn't expect it (I get among others the error /usr/include/c++/4.8/bits/stl_vector.h:248:7: note: candidate expects 0 arguments, 1 provided). However, I don't know then how to pass the allocator to find_or_construc(), which I assume is crucial for the vector to be properly created in the memory mapped file. Removing (alloc_vec) at the end of the last line leads to another error that I have more trouble to solve :

error: cannot convert ‘boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>::construct_proxy<std::vector<int> >::type {aka boost::interprocess::ipcdetail::named_proxy<boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>, std::vector<int>, false>}’ to ‘std::vector<int>*’ in initialization
std::vector<int> * vecptr = file_vec.find_or_construct<std::vector<int> >("myvector");

任何帮助将大大AP preciated.`

Any help will be greatly appreciated.`

推荐答案

像样本显示,告诉您的自定义分配器的Vector类,所以不是

Like the samples show, tell the vector class about your custom allocator, so instead of

typedef std::vector<int>  MyVec;
MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(alloc_vec);

使用

typedef bi::allocator<int, bi::managed_mapped_file::segment_manager> int_alloc;
typedef std::vector<int, int_alloc>  MyVec;

int_alloc alloc(file_vec.get_segment_manager());
MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(alloc);

注意


  • 矢量使用分配器的元素类型(非向量; segment_manager分配的)

  • 自构造分配器&LT;&GT; 是隐含的,你也可以只通 segment_manager

  • vector uses an allocator for the element types (not for the vector; segment_manager allocates that)
  • since the constructor for allocator<> is implicit, you can also just pass the segment_manager:

<大骨节病> 住在Coliru

#include <boost/interprocess/managed_mapped_file.hpp>

namespace bi = boost::interprocess;

int main() {
    std::string vecFile = "vector.dat";
    bi::managed_mapped_file file_vec(bi::open_or_create,vecFile.c_str(), 1000);

    typedef bi::allocator<int, bi::managed_mapped_file::segment_manager> int_alloc;
    typedef std::vector<int, int_alloc>  MyVec;

    MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(file_vec.get_segment_manager());

    vecptr->push_back(rand());
}

这篇关于在内存映射文件中存储矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆