{"id":4378,"date":"2020-07-10T16:48:12","date_gmt":"2020-07-10T08:48:12","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4378"},"modified":"2020-07-10T16:51:58","modified_gmt":"2020-07-10T08:51:58","slug":"c%e4%b8%ad%e7%94%a8boost%e5%ae%9e%e7%8e%b0%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98%e5%88%9b%e5%bb%bamap%e5%ae%b9%e5%99%a8-2","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4378","title":{"rendered":"c++\u4e2d\u7528boost\u5b9e\u73b0\u5171\u4eab\u5185\u5b58\u521b\u5efamap\u5bb9\u5668"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"enlighter\">#include &lt;boost\/interprocess\/managed_shared_memory.hpp&gt;\r\n#include &lt;boost\/interprocess\/allocators\/allocator.hpp&gt;\r\n#include &lt;boost\/interprocess\/containers\/map.hpp&gt;\r\n#include &lt;boost\/interprocess\/containers\/string.hpp&gt;\r\n#include &lt;iostream&gt;\r\n\r\nusing namespace boost::interprocess;\r\nusing std::cout;\r\nusing std::endl;\r\n\r\n\/\/\u7c7b\u578b\u548callocator\u7684\u5b9a\u4e49\uff0c\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u65f6\u9700\u8981\u4f7f\u7528boost::interprocess\u4e2d\r\n\/\/\u91cd\u65b0\u5b9e\u73b0\u7684\u5bb9\u5668\u800c\u4e0d\u80fd\u4f7f\u7528\u6807\u51c6\u5bb9\u5668\r\ntypedef managed_shared_memory::segment_manager segment_manager_t;\r\ntypedef allocator&lt;void, segment_manager_t&gt; void_allocator;\r\n\r\n\/*-- \u81ea\u5b9a\u4e49string\u6570\u636e\u7c7b\u578b --*\/\r\ntypedef allocator&lt;char, segment_manager_t&gt; char_allocator;\r\ntypedef basic_string&lt;char, std::char_traits&lt;char&gt;, char_allocator&gt; char_string;\r\n\r\n\/*-- \u81ea\u5b9a\u4e49map&lt;string, string&gt;\u6570\u636e\u7c7b\u578b --*\/\r\ntypedef std::pair&lt;const char_string, char_string&gt; string_string_value_type;\r\ntypedef allocator&lt;string_string_value_type, segment_manager_t&gt; string_string_map_allocator;\r\ntypedef map&lt;char_string, char_string, std::less&lt;char_string&gt;, string_string_map_allocator&gt;\r\n    string_string_map;\r\n\r\n\/*-- \u81ea\u5b9a\u4e49map&lt;string, map&lt;string, string&gt;&gt; --*\/\r\ntypedef std::pair&lt;const char_string, string_string_map&gt; string_string_string_value_type;\r\ntypedef allocator&lt;string_string_string_value_type, segment_manager_t&gt;\r\n    string_string_string_map_allocator;\r\ntypedef map&lt;char_string, string_string_map, std::less&lt;char_string&gt;,\r\n    string_string_string_map_allocator&gt;\r\n    string_string_string_map;\r\n\r\nint main() {\r\n    \/* ---\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c\u5e76\u521b\u5efamap--- *\/\r\n\r\n    \/\/\u79fb\u9664\u5171\u4eab\u5185\u5b58\r\n    shared_memory_object::remove(\"MySharedMemory\");\r\n    \/\/\u521b\u5efa\u5171\u4eab\u5185\u5b58\uff0c\u5e76\u6307\u5b9a\u540d\u79f0\u548c\u5927\u5c0f\r\n    managed_shared_memory segment(open_or_create, \"MySharedMemory\", 102400);\r\n\r\n    void_allocator alloc_inst(segment.get_segment_manager());\r\n\r\n    \/\/\u5728\u5171\u4eab\u5185\u5b58\u4e0a\u6784\u5efastring_string_string_map\uff0c\u5e76\u8fd4\u56de\u6307\u9488\r\n    string_string_string_map* table_map = segment.construct&lt;string_string_string_map&gt;(\"table_map\")(\r\n        std::less&lt;char_string&gt;(), alloc_inst);\r\n\r\n    \/\/\u6784\u5efa\u81ea\u5b9a\u4e49string\r\n    char_string key_key_string(\"tag\", alloc_inst);\r\n    char_string key_string(\"fvalue\", alloc_inst);\r\n    char_string value_string(\"tag_fvalue\", alloc_inst);\r\n\r\n    \/\/\u6784\u5efa\u81ea\u5b9a\u4e49map&lt;string, string&gt;\u7684\u952e\u503c\u5bf9\uff0c\u4e0d\u80fd\u591f\u4f7f\u7528std::string\r\n    string_string_value_type ss_map_value(key_string, value_string);\r\n\r\n    \/\/\u5728\u5171\u4eab\u5185\u5b58\u4e0a\u6784\u5efastring_string_map\u5e76\u8fd4\u56de\u6307\u9488\r\n    string_string_map* col_map =\r\n        segment.construct&lt;string_string_map&gt;(\"tag_fvalue\")(std::less&lt;char_string&gt;(), alloc_inst);\r\n\r\n    col_map-&gt;insert(ss_map_value);\r\n\r\n    \/\/\u6784\u5efa\u81ea\u5b9a\u4e49map&lt;string, map&lt;string, string&gt;&gt;\u7684\u952e\u503c\u5bf9\r\n    string_string_string_value_type sss_map_value(key_key_string, *col_map);\r\n\r\n    table_map-&gt;insert(sss_map_value);\r\n\r\n    \/* ---\u8bfb\u53d6\u5171\u4eab\u5185\u5b58\uff0c\u5e76\u89e3\u6790map--- *\/\r\n    managed_shared_memory segment1(open_or_create, \"MySharedMemory\", 102400);\r\n\r\n    string_string_string_map* table_map2 =\r\n        segment1.find&lt;string_string_string_map&gt;(\"table_map\").first;\r\n\r\n    \/\/\u521b\u5efa\u5bf9\u5e94\u8fed\u4ee3\u5668\r\n    string_string_string_map::iterator sss_ite;\r\n    string_string_map::iterator ss_ite;\r\n\r\n    char_string key_name1(\"tag\", alloc_inst);\r\n    char_string key_name2(\"fvalue\", alloc_inst);\r\n\r\n    \/\/\u4ecemap\u4e2d\u904d\u5386\u6570\u636e\uff0c\u4e0estl\u7684map\u4e00\u81f4\r\n    sss_ite = table_map2-&gt;find(key_name1);\r\n\r\n    if (sss_ite != table_map2-&gt;end()) {\r\n        cout &lt;&lt; sss_ite-&gt;first &lt;&lt; endl;\r\n        ss_ite = sss_ite-&gt;second.find(key_name2);\r\n        cout &lt;&lt; ss_ite-&gt;second &lt;&lt; endl;\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#include &lt;boost\/i<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-4378","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4378"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4378\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}