{"id":483,"date":"2020-07-26T21:26:24","date_gmt":"2020-07-26T13:26:24","guid":{"rendered":"https:\/\/www.notown.club\/?p=483"},"modified":"2023-03-08T21:20:55","modified_gmt":"2023-03-08T13:20:55","slug":"c-%e5%af%b9%e8%b1%a1%e7%a7%bb%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=483","title":{"rendered":"C++ \u5bf9\u8c61\u79fb\u52a8"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>\u4e00\u3001\u53f3\u503c\u5f15\u7528<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u5e38\u89c4\u7684\u5f15\u7528\u4e5f\u53eb\u5de6\u503c\u5f15\u7528\uff0c\u6211\u4eec\u4e0d\u80fd\u5c06\u5de6\u503c\u5f15\u7528\u7ed1\u5b9a\u5230\u8981\u6c42\u8f6c\u6362\u7684\u8868\u8fbe\u5f0f\u3001\u5b57\u9762\u5e38\u91cf\u6216\u662f\u8fd4\u56de\u53f3\u503c\u7684\u8868\u8fbe\u5f0f\u3002 \u800c\u6240\u8c13\u53f3\u503c\u5f15\u7528\u5c31\u662f\u5fc5\u987b\u7ed1\u5b9a\u5230\u53f3\u503c\u7684\u5f15\u7528\uff0c\u4ed6\u6709\u7740\u5b8c\u5168\u76f8\u53cd\u7684\u7ed1\u5b9a\u7279\u6027\uff1a\u6211\u4eec\u53ef\u4ee5\u5c06\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u7ed1\u5b9a\u5230\u8fd9\u7c7b\u8868\u8fbe\u5f0f\u4e0a\uff0c\u4f46\u4e0d\u80fd\u5c06\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u76f4\u63a5\u7ed1\u5b9a\u5728\u5de6\u503c\u4e0a.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>int i = 42; int&amp; r = i;\/\/\u6b63\u786e\uff0c\u5e38\u89c4\u5de6\u503c\u5f15\u7528 <\/code>\n<code>int&amp;&amp; rr = i; \/\/\u9519\u8bef\uff0c\u4e0d\u80fd\u5c06\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u7ed1\u5b9a\u5728\u5de6\u503c\u4e0a <\/code>\n<code>int&amp; r2 = i * 42;\/\/\u9519\u8bef\uff0c\u4e0d\u80fd\u5c06\u5de6\u503c\u5f15\u7528\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53f3\u503c\u4e0a <\/code>\n<code>const int&amp; r3 = i * 42;\/\/\u6b63\u786e\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6307\u5411\u5e38\u91cf\u7684\u5f15\u7528\u7ed1\u5b9a\u5728\u4e00\u4e2a\u53f3\u503c\u4e0a <\/code>\n<code>int&amp;&amp; rr2 = i * 42;\/\/\u6b63\u786e\uff0c\u5c06\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u7ed1\u5b9a\u5728\u53f3\u503c\u4e0a<\/code><\/pre>\n\n\n\n<p>\u53f3\u503c\u5f15\u7528\u4e00\u822c\u53ea\u80fd\u7ed1\u5b9a\u5728\u4e34\u65f6\u5bf9\u8c61\uff0c\u6709\u5982\u4e0b\u7279\u70b9\uff1a<br><strong>1\u3001\u6240\u5f15\u7528\u7684\u5bf9\u8c61\u5c06\u8981\u88ab\u9500\u6bc1<br>2\u3001\u8be5\u5bf9\u8c61\u6ca1\u6709\u5176\u5b83\u7528\u6237<\/strong><br>\u8fd9\u4e24\u4e2a\u7279\u6027\u610f\u5473\u7740\uff1a<strong>\u4f7f\u7528\u53f3\u503c\u5f15\u7528\u7684\u4ee3\u7801\u53ef\u4ee5\u81ea\u7531\u5730\u63a5\u7ba1\u6240\u5f15\u7528\u5730\u5bf9\u8c61\u7684\u8d44\u6e90<\/strong><\/p>\n\n\n<p><strong>NOTE: \u53d8\u91cf\u8868\u8fbe\u5f0f\u90fd\u662f\u5de6\u503c\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u80fd\u5c06\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u7ed1\u5b9a\u5230\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\u4e0a\uff01\uff01<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code>int &amp;&amp;r1 = 40;\/\/r1\u662f\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u5b83\u7ed1\u5b9a\u4e86\u4e00\u4e2a\u5b57\u9762\u503c\nint &amp;&amp;r2 = r1;\/\/\u975e\u6cd5\uff01\uff0c\u53f3\u503c\u5f15\u7528\u4e0d\u80fd\u7ed1\u5b9a\u53d8\u91cf<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e8c\u3001\u6807\u51c6\u5e93move\u51fd\u6570<\/strong><\/p>\n\n\n\n<p>\u867d\u7136\u4e0d\u80fd\u5c06\u4e00\u4e2a\u53f3\u503c\u5f15\u7528\u76f4\u63a5\u7ed1\u5b9a\u5230\u4e00\u4e2a\u5de6\u503c\u4e0a\uff0c\u4f46\u53ef\u4ee5\u663e\u5f0f\u5730\u5c06\u4e00\u4e2a\u5de6\u503c\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u53f3\u503c\u5f15\u7528\u7c7b\u578b\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u4e00\u4e2a\u540d\u4e3amove\u7684\u65b0\u6807\u51c6\u5e93\u51fd\u6570\u6765\u83b7\u5f97\u7ed1\u5b9a\u5230\u5de6\u503c\u7684\u53f3\u503c\u5f15\u7528\uff0c\u6b64\u51fd\u6570\u5b9a\u4e49\u5728\u5934\u6587\u4ef6utility\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int &amp;&amp;rr3 = std::move(rr1)\/\/rr1\u53ef\u4ee5\u662f\u4efb\u4f55\u503c\uff08\u53f3\u503c\u3001\u5de6\u503c\u3001\u5f15\u7528\uff09\uff0c\u8fd4\u56de\u4e00\u4e2a\u53f3\u503c\u3002<\/code><\/pre>\n\n\n\n<p>\u8c03\u7528\u4e86move\u5c31\u610f\u5473\u7740\u627f\u8bfa\uff1a\u9664\u4e86\u5bf9rr1\u8d4b\u503c\u6216\u8005\u9500\u6bc1\u5b83\u5916\uff0c\u6211\u4eec\u5c06\u4e0d\u518d\u4f7f\u7528\u5b83\u3002\u5728\u8c03\u7528move\u4e4b\u540e\uff0c\u6211\u4eec\u4e0d\u80fd\u5bf9\u79fb\u540e\u7684\u6e90\u5bf9\u8c61\u7684\u503c\u505a\u4efb\u4f55\u5047\u8bbe\u3002<\/p>\n\n\n\n<p>Note\uff1a\u4f7f\u7528move\u7684\u4ee3\u7801\u5e94\u8be5\u4f7f\u7528std::move\uff0c\u800c\u4e0d\u662f\u7528\u56e0\u4e3a\u6709\u4e86using namespace std \u5c31\u4e0d\u5199\uff0c\u8fd9\u53ef\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u540d\u5b57\u51b2\u7a81\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4e09\u3001\u79fb\u52a8\u6784\u9020\u51fd\u6570<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">StrVec::StrVec(StrVec &amp;&amp;s) noexcept:elements(s.elements),first_free(s.first_free),cap(s.cap)\n{\n    s.elements = s.first_free = s.cap =nullptr;\n}<\/pre>\n\n\n\n<p>noexcept\u7684\u4f5c\u7528\u5c31\u662f\u544a\u8bc9\u6807\u51c6\u5e93\u6211\u4eec\u7684\u6784\u9020\u51fd\u6570\u4e0d\u629b\u51fa\u4efb\u4f55\u5f02\u5e38\u3002<br>\u8fd9\u4e2a\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4f1a\u5148\u63a5\u7ba1\u7ed9\u5b9aStrVec\u4e2d\u7684\u5185\u5b58\uff0c\u5728\u63a5\u7ba1\u5185\u5b58\u540e\uff0c\u5c06\u7ed9\u5b9a\u5bf9\u8c61\u4e2d\u7684\u6307\u9488\u90fd\u7f6e\u4e3anullptr\uff0c\u907f\u514d\u79fb\u52a8\u540e\uff0c\u65e7\u5bf9\u8c61\u88ab\u9500\u6bc1\u6790\u6784\u3001\u6307\u9488\u88abdelete\u3002<\/p>\n\n\n\n<p><strong>NOTE\uff1a \u4e0d\u4f1a\u629b\u51fa\u5f02\u5e38\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u5fc5\u987b\u6807\u8bb0\u4e3anoexcept !!<\/strong><br>\u56e0\u4e3a<strong>\u6807\u51c6\u5e93\u4f1a\u56e0\u4e3a\u662f\u5426\u6709noexcept\u800c\u4f7f\u7528\u6216\u907f\u514d\u4f7f\u7528\u5b83<\/strong>\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u88c5\u7c7b\u5bf9\u8c61\u7684vector\uff0c\u5f53\u6211\u4eecpush_back\u7684\u65f6\u5019\u5f15\u8d77\u4e86\u91cd\u65b0\u5206\u914d\u5185\u5b58\uff0c\u8fd9\u65f6\u5019\uff0c\u6807\u51c6\u5e93\u6709\u4e24\u4e2a\u9009\u62e9\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u3002\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u76f8\u5bf9\u6765\u8bf4\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5982\u679c\u5728\u5c06\u65e7\u7a7a\u95f4\u4e0a\u7684\u5bf9\u8c61\u4e00\u4e00\u62f7\u8d1d\u5230\u65b0\u7a7a\u95f4\u7684\u8fc7\u7a0b\u4e2d\uff0c\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u6807\u51c6\u5e93\u53ef\u505c\u6b62\u4f7f\u7528\u65b0\u5206\u914d\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u8fd9\u6837\u5373\u4f7f\u51fa\u73b0\u5f02\u5e38\uff0cvector\u7684\u5185\u5bb9\u8fd8\u662f\u6ca1\u6709\u6539\u53d8\uff0c\u9876\u591a\u5c31\u662f\u6700\u65b0\u7684push_back\u6ca1\u6210\u529f\u3002\u800c\u4f7f\u7528\u79fb\u52a8\u6784\u9020\u51fd\u6570\u65f6\uff0c\u56e0\u4e3a\u79fb\u52a8\u7684\u8fc7\u7a0b\u4f1a\u628a\u65e7\u7a7a\u95f4\u7684\u5bf9\u8c61\u6790\u6784\uff0c\u4e00\u65e6\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u95ee\u9898\uff0c\u65e7\u7a7a\u95f4\u7684\u5185\u5bb9\u88ab\u7834\u574f\u800c\u65b0\u7a7a\u95f4\u7684\u5185\u5bb9\u4e5f\u5f02\u5e38\u3002\u6240\u4ee5\u5982\u679c\u4e0d\u58f0\u660e\u79fb\u52a8\u6784\u9020\u51fd\u6570\u4e3anoexcept\uff0c\u6807\u51c6\u5e93\u4f1a\u4f18\u5148\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u56db\u3001\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">StrVec &amp;StrVec::operator= (StrVec &amp;&amp;rhs) noexcept\n{\n    if (this!=&amp;rhs)\/\/\u6392\u9664\u81ea\u8d4b\u503c\n    {\n        free();\/\/\u91ca\u653e\u5df2\u6709\u5143\u7d20\n        elements = rhs.elements;\n        first_free = rhs.first_free;\n        cap = rhs.cap;\n        rhs.elements = rhs.first_free = rhs.cap = nullptr;\n    }\n    return *this;\n}<\/pre>\n\n\n\n<p>\u5148\u68c0\u67e5this\u6307\u9488\u4e0erhs\u7684\u5730\u5740\u662f\u5426\u76f8\u540c\u3002\u5982\u679c\u76f8\u540c\uff0c\u53f3\u4fa7\u548c\u5de6\u4fa7\u8fd0\u7b97\u5bf9\u8c61\u6307\u5411\u76f8\u540c\u7684\u5bf9\u8c61\uff0c\u6211\u4eec\u53ef\u76f4\u63a5\u8fd4\u56de\u3002\u5982\u679c\u4e0d\u540c\uff0c\u6211\u4eec\u91ca\u653e\u5de6\u4fa7\u8fd0\u7b97\u5bf9\u8c61\u6240\u4f7f\u7528\u7684\u5185\u5b58\uff0c\u5e76\u63a5\u7ba1\u7ed9\u5b9a\u5bf9\u8c61\u7684\u5185\u5b58\uff0c\u6700\u540e\u5c06\u7ed9\u5b9a\u5bf9\u8c61\u7684\u6307\u9488\u7f6e\u4e3anullptr\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e94\u3001\u79fb\u52a8\u6784\u9020\u548c\u62f7\u8d1d\u6784\u9020<\/h3>\n\n\n\n<p>\u4e00\u4e2a\u7c7b\u5982\u679c\u5b9a\u4e49\u4e86<strong>\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/strong>\u3001<strong>\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong>\u6216\u8005\u6790\u6784\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5c31<strong>\u4e0d\u4f1a\u4e3a\u5b83\u5408\u6210\u79fb\u52a8\u6784\u9020\u51fd\u6570<\/strong>\u548c<strong>\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong>\u3002<\/p>\n\n\n\n<p>\u540c\u7406\uff0c\u5982\u679c\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a<strong>\u79fb\u52a8\u6784\u9020\u51fd\u6570\u3001\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26<\/strong>\uff0c\u5219\u8be5\u7c7b\u7684\u5408<strong>\u6210\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u62f7\u8d1d\u8d4b\u503c\u8fd0\u7b97\u7b26\u4f1a\u88ab\u5b9a\u4e49\u4e3a\u5220\u9664\u7684<\/strong>\u3002<\/p>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u7c7b<strong>\u65e2\u6709<\/strong>\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0c<strong>\u4e5f\u6709<\/strong>\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u901a\u8fc7<strong>\u666e\u901a\u51fd\u6570\u5339\u914d\u89c4\u5219<\/strong>\u6765\u786e\u5b9a\u4f7f\u7528\u54ea\u4e2a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>StrVec v1,v2;\nv1 = v2;\/\/v2\u662f\u5de6\u503c\uff0c\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\nv2 = std::move(v1);\/\/std::move\u8fd4\u56de\u7684\u662f\u53f3\u503c\uff0c\u867d\u7136\u5b9e\u53c2\u8f6c\u6362\u4e3aconst\u540e\u80fd\u5339\u914d\u4e0a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4f46\u79fb\u52a8\u6784\u9020\u5f62\u53c2\u662f\u53f3\u503c\uff0c\u80fd\u76f4\u63a5\u5339\u914d\u4e0a\uff0c\u4e3a\u6700\u4f73\u5339\u914d<\/code><\/pre>\n\n\n<p>\u5982\u679cStrVec<\/p>\n\n\n<p class=\"has-medium-font-size\"><strong>\u516d\u3001\u79fb\u52a8\u6784\u9020\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Test_contruct\n {\n public:\n     int data_int;\n     int* p;\n     Test_contruct()\n     {\n         data_int = 0;\n         p = new int(0);\n         cout &lt;&lt; \"\u6267\u884c\u4e86\u9ed8\u8ba4\u6784\u9020\" &lt;&lt; endl;\n     } \/\/\u65e0\u53c2\u6784\u9020\n     Test_contruct(int var)\n     {\n         data_int = var;\n         p = new int(0);\n         cout &lt;&lt; \"\u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\" &lt;&lt; endl;\n     } \/\/\u6709\u53c2\u6784\u9020\n     Test_contruct(const Test_contruct&amp; point)\n     {\n         data_int = point.data_int;\n         p = new int(*(point.p)); \n        cout &lt;&lt; \"\u6267\u884c\u4e86\u62f7\u8d1d\u6784\u9020\" &lt;&lt; endl;\n     }\/\/\u62f7\u8d1d\u6784\u9020\n     Test_contruct(Test_contruct&amp;&amp; point) noexcept:data_int(point.data_int) , p(point.p)\n     {\n         cout &lt;&lt; \"\u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\" &lt;&lt; endl;\n         point.p = nullptr;\n     }\/\/\u79fb\u52a8\u6784\u9020\n      Test_contruct operator=(const Test_contruct&amp; point) \/\/=\u8fd0\u7b97\u7b26\u91cd\u8f7d\n     {\n         Test_contruct temp;\n         temp.data_int = point.data_int;\n         temp.p = new int(*(point.p));\n         cout &lt;&lt; \"\u6267\u884c\u4e86\u8d4b\u503c\u62f7\u8d1d\u8fd0\u7b97\u7b26\" &lt;&lt; endl;\n         return temp;\n     }\n     ~Test_contruct()\n     {\n         delete p;\n     }\n };<\/pre>\n\n\n\n<p><strong>\u4f8b\u4e00\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Test_contruct Test01()\n {\n     auto b = Test_contruct(2);\n     return b;\n }\nint main()\n{\n    <code>cout &lt;&lt; \"Test01()  :\" &lt;&lt; endl;<\/code>\n    <code>auto t01 = Test01();<\/code>\n}\n\/*\u8f93\u51fa\u4e3a\uff1a\nTest01()  :\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n*\/<\/pre>\n\n\n\n<p>\u5c31\u662f\u5148\u5728\u51fd\u6570Test01\u5185\u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\u51fd\u6570\uff0c\u7136\u540e\u5728\u51fd\u6570\u8fd4\u56de\u7684\u65f6\u5019\u6267\u884c\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff0c\u6784\u9020t01\u3002<\/p>\n\n\n\n<p><strong>\u4f8b\u4e8c\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Test_contruct Test02()\n {\n     return Test_contruct(2);\n }\nint main()\n{\n    cout &lt;&lt; \"Test02()  :\" &lt;&lt; endl;\n    auto t02 = Test02();\n}\n\/*\u8f93\u51fa\u7ed3\u679c\nTest02()  :\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n*\/<\/pre>\n\n\n\n<p>\u5982\u679c\u5728return\u7684\u65f6\u5019\u624d\u6784\u9020\u7684\u8bdd\uff0c\u5c31\u76f8\u5f53\u4e8e\u76f4\u63a5\u5728main\u51fd\u6570\u91cc\u6267\u884c\u6709\u53c2\u6784\u9020\uff0c\u65e0\u79fb\u52a8\u6784\u9020<\/p>\n\n\n\n<p><strong>\u4f8b\u4e09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void Test03()\n {\n     vector arr;\n     arr.reserve(3);\n     for (int i = 0; i &lt; 4; ++i)\n     {\n         auto temp = Test_contruct(i);\n         arr.push_back(temp);\n     }\n }\nint main()\n{\n    <code>cout &lt;&lt; \"Test03()  :\" &lt;&lt; endl;<\/code>\n   <code> Test03();<\/code>\n}\n\/*\u8f93\u51fa\u7ed3\u679c\nTest03()  :\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u62f7\u8d1d\u6784\u9020\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u62f7\u8d1d\u6784\u9020\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u62f7\u8d1d\u6784\u9020\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u62f7\u8d1d\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n*\/<\/pre>\n\n\n\n<p>\u6211\u4eec\u5148\u521b\u5efa\u4e86\u4e00\u4e2avertor\u5e76\u9884\u75593\u4e2a\u4f4d\u7f6e\u7684\u7a7a\u95f4\uff0c\u7136\u540e\u518d\u5faa\u73af\u91cc\uff0c\u5148\u7ed9temp\u6267\u884c\u4e00\u6b21\u6709\u53c2\u6784\u9020\u51fd\u6570\uff0c\u7136\u540e\u7528push_back\u7684\u65b9\u5f0f\u52a0\u5230vector\u4e2d\uff0c\u6b64\u65f6\u4f1a\u6267\u884c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u5f53\u5faa\u73af\u5230\u4e86\u7b2c\u56db\u6b21\u540e\uff0c\u6b64\u65f6push_back\u65f6\uff0c\u5fc5\u987b\u5148\u5c06\u539fvector\u76843\u4e2a\u5143\u7d20\u79fb\u52a8\u5230\u4e00\u4e2a\u65b0\u7684\u7a7a\u95f4\uff08\u56e0\u4e3a\u539f\u7a7a\u95f4\u53ea\u80fd\u5bb9\u7eb3\u4e09\u4e2a\u5143\u7d20\uff09\uff0c\u6545\u6709\u4e86\u4e09\u6b21\u79fb\u52a8\u6784\u9020\u3002<\/p>\n\n\n\n<p><strong>\u4f8b\u56db\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void Test04()\n {\n     vector arr;\n     arr.reserve(3);\n     for (int i = 0; i &lt; 4; ++i)\n     {\n         arr.push_back(Test_contruct(i));\n         \/*\n         \u76f8\u5f53\u4e8e\uff1a\n         auto temp = Test_contruct(i);\n         arr.push_back(std::move(temp));\n         *\/\n     }\n }\nint main()\n{\n    <code>cout &lt;&lt; \"Test04()  :\" &lt;&lt; endl;<\/code>\n   <code> Test04();<\/code>\n}\n\/*\u8f93\u51fa\u7ed3\u679c\nTest04()  :\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n \u6267\u884c\u4e86\u6709\u53c2\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n \u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\n*\/<\/pre>\n\n\n\n<p>\u76f8\u6bd4\u4e8e\u4f8b\u4e09\uff0c\u6267\u884c\u4e86\u79fb\u52a8\u6784\u9020\u800c\u4e0d\u662f\u62f7\u8d1d\u6784\u9020\uff0c\u63d0\u5347\u4e86\u4e00\u5b9a\u6548\u7387<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u53f3\u503c\u5f15\u7528 \u6211\u4eec\u5e38\u89c4\u7684\u5f15\u7528\u4e5f\u53eb\u5de6\u503c\u5f15\u7528\uff0c\u6211\u4eec\u4e0d\u80fd\u5c06\u5de6\u503c\u5f15\u7528\u7ed1\u5b9a\u5230\u8981\u6c42\u8f6c\u6362\u7684\u8868\u8fbe\u5f0f\u3001\u5b57\u9762\u5e38\u91cf\u6216\u662f\u8fd4\u56de\u53f3\u503c\u7684\u8868\u8fbe&hellip;<a href=\"https:\/\/www.notown.top\/?p=483\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++ \u5bf9\u8c61\u79fb\u52a8<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[11],"tags":[],"class_list":["post-483","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=483"}],"version-history":[{"count":10,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/483\/revisions"}],"predecessor-version":[{"id":626,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/483\/revisions\/626"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}