Boost进程间映射与int和string
boost interprocess map with int and string
我有以下代码,使用boost进程间
将映射保存到共享内存中 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));
}
}
这段代码不能编译。它抛出以下错误
no matching function for call to ‘std::pair<const int, boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >::pair(int&, main()::MyShmString&)’
/usr/include/c++/4.2.1/bits/stl_pair.h:84: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = const int, _T2 = boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
/usr/include/c++/4.2.1/bits/stl_pair.h:80: note: std::pair<_T1, _T2>::pair() [with _T1 = const int, _T2 = boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> >]
/usr/include/c++/4.2.1/bits/stl_pair.h:69: note: std::pair<const int, boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >::pair(const std::pair<const int, boost::interprocess::allocator<boost::container::basic_string<char, std::char_traits<char>, boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0ul>, boost::interprocess::iset_index> > >&)
::pair(int&, main()::MyShmString&)
所以我猜
mymap->insert(std::pair<const int, MappedType>(i, mystring));
不是正确的方法…如果有错误,我该如何插入到映射中。否则误差是多少?
value_type当然是:
typedef std::pair<const int, MyShmString > ValueType;
不typedef std::pair<const int, StringAllocator> ValueType;
和类似:
typedef MyShmString MappedType;
不typedef StringAllocator MappedType;
你的类型不同,这是故意的吗?MyShmString
和MappedType
不一样——可能你应该改变下面的行:
typedef StringAllocator MappedType;
typedef MyShmString MappedType;
相关文章:
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 两个数组,int 和 string,如何显示带有对应 int 的字符串?
- <string> 如何在使用 SWIG 时将 int 数组和 List 作为参数传递给 C# C++
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- map<string, int> m= {}; 在C++中是什么意思?
- 如何从map<pair<string,int>,pair<string,Array>>中迭代和查找?
- C++将文件行分离为String和Int
- 与 string.size() 比较时 int 的符号性显示警告
- freq_table[field_pm_value] = map<string, int>(); 是什么意思?
- 将 int 附加到 std::string
- 如何在一对集合的向量中插入元素?vector<pair<int,set<string>>>
- 按值对 std::unordered_map<std:::string, std::atomic<unsigned int>> 进行排序
- 在 C++03 中将 std::string 转换为 int
- 方括号在"map <string, int> instance[numberFeatures];"中是什么意思
- 如何使用 c++ 中的 int* count(const string&s)计算字符串中每个数字的出现次数?
- 如果可以将其放在私有中,为什么要使用静态常量(int/string/.)?
- 在字节数组存储中嵌入 int/string
- c++中从double/int/string到bool的自动转换
- c++ string (int) + string (int)
- 如何在int,string类型的映射上使用find