{"id":1420,"date":"2018-06-20T19:30:01","date_gmt":"2018-06-20T11:30:01","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=1420"},"modified":"2018-06-20T19:30:01","modified_gmt":"2018-06-20T11:30:01","slug":"%e6%8a%8astl%e5%ae%b9%e5%99%a8%e6%94%be%e5%85%a5%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/1420","title":{"rendered":"\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58"},"content":{"rendered":"<div class=\"post\">\n<div class=\"postTitle\"><a id=\"viewpost1_TitleUrl\" href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html\">[\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58<\/a><\/div>\n<div class=\"postText\">\n<p><em>\u6628\u5929\u5728\u4e0a\u7bc7blog\u91cc\u63cf\u5199\u4e86\u5982\u4f55\u628aSTL\u5bb9\u5668\u653e\u5230\u5171\u4eab\u5185\u5b58\u91cc\u53bb\uff0c\u4e0d\u8fc7\u7531\u4e8e\u597d\u4e45\u4e0d\u5199blog\uff0c\u53d1\u89c9\u8bcd\u6c47\u7ec4\u7ec7\u80fd\u529b\u5dee\u4e86\u5f88\u591a\uff0c\u4e0d\u5c11\u60f3\u5199\u7684\u4e1c\u897f\u5199\u7684\u5f88\u96f6\u6563\uff0c\u4eca\u5929\u521a\u597d\u7ffb\u770b\u81ea\u5df1\u7684\u4e66\u7b7e\uff0c\u770b\u5230\u4e00\u7bc7\u633a\u8001\u7684\u6587\u7ae0\uff0c\u4e0d\u8fc7\u4ece\u5171\u4eab\u5185\u5b58\u5230STL\u5bb9\u5668\u8bb2\u8ff0\u5f97\u86ee\u5168\u9762\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5b66\u4e60\u7684\u5b9e\u4f8b\uff0c\u6240\u4ee5\u987a\u4fbf\u7ffb\u8bd1\u8fc7\u6765\uff0c\u5e76\u9644\u4e0a<a class=\"liexternal previewlink\" href=\"http:\/\/www.ddj.com\/cpp\/184401639\" target=\"_blank\" rel=\"noopener\"><u><span style=\"color: #810081;\">\u539f\u6587\u5730\u5740<\/span><\/u><\/a>\u3002<\/em><\/p>\n<p>\u5171\u4eab\u5185\u5b58(shm)\u662f\u5f53\u524d\u4e3b\u6d41UNIX\u7cfb\u7edf\u4e2d\u7684\u4e00\u79cdIPC\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u628a\u540c\u4e00\u5757\u7269\u7406\u5185\u5b58\u6bb5(segment)\u6620\u5c04(map)\u5230\u5b83\u4eec\u7684\u5730\u5740\u7a7a\u95f4\u4e2d\u53bb\u3002\u65e2\u7136\u5185\u5b58\u6bb5\u5bf9\u4e8e\u5404\u81ea\u9644\u7740(attach)\u7684\u8fdb\u7a0b\u662f\u5171\u4eab\u7684\uff0c\u8fd9\u4e9b\u8fdb\u7a0b\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u901a\u8fc7\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u4e0a\u7684\u5171\u6709\u6570\u636e\u8fdb\u884c\u901a\u4fe1\u3002\u56e0\u6b64\uff0c\u987e\u540d\u601d\u4e49\uff0c\u5171\u4eab\u5185\u5b58\u5c31\u662f\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u7684\u4e00\u7ec4\u5185\u5b58\u6bb5\u3002\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u9644\u7740\u5230\u4e00\u5757\u5171\u4eab\u5185\u5b58\u4e0a\u540e\uff0c\u5b83\u5f97\u5230\u4e00\u4e2a\u6307\u5411\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u7684\u6307\u9488\uff1b\u8be5\u8fdb\u7a0b\u53ef\u4ee5\u50cf\u4f7f\u7528\u5176\u4ed6\u5185\u5b58\u4e00\u6837\u4f7f\u7528\u8fd9\u5757\u5171\u4eab\u5185\u5b58\u3002\u5f53\u7136\uff0c\u7531\u4e8e\u8fd9\u5757\u5185\u5b58\u540c\u6837\u4f1a\u88ab\u5176\u4ed6\u8fdb\u7a0b\u8bbf\u95ee\u6216\u5199\u5165\uff0c\u6240\u4ee5\u5fc5\u987b\u8981\u6ce8\u610f\u8fdb\u7a0b\u540c\u6b65\u95ee\u9898\u3002<\/p>\n<p>\u53c2\u8003\u5982\u4e0b\u4ee3\u7801\uff0c\u8fd9\u662fUNIX\u7cfb\u7edf\u4e0a\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u7684\u4e00\u822c\u65b9\u6cd5\uff08\u6ce8\uff1a\u672c\u6587\u8c03\u7528\u7684\u662fPOSIX\u51fd\u6570\uff09\uff1a<br \/>\n<span id=\"more-299\"><\/span><\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2991\">\n<td id=\"p299code1\" class=\"code\">\n<pre class=\"cpp\">\/\/Get shared memory id\r\n            \/\/shared memory key\r\n            const key_t ipckey = 24568;\r\n            \/\/shared memory permission; can be\r\n            \/\/read and written by anybody\r\n            const int perm = 0666;\r\n            \/\/shared memory segment size\r\n            size_t shmSize = 4096;\r\n            \/\/Create shared memory if not\r\n            \/\/already created with specified\r\n            \/\/permission\r\n            int shmId = shmget\r\n            (ipckey,shmSize,IPC_CREAT|perm);\r\n            if (shmId ==-1) {\r\n            \/\/Error\r\n            }\r\n            \u00a0\r\n            \/\/Attach the shared memory segment\r\n            \u00a0\r\n            void* shmPtr = shmat(shmId,NULL,0);\r\n            \u00a0\r\n            struct commonData* dp =  (struct commonData*)shmPtr;\r\n            \u00a0\r\n            \/\/detach shared memory\r\n            shmdt(shmPtr);<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>\u5b58\u653e\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u6570\u636e\u7ed3\u6784<\/h2>\n<p>\u5f53\u4fdd\u5b58\u6570\u636e\u5230\u5171\u4eab\u5185\u5b58\u4e2d\u65f6\u9700\u8981\u7559\u610f\uff0c\u53c2\u8003\u5982\u4e0b\u7ed3\u6784\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2992\">\n<td id=\"p299code2\" class=\"code\">\n<pre class=\"cpp\">struct commonData {\r\n            int sharedInt;\r\n            float  sharedFloat;\r\n            char* name;\r\n            Struct CommonData* next;\r\n            };<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u8fdb\u7a0bA\u628a\u6570\u636e\u5199\u5165\u5171\u4eab\u5185\u5b58\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2993\">\n<td id=\"p299code3\" class=\"code\">\n<pre class=\"cpp\">\/\/Attach shared memory\r\n            struct commonData* dp =\r\n            (struct commonData*) shmat\r\n            (shmId,NULL,0);\r\n            \u00a0\r\n            dp-&gt;sharedInt = 5;\r\n            .\r\n            .\r\n            dp-&gt;name = new char [20];\r\n            strcpy(dp-&gt;name,\"My Name\");\r\n            \u00a0\r\n            dp-&gt;next = new struct commonData();<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u7a0d\u540e\uff0c\u8fdb\u7a0bB\u628a\u6570\u636e\u8bfb\u51fa\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2994\">\n<td id=\"p299code4\" class=\"code\">\n<pre class=\"cpp\">struct commonData* dp =\r\n            (struct commonData*) shmat\r\n            (shmId,NULL,0);\r\n            \u00a0\r\n            \/\/count = 5;\r\n            int count = dp-&gt;sharedInt;\r\n            \/\/problem\r\n            printf(\"name = [%s]\\n\",dp-&gt;name);\r\n            dp = dp-&gt;next;  \/\/problem<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u7ed3\u6784\u00a0<code>commonData<\/code>\u00a0\u7684\u6210\u5458\u00a0<code>name<\/code>\u00a0\u548c\u6307\u5411\u4e0b\u4e00\u4e2a\u7ed3\u6784\u7684\u00a0<code>next<\/code>\u00a0\u6240\u6307\u5411\u7684\u5185\u5b58\u5206\u522b\u4ece\u8fdb\u7a0bA\u7684\u5730\u5740\u7a7a\u95f4\u4e2d\u7684\u5806\u4e0a\u5206\u914d\uff0c\u663e\u7136 name \u548c next \u6307\u5411\u7684\u5185\u5b58\u4e5f\u53ea\u6709\u8fdb\u7a0bA\u53ef\u4ee5\u8bbf\u95ee\u3002\u5f53\u8fdb\u7a0bB\u8bbf\u95ee\u00a0<code>dp-&gt;name<\/code>\u00a0\u6216\u8005\u00a0<code>dp-&gt;next<\/code>\u00a0\u65f6\u5019\uff0c\u7531\u4e8e\u5b83\u5728\u8bbf\u95ee\u81ea\u5df1\u5730\u5740\u7a7a\u95f4\u4ee5\u5916\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u6240\u4ee5\u8fd9\u5c06\u662f\u975e\u6cd5\u64cd\u4f5c(memory violation)\uff0c\u5b83\u65e0\u6cd5\u6b63\u786e\u5f97\u5230\u00a0<code>name<\/code>\u548c\u00a0<code>next<\/code>\u00a0\u6240\u6307\u5411\u7684\u5185\u5b58\u3002\u56e0\u6b64\uff0c\u6240\u6709\u7684\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u6307\u9488\u5fc5\u987b\u540c\u6837\u6307\u5411\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u5730\u5740\u3002\uff08\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5305\u542b\u865a\u51fd\u6570\u7ee7\u627f\u7684C++\u7c7b\u5bf9\u8c61\u4e0d\u80fd\u653e\u5230\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u539f\u56e0\u2014\u2014\u8fd9\u662f\u53e6\u5916\u4e00\u4e2a\u8bdd\u9898\u3002\u6ce8\uff1a\u56e0\u4e3a\u865a\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u53ef\u80fd\u4f1a\u5728\u5176\u4ed6\u7684\u5185\u5b58\u7a7a\u95f4\u4e2d\uff09\u7531\u4e8e\u8fd9\u4e9b\u6761\u4ef6\u9650\u5236\uff0c\u653e\u5165\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u7ed3\u6784\u5e94\u8be5\u7b80\u5355\u7b80\u5355\u3002\uff08\u6ce8\uff1a\u6211\u89c9\u5f97\u6700\u597d\u907f\u514d\u4f7f\u7528\u6307\u9488\uff09<\/p>\n<h2>\u5171\u4eab\u5185\u5b58\u4e2d\u7684STL\u5bb9\u5668<\/h2>\n<p>\u60f3\u50cf\u4e00\u4e0b\u628aSTL\u5bb9\u5668\uff0c\u4f8b\u5982map, vector, list\u7b49\u7b49\uff0c\u653e\u5165\u5171\u4eab\u5185\u5b58\u4e2d\uff0cIPC\u4e00\u65e6\u6709\u4e86\u8fd9\u4e9b\u5f3a\u5927\u7684\u901a\u7528\u6570\u636e\u7ed3\u6784\u505a\u8f85\u52a9\uff0c\u65e0\u7591\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u80fd\u529b\u4e00\u4e0b\u5b50\u5f3a\u5927\u4e86\u5f88\u591a\u3002\u6211\u4eec\u6ca1\u5fc5\u8981\u518d\u4e3a\u5171\u4eab\u5185\u5b58\u8bbe\u8ba1\u5176\u4ed6\u989d\u5916\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53e6\u5916\uff0cSTL\u7684\u9ad8\u5ea6\u53ef\u6269\u5c55\u6027\u5c06\u4e3aIPC\u6240\u9a71\u4f7f\u3002STL\u5bb9\u5668\u88ab\u826f\u597d\u7684\u5c01\u88c5\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6709\u5b83\u4eec\u81ea\u5df1\u7684\u5185\u5b58\u7ba1\u7406\u65b9\u6848\u3002\u5f53\u4e00\u4e2a\u5143\u7d20\u88ab\u63d2\u5165\u5230\u4e00\u4e2aSTL\u5217\u8868(list)\u4e2d\u65f6\uff0c\u5217\u8868\u5bb9\u5668\u81ea\u52a8\u4e3a\u5176\u5206\u914d\u5185\u5b58\uff0c\u4fdd\u5b58\u6570\u636e\u3002\u8003\u8651\u5230\u8981\u5c06STL\u5bb9\u5668\u653e\u5230\u5171\u4eab\u5185\u5b58\u4e2d\uff0c\u800c\u5bb9\u5668\u5374\u81ea\u5df1\u5728\u5806\u4e0a\u5206\u914d\u5185\u5b58\u3002\u4e00\u4e2a\u6700\u7b28\u62d9\u7684\u529e\u6cd5\u662f\u5728\u5806\u4e0a\u6784\u9020STL\u5bb9\u5668\uff0c\u7136\u540e\u628a\u5bb9\u5668\u590d\u5236\u5230\u5171\u4eab\u5185\u5b58\uff0c\u5e76\u4e14\u786e\u4fdd\u6240\u6709\u5bb9\u5668\u7684\u5185\u90e8\u5206\u914d\u7684\u5185\u5b58\u6307\u5411\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u76f8\u5e94\u533a\u57df\uff0c\u8fd9\u57fa\u672c\u662f\u4e2a\u4e0d\u53ef\u80fd\u5b8c\u6210\u7684\u4efb\u52a1\u3002\u4f8b\u5982\u4e0b\u8fb9\u8fdb\u7a0bA\u6240\u505a\u7684\u4e8b\u60c5\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2995\">\n<td id=\"p299code5\" class=\"code\">\n<pre class=\"cpp\">\/\/Attach to shared memory\r\n            void* rp = (void*)shmat(shmId,NULL,0);\r\n            \/\/Construct the vector in shared\r\n            \/\/memory using placement new\r\n            vector&lt;int&gt;* vpInA = new(rp) vector&lt;int&gt;*;\r\n            \/\/The vector is allocating internal data\r\n            \/\/from the heap in process A's address\r\n            \/\/space to hold the integer value\r\n            (*vpInA)[0] = 22;<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u7136\u540e\u8fdb\u7a0bB\u5e0c\u671b\u4ece\u5171\u4eab\u5185\u5b58\u4e2d\u53d6\u51fa\u6570\u636e\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2996\">\n<td id=\"p299code6\" class=\"code\">\n<pre class=\"cpp\">vector&lt;int&gt;* vpInB =\r\n            (vector&lt;int&gt;*) shmat(shmId,NULL,0);\r\n            \u00a0\r\n            \/\/problem - the vector contains internal \r\n            \/\/pointers allocated in process A's address \r\n            \/\/space and are invalid here \r\n            int i = *(vpInB)[0];<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>\u91cd\u7528STL allocator<\/h2>\n<p>\u8fdb\u4e00\u6b65\u8003\u5bdfSTL\u5bb9\u5668\uff0c\u6211\u4eec\u53d1\u73b0\u5b83\u7684\u6a21\u677f\u5b9a\u4e49\u4e2d\u6709\u7b2c\u4e8c\u4e2a\u9ed8\u8ba4\u53c2\u6570\uff0c\u4e5f\u5c31\u662fallocator \u7c7b\uff0c\u8be5\u7c7b\u5b9e\u9645\u662f\u4e00\u4e2a\u5185\u5b58\u5206\u914d\u6a21\u578b\u3002\u9ed8\u8ba4\u7684allocator\u662f\u4ece\u5806\u4e0a\u5206\u914d\u5185\u5b58\uff08\u6ce8\uff1a\u8fd9\u5c31\u662fSTL\u5bb9\u5668\u7684\u9ed8\u8ba4\u8868\u73b0\uff0c\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u6539\u9020\u5b83\u4ece\u4e00\u4e2a\u7f51\u7edc\u6570\u636e\u5e93\u4e2d\u5206\u914d\u7a7a\u95f4\uff0c\u4fdd\u5b58\u6570\u636e\uff09\u3002\u4e0b\u8fb9\u662f vector \u7c7b\u7684\u4e00\u90e8\u5206\u5b9a\u4e49\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2997\">\n<td id=\"p299code7\" class=\"code\">\n<pre class=\"cpp\">template&lt;class T, class A = allocator&lt;T&gt; &gt;\r\n            class vector {\r\n            \/\/other stuff\r\n            };<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u8003\u8651\u5982\u4e0b\u58f0\u660e\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2998\">\n<td id=\"p299code8\" class=\"code\">\n<pre class=\"cpp\">\/\/User supplied allocator myAlloc\r\n            vector&lt;int,myAlloc&lt;int&gt; &gt; alocV;<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u5047\u8bbe\u00a0<code>myAlloc<\/code>\u00a0\u4ece\u5171\u4eab\u5185\u5b58\u4e0a\u5206\u914d\u5185\u5b58\uff0c\u5219\u00a0<code>alocV<\/code>\u00a0\u5c06\u5b8c\u5168\u5728\u5171\u4eab\u5185\u5b58\u4e0a\u88ab\u6784\u9020\uff0c\u6240\u4ee5\u8fdb\u7a0bA\u53ef\u4ee5\u5982\u4e0b\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p2999\">\n<td id=\"p299code9\" class=\"code\">\n<pre class=\"cpp\">\/\/Attach to shared memory\r\n            void* rp = (void*)shmat(shmId,NULL,0);\r\n            \/\/Construct the vector in shared memory\r\n            \/\/using placement new\r\n            vector&lt;int&gt;* vpInA =\r\n            new(rp) vector&lt;int,myAlloc&lt;int&gt; &gt;*;\r\n            \/\/The vector uses myAlloc&lt;int&gt; to allocate\r\n            \/\/memory for its internal data structure\r\n            \/\/from shared memory\r\n            (*v)[0] = 22;<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u8fdb\u7a0bB\u53ef\u4ee5\u5982\u4e0b\u8bfb\u51fa\u6570\u636e\uff1a<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left\"><a><u><span style=\"color: #0000ff;\">View Code<\/span><\/u><\/a>\u00a0CPP<\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p29910\">\n<td id=\"p299code10\" class=\"code\">\n<pre class=\"cpp\">vector&lt;int&gt;* vpInB =\r\n            (vector&lt;int,myAlloc&lt;int&gt; &gt;*) shmat\r\n            (shmId,NULL,0);\r\n            \u00a0\r\n            \/\/Okay since all of the vector is\r\n            \/\/in shared memory\r\n            int i = *(vpInB)[0];<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u6240\u6709\u9644\u7740\u5728\u5171\u4eab\u5185\u5b58\u4e0a\u7684\u8fdb\u7a0b\u90fd\u53ef\u4ee5\u5b89\u5168\u7684\u4f7f\u7528\u8be5vector\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8be5\u7c7b\u7684\u6240\u6709\u5185\u5b58\u90fd\u5728\u5171\u4eab\u5185\u5b58\u4e0a\u5206\u914d\uff0c\u540c\u65f6\u53ef\u4ee5\u88ab\u5176\u4ed6\u7684\u8fdb\u7a0b\u8bbf\u95ee\u3002\u53ea\u8981\u63d0\u4f9b\u4e00\u4e2a\u7528\u6237\u81ea\u5b9a\u4e49\u7684allocator\uff0c\u4efb\u4f55STL\u5bb9\u5668\u90fd\u53ef\u4ee5\u5b89\u5168\u7684\u653e\u7f6e\u5230\u5171\u4eab\u5185\u5b58\u4e0a\u3002<\/p>\n<h2>\u4e00\u4e2a\u57fa\u4e8e\u5171\u4eab\u5185\u5b58\u7684STL Allocator<\/h2>\n<p>\u6e05\u5355 shared_allocator.hh \u662f\u4e00\u4e2aSTL Allocator\u7684\u5b9e\u73b0\uff0c<code>SharedAllocator<\/code>\u00a0\u662f\u4e00\u4e2a\u6a21\u677f\u7c7b\u3002\u800c\u00a0<code>Pool<\/code>\u00a0\u7c7b\u5b8c\u6210\u5171\u4eab\u5185\u5b58\u7684\u5206\u914d\u4e0e\u56de\u6536\u3002<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left2\">Download\u00a0<a href=\"http:\/\/blog.erera.net\/wp-content\/plugins\/wp-codebox\/wp-codebox.php?p=299&amp;download=shared_allocator.hh\"><u><span style=\"color: #0000ff;\">shared_allocator.hh<\/span><\/u><\/a><\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p29911\">\n<td id=\"p299code11\" class=\"code\">\n<pre class=\"cpp\">template&lt;class T&gt;class SharedAllocator {\r\n            private:\r\n            Pool pool_;    \/\/ pool of elements of sizeof(T)\r\n            public:\r\n            typedef T value_type;\r\n            typedef unsigned int  size_type;\r\n            typedef ptrdiff_t difference_type;\r\n            typedef T* pointer;\r\n            typedef const T* const_pointer;\r\n            typedef T&amp; reference;\r\n            typedef const T&amp; const_reference;\r\n            pointer address(reference r) const { return &amp;r; }\r\n            const_pointer address(const_reference r) const {return &amp;r;}\r\n            SharedAllocator() throw():pool_(sizeof(T)) {}\r\n            template&lt;class U&gt; SharedAllocator\r\n            (const SharedAllocator&lt;U&gt;&amp; t) throw():\r\n            pool_(sizeof(T)) {}\r\n            ~SharedAllocator() throw() {};\r\n            \/\/ space for n Ts\r\n            pointer allocate(size_t n, const void* hint=0)\r\n            {\r\n            return(static_cast&lt;pointer&gt; (pool_.alloc(n)));\r\n            }\r\n            \/\/ deallocate n Ts, don't destroy\r\n            void deallocate(pointer p,size_type n)\r\n            {\r\n            pool_.free((void*)p,n);\r\n            return;\r\n            }\r\n            \/\/ initialize *p by val\r\n            void construct(pointer p, const T&amp; val) { new(p) T(val); }\r\n            \/\/ destroy *p but don't deallocate\r\n            void destroy(pointer p) { p-&gt;~T(); }\r\n            size_type max_size() const throw()\r\n            {\r\n            pool_.maxSize();\r\n            }\r\n            template&lt;class U&gt;\r\n            \/\/ in effect: typedef SharedAllocator&lt;U&gt; other\r\n            struct rebind { typedef SharedAllocator&lt;U&gt; other; };\r\n            };\r\n            \u00a0\r\n            template&lt;class T&gt;bool operator==(const SharedAllocator&lt;T&gt;&amp; a,\r\n            const SharedAllocator&lt;T&gt;&amp; b) throw()\r\n            {\r\n            return(a.pool_ == b.pool_);\r\n            }\r\n            template&lt;class T&gt;bool operator!=(const SharedAllocator&lt;T&gt;&amp; a,\r\n            const SharedAllocator&lt;T&gt;&amp; b) throw()\r\n            {\r\n            return(!(a.pool_ == b.pool_));\r\n            }<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u6e05\u5355pool.hh\u662f\u00a0<code>Pool<\/code>\u00a0\u7c7b\u5b9a\u4e49\uff0c\u5176\u4e2d\u9759\u6001\u6210\u5458<code>shm_<\/code>\u00a0\u662f\u7c7b\u578b\u00a0<code>shmPool<\/code>\uff0c\u4fdd\u8bc1\u6bcf\u4e2a\u8fdb\u7a0b\u53ea\u6709\u552f\u4e00\u7684\u4e00\u4e2a<code>shmPool<\/code>\u00a0\u5b9e\u4f8b\u3002<code>shmPool<\/code>\u00a0ctor \u521b\u5efa\u5e76\u9644\u7740\u6240\u9700\u5927\u5c0f\u7684\u5185\u5b58\u5230\u5171\u4eab\u5185\u5b58\u4e0a\u3002\u5171\u4eab\u5185\u5b58\u7684\u53c2\u6570\uff0c\u6bd4\u5982 \u952e\u503c\u3001\u6bb5\u6570\u76ee\u3001\u6bb5\u5927\u5c0f\uff0c\u90fd\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u4f20\u9012\u7ed9\u00a0<code>shmPool<\/code>ctor\u3002\u6210\u5458\u00a0<code>segs_<\/code>\u00a0\u662f\u5171\u4eab\u6bb5\u7684\u6570\u76ee\uff0c<code>segSize_<\/code>\u662f\u6bcf\u4e2a\u5171\u4eab\u6bb5\u7684\u5927\u5c0f\uff0c\u6210\u5458<code>path_<\/code>\u548c<code>key_<\/code>\u00a0\u7528\u6765\u521b\u5efa\u552f\u4e00\u7684\u00a0<code>ipckey<\/code>\u3002<code>shmPool<\/code>\u00a0\u4e3a\u6bcf\u4e2a\u5171\u4eab\u6bb5\u521b\u5efa\u4e00\u4e2a\u4fe1\u53f7\u91cf(semaphore)\u7528\u4e8e\u540c\u6b65\u3002<code>shmPool<\/code>\u00a0\u8fd8\u5728\u4e3a\u6bcf\u4e2a\u5171\u4eab\u6bb5\u6784\u9020\u4e86\u4e00\u4e2a\u00a0<code>Chunk<\/code>\u00a0\u7c7b\uff0c\u4e00\u4e2a\u00a0<code>Chunk<\/code>\u4ee3\u8868\u4e00\u4e2a\u5171\u4eab\u6bb5\u3002\u6bcf\u4e2a\u5171\u4eab\u6bb5\u7684\u6807\u8bc6\u662f<code>shmId_<\/code>\uff0c \u4fe1\u53f7\u91cf\u00a0<code>semId_<\/code>\u63a7\u5236\u8be5\u6bb5\u7684\u8bbf\u95ee\u8bb8\u53ef\uff0c\u4e00\u4e2a\u6307\u5411\u00a0<code>Link<\/code>\u00a0\u7ed3\u6784\u7684\u6307\u9488\u8868\u660e\u00a0<code>Chunk<\/code>\u7c7b\u7684\u5269\u4f59\u5217\u8868\u3002<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left2\">Download\u00a0<a href=\"http:\/\/blog.erera.net\/wp-content\/plugins\/wp-codebox\/wp-codebox.php?p=299&amp;download=pool.hh\"><u><span style=\"color: #0000ff;\">pool.hh<\/span><\/u><\/a><\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p29912\">\n<td id=\"p299code12\" class=\"code\">\n<pre class=\"cpp\">class Pool {\r\n            private:\r\n            class shmPool {\r\n            private:\r\n            struct Container {\r\n            containerMap* cont;\r\n            };\r\n            class Chunk {\r\n            public:\r\n            Chunk()\r\n            Chunk(Chunk&amp;);\r\n            ~Chunk() {}\r\n            void* alloc(size_t size);\r\n            void free (void* p,size_t size);\r\n            private:\r\n            int shmId_;\r\n            int semId_;\r\n            int lock_()\r\n            };\r\n            int key_;\r\n            char* path_;\r\n            Chunk** chunks_;\r\n            size_t segs_;\r\n            size_t segSize_;\r\n            Container* contPtr_;\r\n            int contSemId_;\r\n            public:\r\n            shmPool();\r\n            ~shmPool();\r\n            size_t maxSize();\r\n            void* alloc(size_t size);\r\n            void free(void* p, size_t size);\r\n            int shmPool::lockContainer()\r\n            int unLockContainer()\r\n            containerMap* getContainer()\r\n            void shmPool::setContainer(containerMap* container)\r\n            };\r\n            \u00a0\r\n            private:\r\n            static shmPool shm_;\r\n            size_t elemSize_;\r\n            public:\r\n            Pool(size_t elemSize);\r\n            ~Pool() {}\r\n            size_t maxSize();\r\n            void* alloc(size_t size);\r\n            void free(void* p, size_t size);\r\n            int lockContainer();\r\n            int unLockContainer();\r\n            containerMap* getContainer();\r\n            void setContainer(containerMap* container);\r\n            };\r\n            inline bool operator==(const Pool&amp; a,const Pool&amp; b)\r\n            {\r\n            return(a.compare(b));\r\n            }<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58<\/h2>\n<p>\u5047\u8bbe\u8fdb\u7a0bA\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u653e\u5165\u4e86\u6570\u4e2a\u5bb9\u5668\uff0c\u8fdb\u7a0bB\u5982\u4f55\u627e\u5230\u8fd9\u4e9b\u5bb9\u5668\u5462\uff1f\u4e00\u4e2a\u65b9\u6cd5\u5c31\u662f\u8fdb\u7a0bA\u628a\u5bb9\u5668\u653e\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u786e\u5b9a\u5730\u5740\u4e0a\uff08fixed offsets\uff09\uff0c\u5219\u8fdb\u7a0bB\u53ef\u4ee5\u4ece\u8be5\u5df2\u77e5\u5730\u5740\u4e0a\u83b7\u53d6\u5bb9\u5668\u3002\u53e6\u5916\u4e00\u4e2a\u6539\u8fdb\u70b9\u7684\u529e\u6cd5\u662f\uff0c\u8fdb\u7a0bA\u5148\u5728\u5171\u4eab\u5185\u5b58\u67d0\u5757\u786e\u5b9a\u5730\u5740\u4e0a\u653e\u7f6e\u4e00\u4e2amap\u5bb9\u5668\uff0c\u7136\u540e\u8fdb\u7a0bA\u518d\u521b\u5efa\u5176\u4ed6\u5bb9\u5668\uff0c\u7136\u540e\u7ed9\u5176\u53d6\u4e2a\u540d\u5b57\u548c\u5730\u5740\u4e00\u5e76\u4fdd\u5b58\u5230\u8fd9\u4e2amap\u5bb9\u5668\u91cc\u3002\u8fdb\u7a0bB\u77e5\u9053\u5982\u4f55\u83b7\u53d6\u8be5\u4fdd\u5b58\u4e86\u5730\u5740\u6620\u5c04\u7684map\u5bb9\u5668\uff0c\u7136\u540e\u540c\u6837\u518d\u6839\u636e\u540d\u5b57\u53d6\u5f97\u5176\u4ed6\u5bb9\u5668\u7684\u5730\u5740\u3002\u6e05\u5355container_factory.hh\u662f\u4e00\u4e2a\u5bb9\u5668\u5de5\u5382\u7c7b\u3002\u7c7b<code>Pool<\/code>\u7684\u65b9\u6cd5<code>setContainer<\/code>\u628amap\u5bb9\u5668\u653e\u7f6e\u5728\u4e00\u4e2a\u5df2\u77e5\u5730\u5740\u4e0a\uff0c\u65b9\u6cd5<code>getContainer<\/code>\u53ef\u4ee5\u91cd\u65b0\u83b7\u53d6\u8fd9\u4e2amap\u3002\u8be5\u5de5\u5382\u7684\u65b9\u6cd5\u7528\u6765\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u521b\u5efa\u3001\u83b7\u53d6\u548c\u5220\u9664\u5bb9\u5668\u3002\u5f53\u7136\uff0c\u4f20\u9012\u7ed9\u5bb9\u5668\u5de5\u5382\u7684\u5bb9\u5668\u9700\u8981\u4ee5<code>SharedAllocator<\/code>\u4f5c\u4e3aallocator\u3002<\/p>\n<div class=\"wp_codebox_msgheader\"><span class=\"right\"><sup><a title=\"WP-CodeBox HowTo?\" href=\"http:\/\/www.ericbess.com\/ericblog\/2008\/03\/03\/wp-codebox\/#examples\" target=\"_blank\" rel=\"noopener\"><u>?<\/u><\/a><\/sup><a><u><span style=\"color: #0000ff;\">[Copy to clipboard]<\/span><\/u><\/a><\/span><span class=\"left2\">Download\u00a0<a href=\"http:\/\/blog.erera.net\/wp-content\/plugins\/wp-codebox\/wp-codebox.php?p=299&amp;download=container_factory.hh\"><u><span style=\"color: #0000ff;\">container_factory.hh<\/span><\/u><\/a><\/span><\/p>\n<div class=\"codebox_clear\"><\/div>\n<\/div>\n<div class=\"wp_codebox\">\n<table>\n<tbody>\n<tr id=\"p29913\">\n<td id=\"p299code13\" class=\"code\">\n<pre class=\"cpp\">struct keyComp {\r\n            bool operator()(const char* key1,const char* key2)\r\n            {\r\n            return(strcmp(key1,key2) &lt; 0);\r\n            }\r\n            };\r\n            class containerMap: public map&lt;char*,void*,keyComp,SharedAllocator&lt;char* &gt; &gt; {};\r\n            class containerFactory {\r\n            public:\r\n            containerFactory():pool_(sizeof(containerMap)){}\r\n            ~containerFactory() {}\r\n            template&lt;class Container&gt; Container* createContainer\r\n            (char* key,Container* c=NULL);\r\n            template&lt;class Container&gt; Container* getContainer\r\n            (char* key,Container* c=NULL);\r\n            template&lt;class Container&gt; int removeContainer\r\n            (char* key,Container* c=NULL);\r\n            private:\r\n            Pool pool_;\r\n            int lock_();\r\n            int unlock_();\r\n            };<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u672c\u6587\u63cf\u8ff0\u7684\u65b9\u6848\u53ef\u4ee5\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u521b\u5efaSTL\u5bb9\u5668\uff0c\u5176\u4e2d\u7684\u4e00\u4e2a\u7f3a\u9677\u662f\uff0c\u5728\u5206\u914d\u5171\u4eab\u5185\u5b58\u4e4b\u524d\uff0c\u5e94\u8be5\u4fdd\u8bc1\u5171\u4eab\u5185\u5b58\u7684\u603b\u5927\u5c0f(<code>segs_* segSize_<\/code>)\u5927\u4e8e\u4f60\u8981\u4fdd\u5b58STL\u5bb9\u5668\u7684\u6700\u5927\u957f\u5ea6\uff0c\u56e0\u4e3a\u4e00\u65e6\u7c7b<code>Pool<\/code>\u00a0\u8d85\u51fa\u4e86\u5171\u4eab\u5185\u5b58\u7684\uff0c\u8be5\u7c7b\u65e0\u6cd5\u518d\u5206\u914d\u65b0\u7684\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>\u5b8c\u6574\u7684\u6e90\u4ee3\u7801\u53ef\u4ee5\u4ece\u8fd9\u91cc\u4e0b\u8f7d\uff1awww.cuj.com\/code<\/p>\n<h2>\u53c2\u8003\u6587\u732e<\/h2>\n<ul>\n<li>Bjarne Stroustrup. The C++ Programming Language, Third Edition (Addison-Wesley, 1997).<\/li>\n<li>Matthew H. Austern. Generic Programming and the STL: Using and<br \/>\nExtending the C++ Standard Template Library (Addison-Wesley, 1999).<\/li>\n<\/ul>\n<h2>\u5173\u4e8e\u4f5c\u8005<\/h2>\n<p>Grum Ketema has Masters degrees in Electrical Engineering and Computer Science. With 17 years of experience in software development, he has been using C since 1985, C++ since 1988, and Java since 1997. He has worked at AT&amp;T Bell Labs, TASC, Massachusetts Institute of Technology, SWIFT, BEA Systems, and Northrop.<\/p>\n<\/div>\n<div class=\"postfoot\">posted on 2010-07-24 21:14\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/\">doing5552<\/a>\u00a0\u9605\u8bfb(6642)\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#Post\">\u8bc4\u8bba(6)<\/a>\u00a0\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/admin\/EditPosts.aspx?postid=121197\">\u7f16\u8f91<\/a>\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/AddToFavorite.aspx?id=121197\">\u6536\u85cf<\/a>\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/services\/trackbacks\/121197.aspx\">\u5f15\u7528<\/a><\/div>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.cppblog.com\/doing5552\/aggbug\/121197.html?webview=1\" width=\"1\" height=\"1\" \/><\/p>\n<div id=\"AjaxHolder_UpdatePanel1\"><a name=\"pagedcomment\"><\/a><a name=\"feedback\"><\/a><\/p>\n<div id=\"comments\">\n<h3>Feedback<\/h3>\n<div class=\"post\">\n<div class=\"postTitle\"><a title=\"permalink: re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58\" href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#166887\">#<\/a>\u00a0<a name=\"166887\"><\/a>re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58\u00a02012-03-01 16:42\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl00_NameLink\" target=\"_blank\"><\/a>\u5f27\u5149<\/div>\n<div class=\"postText\">\u535a\u4e3b,\u4f60\u597d!,\u9996\u5148\u8c22\u8c22\u4f60\u7ffb\u8bd1\u5e76\u8f6c\u8f7d\u8fd9\u7bc7\u6587\u7ae0,\u672c\u4eba\u53d7\u76ca\u532a\u6d45\u3002\u5bf9\u4e8e\u535a\u6587\u4e2d\u6709\u4e00\u6bb5\u5185\u5bb9\u4e0d\u7406\u89e3\uff0c\u4e0d\u77e5\u9053\u535a\u4e3b\u662f\u5426\u80fd\u89e3\u91ca\u6216\u8005\u6709\u5b9e\u9645\u7684\u4f8b\u5b50\u4ee3\u7801\uff1f<br \/>\n\u4ee5\u4e0b\u90e8\u5206\u4e3a\u672c\u4eba\u7591\u60d1\u7684\u5730\u65b9\uff1a<br \/>\n\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58<br \/>\n\u5047\u8bbe\u8fdb\u7a0bA\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u653e\u5165\u4e86\u6570\u4e2a\u5bb9\u5668\uff0c\u8fdb\u7a0bB\u5982\u4f55\u627e\u5230\u8fd9\u4e9b\u5bb9\u5668\u5462\uff1f\u4e00\u4e2a\u65b9\u6cd5\u5c31\u662f\u8fdb\u7a0bA\u628a\u5bb9\u5668\u653e\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u7684\u786e\u5b9a\u5730\u5740\u4e0a\uff08fixed offsets\uff09\uff0c\u5219\u8fdb\u7a0bB\u53ef\u4ee5\u4ece\u8be5\u5df2\u77e5\u5730\u5740\u4e0a\u83b7\u53d6\u5bb9\u5668\u3002\u53e6\u5916\u4e00\u4e2a\u6539\u8fdb\u70b9\u7684\u529e\u6cd5\u662f\uff0c\u8fdb\u7a0bA\u5148\u5728\u5171\u4eab\u5185\u5b58\u67d0\u5757\u786e\u5b9a\u5730\u5740\u4e0a\u653e\u7f6e\u4e00\u4e2amap\u5bb9\u5668\uff0c\u7136\u540e\u8fdb\u7a0bA\u518d\u521b\u5efa\u5176\u4ed6\u5bb9\u5668\uff0c\u7136\u540e\u7ed9\u5176\u53d6\u4e2a\u540d\u5b57\u548c\u5730\u5740\u4e00\u5e76\u4fdd\u5b58\u5230\u8fd9\u4e2amap\u5bb9\u5668\u91cc\u3002\u8fdb\u7a0bB\u77e5\u9053\u5982\u4f55\u83b7\u53d6\u8be5\u4fdd\u5b58\u4e86\u5730\u5740\u6620\u5c04\u7684map\u5bb9\u5668\uff0c\u7136\u540e\u540c\u6837\u518d\u6839\u636e\u540d\u5b57\u53d6\u5f97\u5176\u4ed6\u5bb9\u5668\u7684\u5730\u5740\u3002\u6e05\u5355container_factory.hh\u662f\u4e00\u4e2a\u5bb9\u5668\u5de5\u5382\u7c7b\u3002\u7c7bPool\u7684\u65b9\u6cd5setContainer\u628amap\u5bb9\u5668\u653e\u7f6e\u5728\u4e00\u4e2a\u5df2\u77e5\u5730\u5740\u4e0a\uff0c\u65b9\u6cd5getContainer\u53ef\u4ee5\u91cd\u65b0\u83b7\u53d6\u8fd9\u4e2amap\u3002\u8be5\u5de5\u5382\u7684\u65b9\u6cd5\u7528\u6765\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u521b\u5efa\u3001\u83b7\u53d6\u548c\u5220\u9664\u5bb9\u5668\u3002\u5f53\u7136\uff0c\u4f20\u9012\u7ed9\u5bb9\u5668\u5de5\u5382\u7684\u5bb9\u5668\u9700\u8981\u4ee5SharedAllocator\u4f5c\u4e3aallocator\u3002<\/p>\n<p>\u672c\u4eba\u7684\u8054\u7cfb\u65b9\u5f0f\uff1a<br \/>\nmsn:duoduono2002@hotmail.com<br \/>\nqq:93320694<br \/>\nmail:hewg@szkingdom.com<br \/>\n\u4ee5\u4e0a\u4efb\u4f55\u65b9\u5f0f\u5747\u80fd\u8054\u7cfb\u5230\u672c\u4eba\uff0c\u6b22\u8fce\u535a\u4e3b\u4e0e\u5176\u4ed6\u770b\u5ba2\u6765\u5e2e\u5fd9\u89e3\u51b3\u6b64\u7591\u95ee\uff0c\u8c22\u8c22\uff01\uff01\uff01\u00a0\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#post\">\u56de\u590d<\/a>\u00a0\u00a0<a title=\"\u67e5\u770b\u8be5\u4f5c\u8005\u53d1\u8868\u8fc7\u7684\u8bc4\u8bba\" href=\"http:\/\/www.cppblog.com\/comment?author=%e5%bc%a7%e5%85%89\" target=\"_blank\" rel=\"noopener\">\u66f4\u591a\u8bc4\u8bba<\/a><br \/>\n<a id=\"AjaxHolder_Comments_CommentList_ctl00_DeleteLink\"><\/a>\u00a0\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl00_EditLink\"><\/a><\/div>\n<\/div>\n<div class=\"post\">\n<div class=\"postTitle\"><a title=\"permalink: re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58[\u672a\u767b\u5f55]\" href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#206770\">#<\/a>\u00a0<a name=\"206770\"><\/a>re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58[\u672a\u767b\u5f55]\u00a02014-04-30 10:35\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl01_NameLink\" target=\"_blank\"><\/a>none<\/div>\n<div class=\"postText\">\u8fd9\u7bc7\u6587\u7ae0\u6700\u5927\u7684\u95ee\u9898\u5c31\u5728\u4e8e\u5b83\u5047\u5b9a\u6bcf\u4e2a\u8fdb\u7a0battach\u4e0a\u8fd9\u4e2a\u5171\u4eab\u5185\u5b58\u65f6\u5f97\u5230\u7684\u5730\u5740\u504f\u79fb\u91cf\u662f\u76f8\u540c\u7684\u3002\u4f46\u5b9e\u9645\u4e0a\u5f80\u5f80\u662f\u4e0d\u540c\u7684\uff0c\u6240\u4ee5\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7ed9\u51fa\u7684\u65b9\u6cd5\u662f\u6839\u672c\u4e0d\u80fd\u6b63\u5e38\u4f7f\u7528\u7684\u3002<\/p>\n<p>\u5f53\u4e24\u4e2a\u8fdb\u7a0b\u4e2d\u540c\u4e00\u5757\u5171\u4eab\u5185\u5b58\u6620\u5c04\u7684\u5730\u5740\u504f\u79fb\u91cf\u4e0d\u540c\u7684\u65f6\u5019\uff0c\u4efb\u4f55\u653e\u5165\u5171\u4eab\u5185\u5b58\u7684\u6307\u9488\u90fd\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528\u3002\u50cfSTL\u8fd9\u79cd\u590d\u6742\u7684\u5bb9\u5668\u5e93\uff0c\u91cc\u9762\u5927\u91cf\u7528\u5230\u4e86\u6307\u9488\uff0c\u5982\u679c\u4e0d\u505a\u504f\u79fb\u91cf\u8c03\u6574\u7684\u8bdd\u51e0\u4e4e\u53ef\u4ee5\u80af\u5b9a\u4f1a\u51fa\u73b0\u4e25\u91cd\u95ee\u9898\u3002\u00a0\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#post\">\u56de\u590d<\/a>\u00a0\u00a0<a title=\"\u67e5\u770b\u8be5\u4f5c\u8005\u53d1\u8868\u8fc7\u7684\u8bc4\u8bba\" href=\"http:\/\/www.cppblog.com\/comment?author=none\" target=\"_blank\" rel=\"noopener\">\u66f4\u591a\u8bc4\u8bba<\/a><br \/>\n<a id=\"AjaxHolder_Comments_CommentList_ctl01_DeleteLink\"><\/a>\u00a0\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl01_EditLink\"><\/a><\/div>\n<\/div>\n<div class=\"post\">\n<div class=\"postTitle\"><a title=\"permalink: re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58\" href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#207916\">#<\/a>\u00a0<a name=\"207916\"><\/a>re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58\u00a02014-08-05 19:33\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl02_NameLink\" target=\"_blank\"><\/a>zzyoucan<\/div>\n<div class=\"postText\">\u4eca\u5929\u4e5f\u5728\u770bvector\u600e\u4e48\u5b58\u5165\u5171\u4eab\u5185\u5b58\uff0c\u4e0d\u8fc7boost\u53ef\u4ee5\uff0c\u8fd9\u4e2a\u53ea\u662f\u5b58\u5165\u5355\u4e2avector\u8981\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\u5c31\u4e0d\u592a\u597d\u5f04\u4e86\uff0c\u8fd8\u662f\u628avector\u6362\u6210\u6570\u7ec4\u5427\u3002\u00a0\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#post\">\u56de\u590d<\/a>\u00a0\u00a0<a title=\"\u67e5\u770b\u8be5\u4f5c\u8005\u53d1\u8868\u8fc7\u7684\u8bc4\u8bba\" href=\"http:\/\/www.cppblog.com\/comment?author=zzyoucan\" target=\"_blank\" rel=\"noopener\">\u66f4\u591a\u8bc4\u8bba<\/a><br \/>\n<a id=\"AjaxHolder_Comments_CommentList_ctl02_DeleteLink\"><\/a>\u00a0\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl02_EditLink\"><\/a><\/div>\n<\/div>\n<div class=\"post\">\n<div class=\"postTitle\"><a title=\"permalink: re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58[\u672a\u767b\u5f55]\" href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#207920\">#<\/a>\u00a0<a name=\"207920\"><\/a>re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58[\u672a\u767b\u5f55]\u00a02014-08-06 14:39\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl03_NameLink\" target=\"_blank\"><\/a>a<\/div>\n<div class=\"postText\">\u6b6a\u95e8\u90aa\u9053\uff0c\u6b63\u9014\u662f\u8fdb\u884c\u5e8f\u5217\u5316\u64cd\u4f5c\u00a0\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#post\">\u56de\u590d<\/a>\u00a0\u00a0<a title=\"\u67e5\u770b\u8be5\u4f5c\u8005\u53d1\u8868\u8fc7\u7684\u8bc4\u8bba\" href=\"http:\/\/www.cppblog.com\/comment?author=a\" target=\"_blank\" rel=\"noopener\">\u66f4\u591a\u8bc4\u8bba<\/a><br \/>\n<a id=\"AjaxHolder_Comments_CommentList_ctl03_DeleteLink\"><\/a>\u00a0\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl03_EditLink\"><\/a><\/div>\n<\/div>\n<div class=\"post\">\n<div class=\"postTitle\"><a title=\"permalink: re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58\" href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#212657\">#<\/a>\u00a0<a name=\"212657\"><\/a>re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58\u00a02016-01-15 11:50\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl04_NameLink\" target=\"_blank\"><\/a>ABC<\/div>\n<div class=\"postText\">@none<br \/>\n\u5982\u679c\u662f\u540c\u6837\u7684\u6620\u5c04 \u504f\u79fb\u600e\u4e48\u53ef\u80fd\u662f\u4e0d\u540c\u7684\u5462\uff1f\u5982\u679c \u6309\u4f60\u8fd9\u79cd\u8bf4\u6cd5 \u5171\u4eab\u5185\u5b58\u8fd8\u80fd\u7528\uff1f \u4f60\u8981\u5982\u4f55\u624d\u80fd\u7b97\u51fa\u91cc\u9762\u7684\u6570\u636e\uff1f \u4e0d\u8bf4\u653emap \u8fd9\u79cd\u4e86 \u4f60\u653e\u5b57\u7b26\u4e32 \u653e struct \u90fd\u6ca1\u6cd5\u7528\u3002 \u522b\u8bef\u4eba \u597d\u4e0d~\uff01\u00a0\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#post\">\u56de\u590d<\/a>\u00a0\u00a0<a title=\"\u67e5\u770b\u8be5\u4f5c\u8005\u53d1\u8868\u8fc7\u7684\u8bc4\u8bba\" href=\"http:\/\/www.cppblog.com\/comment?author=ABC\" target=\"_blank\" rel=\"noopener\">\u66f4\u591a\u8bc4\u8bba<\/a><br \/>\n<a id=\"AjaxHolder_Comments_CommentList_ctl04_DeleteLink\"><\/a>\u00a0\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl04_EditLink\"><\/a><\/div>\n<\/div>\n<div class=\"post\">\n<div class=\"postTitle\"><a title=\"permalink: re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58\" href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#213714\">#<\/a>\u00a0<a name=\"213714\"><\/a>re: [\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58<a name=\"Post\"><\/a>\u00a02016-06-14 22:56\u00a0<a id=\"AjaxHolder_Comments_CommentList_ctl05_NameLink\" target=\"_blank\"><\/a>DDD<\/div>\n<div class=\"postText\">@ABC<br \/>\n\u4eba\u5bb6\u662f\u8bf4\u7684\u6620\u5c04\u4e4b\u540e\u7684\u57fa\u5730\u5740\u5427\uff0c\u5171\u4eab\u5185\u5b58\u5185\u7684 offset \u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u5728\u865a\u62df\u5185\u5b58\u4e2d\u7684\u5730\u5740\u5f88\u53ef\u80fd\u662f\u4e0d\u4e00\u6837\u7684\u00a0\u00a0<a href=\"http:\/\/www.cppblog.com\/doing5552\/archive\/2010\/07\/24\/121197.html#post\">\u56de\u590d<\/a>\u00a0\u00a0<a title=\"\u67e5\u770b\u8be5\u4f5c\u8005\u53d1\u8868\u8fc7\u7684\u8bc4\u8bba\" href=\"http:\/\/www.cppblog.com\/comment?author=DDD\" target=\"_blank\" rel=\"noopener\">\u66f4\u591a\u8bc4\u8bba<\/a><br \/>\n<a id=\"AjaxHolder_Comments_CommentList_ctl05_DeleteLink\"><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>[\u7ffb\u8bd1]\u628aSTL\u5bb9\u5668\u653e\u5165\u5171\u4eab\u5185\u5b58 \u6628\u5929\u5728<\/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":[118],"class_list":["post-1420","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1420","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=1420"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/1420\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=1420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=1420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=1420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}