{"id":235,"date":"2021-06-07T18:04:55","date_gmt":"2021-06-07T10:04:55","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=235"},"modified":"2025-10-09T13:56:21","modified_gmt":"2025-10-09T05:56:21","slug":"cpp-standard-library-01-new-features-of-c11-language","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/07\/cpp-standard-library-01-new-features-of-c11-language\/","title":{"rendered":"C++\u6807\u51c6\u5e93 01 C++11\u8bed\u8a00\u65b0\u7279\u6027"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<\/p>\n<h2>\u4e00\u3001\u4e24\u4e2a\u7ec6\u8282<\/h2>\n<h3>1.1 Template\u8868\u8fbe\u5f0f\u4e2d\u7684\u7a7a\u683c<\/h3>\n<pre><code class=\"language-cpp\">vector&lt;vector&lt;int&gt; &gt;;   \/\/\u5728\u6240\u6709cpp\u7248\u672c\u4e2d\u53ef\u7528\nvector&lt;vector&lt;int&gt;&gt;;    \/\/\u5728cpp11\u6807\u51c6\u4e2d\u53ef\u7528<\/code><\/pre>\n<h3>1.2 nullpter\u4e0eNULL<\/h3>\n<p>&emsp;&emsp;nullptr\u4e3a\u65e0\u7c7b\u578b\u7684\u6307\u9488\uff0cNULL\u7c7b\u578b\u4e3aint\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nvoid func(int);\nvoid func(void*);\n\nint main()\n{  \n    func(NULL); \/\/calls func(int)\n    func(nullptr);  \/\/calls func(void*)\n    return 0;\n}\n\nvoid func(int)\n{\n    cout &lt;&lt; &quot;calls func(int)&quot; &lt;&lt; endl;\n}\n\nvoid func(void*)\n{\n    cout &lt;&lt; &quot;calls func(void*)&quot; &lt;&lt; endl;\n}<\/code><\/pre>\n<h2>\u4e8c\u3001\u4ee5auto\u5b8c\u6210\u7c7b\u578b\u81ea\u52a8\u63a8\u5bfc<\/h2>\n<p>&emsp;&emsp;\u5728cpp11\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u201cauto\u201d\u5173\u952e\u5b57\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u800c\u4e0d\u201c\u76f4\u63a5\u6307\u5b9a\u201d\u5176\u7c7b\u578b\u3002<br \/>\n&emsp;&emsp;auto\u4ea7\u751f\u7684\u5bf9\u8c61\u6839\u636e\u5176\u521d\u59cb\u5316\u503c\u81ea\u52a8\u63a8\u5bfc\u51fa\u8be5\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u8fd9\u79cd\u521d\u59cb\u5316\u53ef\u4ee5\u662f\u663e\u793a\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u9690\u5f0f\u7684\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint main()\n{  \n    \/\/\u57fa\u672c\u7528\u6cd5\uff1a\n    auto MyInt = 5;\n    auto MyDouble(5.6);\n    cout &lt;&lt; &quot;MyInt == &quot; &lt;&lt; MyInt &lt;&lt; endl;\n    cout &lt;&lt; &quot;MyDouble == &quot; &lt;&lt; MyDouble &lt;&lt; endl;\n    \/\/auto a;   \u9519\u8bef,auto\u5e94\u8be5\u5728\u521d\u59cb\u5316\u65f6\u5019\u4e3a\u5176\u6307\u5b9a\u7c7b\u578b\n\n    \/\/\u5e38\u89c1\u7528\u6cd5\uff1aauto\u4ee3\u66ff\u590d\u6742\u7c7b\u578b\n    vector&lt;int&gt;MyVector;    \/\/\u521b\u5efa\u5bb9\u5668\n    MyVector.push_back(0);\n\n    vector&lt;int&gt;::iterator it = MyVector.begin();\n    auto itr = MyVector.begin();\n    if (it == itr)\n    {\n        cout &lt;&lt; &quot;it == itr&quot; &lt;&lt; endl;\n    }\n    \/\/display: it == itr\n    return 0;\n}<\/code><\/pre>\n<h2>\u4e09\u3001\u521d\u59cb\u5316\u76f8\u5173<\/h2>\n<h3>3.1 \u4e00\u81f4\u6027\u521d\u59cb\u5316\uff08Uniform Initialization\uff09<\/h3>\n<p>&emsp;&emsp;\u5728cpp11\u8868\u4e2d\u4e2d\uff0c\u6240\u6709\u7684\u521d\u59cb\u5316\u64cd\u4f5c\u90fd\u88ab\u53ef\u4ee5\u88ab\u5927\u62ec\u53f7\u6240\u4ee3\u66ff\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;complex&gt;\nusing namespace std;\n\nint main()\n{  \n    int a(5);   \/\/ok\n    int b{ 5 }; \/\/ok\n\n    complex&lt;int&gt;c1{ 5,6 };  \/\/ok\n    complex&lt;int&gt;c2(5, 6);   \/\/ok\n\n    vector&lt;int&gt; v1{ 0, 1, 2 };  \/\/ok \u6570\u7ec4\u7684\u521d\u59cb\u5316\n    \/\/vector&lt;int&gt; v2(0, 1, 2);  \/\/wrong\n\n    int c[]{ 1,2,3 };   \/\/ok \u6570\u7ec4\u7684\u521d\u59cb\u5316\n    \/\/int d[](1, 2, 3); \/\/wrong\n    return 0;\n}<\/code><\/pre>\n<h3>3.2 \u521d\u503c\u5217\uff08Initializer List\uff09<\/h3>\n<p>&emsp;&emsp;\u521d\u503c\u5217\uff08\u7528\u5927\u62ec\u53f7\u8fdb\u884c\u521d\u59cb\u5316\uff09\u4f1a\u9020\u6210\u5f3a\u8feb\u521d\u59cb\u5316\uff0c\u5982\u679c\u53d8\u91cf\u5c5e\u4e8e\u67d0\u79cd\u57fa\u672c\u7c7b\u578b\uff0c\u90a3\u4e48\u5b83\u5c06\u88ab\u521d\u59cb\u5316\u4e3a0\uff08\u6216\u8005nullptr\u2014\u2014\u5bf9\u6307\u9488\u800c\u8a00\uff09\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    int a(5);   \/\/\u5982\u679c\u662f\uff1aint a(); \u5219\u4f1a\u62a5\u9519\uff1a\u65e0\u6cd5\u89e3\u6790\u7684\u5916\u90e8\u547d\u4ee4\n    int b{};\n    cout &lt;&lt; &quot;a == &quot; &lt;&lt; a &lt;&lt; endl;   \/\/display 5\n    cout &lt;&lt; &quot;b == &quot; &lt;&lt; b &lt;&lt; endl;   \/\/display 0\n    return 0;\n}\n    \u521d\u59cb\u5217\u53ef\u4ee5\u907f\u514d\u7a84\u5316\uff08narrowing\uff09\n    \u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002\n\n#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    int a(5.6);     \/\/warning\n    int b{ 5.6 };   \/\/errow\n    cout &lt;&lt; &quot;a == &quot; &lt;&lt; a &lt;&lt; endl;\n    cout &lt;&lt; &quot;b == &quot; &lt;&lt; b &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n<p>&emsp;&emsp;cpp11\u63d0\u4f9b\u4e86\u81ea\u5b9a\u4e49\u521d\u503c\u5217\u6765\u8fdb\u884c\u521d\u59cb\u5316\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    initializer_list&lt;int&gt;list{ 5,6,7 };\n    for (auto it = list.begin(); it != list.end(); ++it)\n    {\n        cout &lt;&lt; *it &lt;&lt; &#039; &#039;;\n    }\n    cout &lt;&lt; endl;\n    \/\/display 5 6 7\n    return 0;\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u5f53\u521d\u503c\u5217\u548c\u6307\u660e\u53c2\u6570\u4e2a\u6570\u7684\u6784\u9020\u51fd\u6570\u540c\u65f6\u5b58\u5728\u65f6\uff0c\u521d\u503c\u5217\u6784\u9020\u51fd\u6570\u5c06\u80dc\u51fa\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nclass Initializer\n{\npublic:\n    Initializer(int, int)\n    {\n        cout &lt;&lt; &quot;calls (int, int)&quot; &lt;&lt; endl;\n    }\n\n    Initializer(initializer_list&lt;int&gt;)\n    {\n        cout &lt;&lt; &quot;call list&quot; &lt;&lt; endl;\n    }\n};\n\nint main()\n{  \n    \/\/\u53c2\u6570\u4e2a\u6570\u76f8\u540c\u7684\u65f6\u5019\uff1a\n    Initializer a(5, 6);        \/\/\u663e\u5f0f\u5c0f\u62ec\u53f7\uff1acalls (int, int)\n    Initializer b{ 5,6 };       \/\/\u663e\u5f0f\u5927\u62ec\u53f7\uff1acall list\n    Initializer e = { 1,2 };    \/\/\u9690\u5f0f\uff1acall list\n\n    \/\/\u53c2\u6570\u4e2a\u6570\u4e0d\u540c\u7684\u65f6\u5019\uff1a\n    Initializer c{ 1 };     \/\/  call list\n    Initializer d{ 1,2,3 };     \/\/  call list\n    return 0;\n}<\/code><\/pre>\n<h2>\u56db\u3001Range-Based for\u5faa\u73af<\/h2>\n<p>&emsp;&emsp;cpp11\u5f15\u5165\u4e86\u65b0\u7684\u5faa\u73af\u5f62\u5f0f\uff0c\u53ef\u4ee5\u9010\u4e00\u8fed\u4ee3\u67d0\u4e2a\u533a\u95f4\u3001\u96c6\u5408\u3001\u6570\u7ec4\u5185\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u5f0f\u548cPython\u4e2d\u7684for in range()\u7c7b\u4f3c\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint main()\n{  \n    vector&lt;int&gt;num{ 0,1,2 };\n    for (const auto&amp; it : num)\n    {\n        cout &lt;&lt; it &lt;&lt; &quot; &quot;;\n    }\n    \/\/display 0 1 2\n    return 0;\n}<\/code><\/pre>\n<h2>\u4e94\u3001move\u8bed\u4e49<\/h2>\n<p>&emsp;&emsp;<em>\u6ce8\u610f\uff1amove\u8bed\u4e49\u662fcpp11\u6807\u51c6\u4e2d\u6700\u91cd\u8981\u7684\u4e00\u90e8\u5206\uff0c\u5efa\u8bae\u8054\u5408\u5176\u4ed6\u8d44\u6599\u4e00\u8d77\u9605\u8bfb\u3002<\/em><br \/>\n&emsp;&emsp;cpp11\u63d0\u4f9b\u4e86\u4e00\u79cd\u907f\u514d\u975e\u5fc5\u8981\u62f7\u8d1d\uff08copy\uff09\u548c\u4e34\u65f6\u5bf9\u8c61\uff08temporary\uff09\u7684\u65b9\u6cd5\uff0c\u8fd9\u79cd\u65b9\u6cd5\u88ab\u79f0\u4e3a\u642c\u8fc1\u8bed\u4e49\uff08move semantic\uff09\u3002<br \/>\n&emsp;&emsp;\u8bf7\u770b\u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nclass Base\n{\nprivate:\n    int *element;\npublic:\n    Base(const int&amp; _el = 0)\n    {\n        element = new int(_el);\n    }\n\n    Base(const Base&amp; _b):element{ _b.element }\n    {\n        cout &lt;&lt; &quot;call copy constructor&quot; &lt;&lt; endl;\n    }\n\n    Base(Base&amp;&amp; _b) :element{ _b.element }\n    {\n        _b.element = nullptr;\n        cout &lt;&lt; &quot;call move constructor&quot; &lt;&lt; endl;\n    }\n};\n\nBase func()\n{\n    Base b;\n    return b;\n}\n\nint main()\n{  \n    Base Abase{ 5 };\n    Base Bbase{ Abase };        \/\/\u5de6\u503c \u8c03\u7528copy\u6784\u9020\u51fd\u6570\n    Base Cbase{ func() };       \/\/\u53f3\u503c \u8c03\u7528move\u6784\u9020\u51fd\u6570\n    Base Dbase{ move(Abase) };  \/\/move\u5de6\u503c \u8c03\u7528move\u6784\u9020\u51fd\u6570\n    return 0;\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u5728\u4e0a\u8ff0\u7ed3\u679c\u4e2d\u5bf9\u4e8e\u5de6\u503c\uff0c\u91c7\u7528\u590d\u5236\u6784\u9020\u51fd\u6570\uff1b\u5bf9\u4e8e\u53f3\u503c\u91c7\u7528move\u6784\u9020\u51fd\u6570\u3002<br \/>\n&emsp;&emsp;\u90a3\u4e48\u4e3a\u4ec0\u4e48move\u6784\u9020\u4e2d\u8981\u628a\u539f\u59cb\u6307\u9488\u7f6e\u4e3anull\u5462\uff1f\u56e0\u4e3a\u5982\u679c\u4e0d\u7f6e\u4e3anull\uff0c\u90a3\u4e48\u4e34\u65f6\u53d8\u91cf\u6d88\u5931\u65f6\uff0c\u4f1a\u6790\u6784\uff0c\u91ca\u653e\u81ea\u5df1\u7684\u6307\u9488\uff0c\u8fd8\u8d44\u6e90\u7ed9os\uff0c\u90a3\u4e48\u65b0\u5bf9\u8c61\u4e2d\u7684\u6307\u9488\u4e5f\u5c31\u968f\u4e4b\u4f1a\u88abdelete\u6389\u3002\u6240\u4ee5\u9700\u8981\u628a\u539f\u59cb\u6307\u9488\u7f6e\u4e3anull\uff0c\u8fd9\u6837\u4e34\u65f6\u53d8\u91cf\u5728\u6790\u6784\u65f6\u5c31\u627e\u4e0d\u5230\u7ed9\u81ea\u5df1\u5206\u914d\u7684\u90a3\u5757\u5185\u5b58\u4e86\uff0c\u90a3\u5757\u5185\u5b58\u4e5f\u5c31\u53ef\u4ee5\u88ab\u65b0\u5bf9\u8c61\u6b63\u5e38\u4f7f\u7528\u4e86\u3002<br \/>\n&emsp;&emsp;\u6ce8\u610f\uff1a\u8fd9\u91cc\u7684\u91cd\u8f7d\u89c4\u5219\u8981\u6c42\u5982\u679c\u8981\u8c03\u7528move\u6784\u9020\uff0c\u90a3\u4e48\u4e00\u5b9a\u8981\u4f20\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u5e76\u4e14\u4e00\u5b9a\u8981\u662f\u4e00\u4e2a\u53ef\u4ee5\u4fee\u6539\u7684\u4e34\u65f6\u53d8\u91cf\uff0c\u5982\u679c\u4e0d\u80fd\u4fee\u6539\uff0c\u90a3\u4e48\u4e34\u5bf9\u8c61\u5185\u7684\u6307\u9488\u5c31\u4e0d\u80fd\u7f6e\u4e3anull\u4e86\uff0c\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u7684\u662fconst\u7c7b\u578b\uff0c\u90a3\u4e48\u5c31\u4e0d\u80fd\u8c03\u7528move\u6784\u9020\u4e86<br \/>\n&emsp;&emsp;\u4e0b\u9762\u6765\u770bmove\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nint main()\n{  \n    vector&lt;int&gt; num{ 1,2,3,4,5,6 };\n    move(num.begin() + 2, num.begin() + 3, num.begin());\n    for (const auto&amp; it : num)\n    {\n        cout &lt;&lt; it &lt;&lt; &#039; &#039;;\n    }\n    \/\/display 3 2 3 4 5 6\u7b49\u6548\u4e8ecopy\u51fd\u6570\n    \/\/\u8303\u56f4[first, second)\n    return 0;  \n}<\/code><\/pre>\n<h2>\u516d\u3001\u65b0\u5f0f\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u5e38\u91cf\uff08String Literal\uff09<\/h2>\n<p>&emsp;&emsp;Row String Literal\u5141\u8bb8\u6211\u4eec\u5b9a\u4e49\u5b57\u7b26\u5e8f\u5217\uff08character sequence\uff09\u3002\u8fd9\u6837\u53ef\u4ee5\u8282\u7701\u6389\u4e0d\u5fc5\u8981\u7684\u7279\u6b8a\u7b26\u53f7\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    string a = R&quot;(\\\\n)&quot;;    \/\/\u7b49\u4ef7\u4e8e &quot;\\\\\\\\n&quot;\n    cout &lt;&lt; a &lt;&lt; endl;      \/\/display \\\\n\n\n    string b = R&quot;nc(a\\\n                    b\\nc()&quot;\n                    )nc&quot;;\n    cout &lt;&lt; b &lt;&lt; endl;      \/\/\u7b49\u4ef7\u4e8e&quot;a\\\\\\n                   b\\\\nc()&quot;\\n&quot;;\u6ce8\u610f\u7a7a\u683c\n    return 0;  \n}<\/code><\/pre>\n<p>&emsp;&emsp;Row string\u4ee5R\u201d(\u5f00\u5934\uff0c\u4ee5)\u201d\u7ed3\u5c3e\uff0c\u540c\u65f6\u53ef\u4ee5\u5d4c\u5957\u5b57\u6bcd\uff0c\u4f8b\u5982\uff1a\u7528R\u201dabc(_your<em>chars<\/em>)abc\u201d\u6765\u6765\u4e66\u5199\u3002<\/p>\n<h2>\u4e03\u3001Lambda\u8868\u8fbe\u5f0f<\/h2>\n<h3>7.1 Lambda\u8bed\u6cd5<\/h3>\n<p>&emsp;&emsp;\u6240\u8c13Lambda\u662f\u4e00\u4efd\u529f\u80fd\u5b9a\u4e49\u5f0f\uff0c\u53ef\u88ab\u7528\u4e8e\u8bed\u53e5\uff08statement\uff09\u6216\u8868\u8fbe\u5f0f\uff08expression\uff09\u5185\u90e8\u3002\u56e0\u6b64Lambda\u53ef\u4ee5\u88ab\u7528\u4f5c\u5185\u8054\u51fd\u6570\u6765\u4f7f\u7528\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    auto lambda = []{\n        cout &lt;&lt; &quot;call Lambda&quot; &lt;&lt; endl;\n    };\n    lambda();\n    return 0;  \n}\n    \u540c\u65f6\uff0cLambda\u4e5f\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\u3002\n\n#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    string a = &quot;call Lambda&quot;;\n    auto lambda = [](const string&amp; _l){\n        cout &lt;&lt; _l &lt;&lt; endl;\n    };\n    lambda(a);\n    return 0;  \n}<\/code><\/pre>\n<p>&emsp;&emsp;\u6ce8\u610f\uff1aLambda\u4e0d\u53ef\u4ee5\u662ftemplate\uff0c\u5fc5\u987b\u6307\u660e\u6240\u6709\u7c7b\u578b\u3002<\/p>\n<h3>7.2 Lambda\u7684\u8fd4\u56de\u7c7b\u578b<\/h3>\n<p>&emsp;&emsp;Lambda\u4e5f\u53ef\u4ee5\u50cf\u666e\u901a\u51fd\u6570\u4e00\u6837\u62e5\u6709\u8fd4\u56de\u503c\uff0c\u8fd9\u91cc\u4f60\u53ef\u4ee5\u6307\u5b9a\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u6307\u5b9a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    auto UnSpecifyType = []() {\n        return 1;\n    };\n\n    auto SpecifyType = []()-&gt;int {\n        return 2;\n    };\n    cout &lt;&lt; UnSpecifyType() &lt;&lt; endl;\n    cout &lt;&lt; SpecifyType() &lt;&lt; endl;\n    return 0;  \n}<\/code><\/pre>\n<h3>7.3 Capture\u7528\u4ee5\u8bbf\u95ee\u5916\u90e8\u4f5c\u7528\u57df<\/h3>\n<p>&emsp;&emsp;\u5728Lambda introducer\uff08\u6bcf\u4e2aLambda\u5f00\u59cb\u7684\u65b9\u62ec\u53f7\uff09\u5185\uff0c\u4f60\u53ef\u4ee5\u6307\u660e\u4e00\u4e2acapture\u6765\u5904\u7406\u4f5c\u7528\u57df\u5185\u672a\u88ab\u4f20\u9012\u4e3a\u5b9e\u53c2\u7684\u6570\u636e\u3002<\/p>\n<ul>\n<li>[=]\u610f\u5473\u7740\u4ee5by value\u7684\u65b9\u5f0f\u4f20\u5165\uff08\u4f20\u503c\uff0c\u4e0d\u53ef\u4fee\u6539\uff09\u3002\u56e0\u6b64\u5f53\u8fd9\u4e2aLambda\u88ab\u5b9a\u4e49\u65f6\uff0c\u4f60\u53ef\u4ee5\u8bfb\u53d6\u6240\u6709\u5916\u90e8\u6570\u636e\uff0c\u4f46\u662f\u4e0d\u80fd\u66f4\u6539\u4ed6\u4eec\u3002<\/li>\n<li>[&amp;]\u610f\u5473\u7740\u4ee5by reference\u7684\u65b9\u5f0f\u4f20\u5165\uff08\u5f15\u7528\uff0c\u53ef\u4ee5\u4fee\u6539\uff09\u3002\u56e0\u6b64\u5f53\u8fd9\u4e2aLambda\u88ab\u5b9a\u4e49\u65f6\uff0c\u4f60\u5bf9\u6240\u6709\u6570\u636e\u7684\u4fee\u6539\u90fd\u662f\u5408\u6cd5\u7684\uff0c\u524d\u63d0\u662f\u62e5\u6709\u4fee\u6539\u7684\u6743\u9650\u3002<\/li>\n<\/ul>\n<p>&emsp;&emsp;\u4f60\u8fd8\u53ef\u4ee5\u7528mutable\u7684\u65b9\u5f0f\u4f20\u5165\uff08\u4f20\u503c\uff0c\u53ef\u4ee5\u4fee\u6539\uff09\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u5df2by value\u7684\u65b9\u5f0f\u4f20\u5165\uff0c\u4f46\u662f\u4f60\u5374\u53ef\u4ee5\u4fee\u6539\u5176\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    int x = 0, y = 0;\n\n    auto ByValue = [x, y]()\n    {\n        \/\/++x;  error:\u5fc5\u987b\u662f\u53ef\u4ee5\u4fee\u6539\u7684\u5de6\u503c\n        \/\/++y;\n    };\n\n    auto ByReference = [&amp;x, &amp;y]()\n    {\n        ++x;    \/\/ok\n        ++y;\n    };\n\n    auto Mutable = [x, y]()mutable\n    {\n        ++x;    \/\/ok\n        ++y;\n    };\n    return 0;  \n}<\/code><\/pre>\n<p>&emsp;&emsp;\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u60f3\u8981\u5728Lambda\u4e2d\u4f7f\u7528\u5916\u90e8\u53c2\u6570\uff0c\u5fc5\u987b\u8981\u5c06\u5176\u653e\u5165introducer\u4e2d\u3002<\/p>\n<h3>7.4 Lambda\u7684\u7c7b\u578b<\/h3>\n<p>&emsp;&emsp;Lambda\u7684\u7c7b\u578b\u662f\u4e2a\u4e0d\u5177\u540dfunction object\uff08\u6216\u79f0functor\uff09\u3002\u6bcf\u4e2aLambda\u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002\u5982\u679c\u60f3\u6839\u636e\u7c7b\u578b\u58f0\u660e\u5bf9\u8c61\uff0c\u53ef\u501f\u52a9\u4e8etemplate\u6216auto\u3002\u5982\u679c\u5b9e\u5728\u9700\u8981\u5199\u4e0b\u7c7b\u578b\uff0c\u53ef\u4f7f\u7528decltype()\uff0c\u4f8b\u5982\u628a\u4e00\u4e2aLambda\u4f5c\u4e3ahash fucntion\u6216\u662fordering\u51c6\u5219\u6216\u662fsorting\u51c6\u5219\u4f20\u9012\u7ed9\u5173\u8054\u5f0f\uff08associative\uff09\u5bb9\u5668\u6216\u8005\u4e0d\u5b9a\u5e8f\uff08unordered\uff09\u5bb9\u5668\u3002<br \/>\n&emsp;&emsp;\u6216\u8005\u4f7f\u7528\u6807\u51c6\u5e93\u63d0\u4f9b\u7684std::functioon&lt;&gt;class template\uff0c\u6307\u660e\u4e00\u4e2a\u4e00\u822c\u5316\u7c7b\u578b\u7ed9functional programing\u4f7f\u7528\u3002\u8fd9\u4e2aclass template\u63d0\u4f9b\u4e86\u201c\u660e\u786e\u6307\u51fa\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u4e3aLambda\u7684\u552f\u4e00\u65b9\u6cd5\u201d\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;functional&gt;\nusing namespace std;\n\nfunction&lt;int(int, int)&gt; ReturnLambda()\n{\n    return [](int x, int y)\n    {\n        return x + y;\n    };\n}\n\nint main()\n{  \n    auto l = ReturnLambda();\n    cout &lt;&lt; l(5, 6) &lt;&lt;  endl;\n    \/\/\u8fd4\u56de\u7c7b\u578b\u4e3alambda,\u4e0d\u80fd\u591f\u76f4\u63a5cout,\u8fd9\u91cc\u7528\u4e00\u4e2a\u4e2d\u95f4\u53d8\u91cf\u4ee3\u66ff\u51fd\u6570\u672c\u8eab\n    return 0;  \n}<\/code><\/pre>\n<h2>\u516b\u3001\u867d\u65e7\u72b9\u65b0\u7684\u8bed\u8a00\u7279\u6027<\/h2>\n<h3>8.1 \u975e\u6a21\u677f\u7c7b\u578b\u53c2\u6570\uff08Nontype Template Parameter\uff09<\/h3>\n<p>&emsp;&emsp;\u5728\u7c7b\u578b\u53c2\u6570\uff08Type parameter\uff09\u4e4b\u5916\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4e3atemplate\u4f7f\u7528\u975e\u7c7b\u578b\u53c2\u6570\uff08Nontype parameter\uff09\u3002\u8fd9\u6837\u7684\u53c2\u6570\u4ea6\u88ab\u89c6\u4e3atemplate\u7684\u4e00\u90e8\u5206\u3002\u4f8b\u5982\u5bf9\u4e8e\u6807\u51c6\u7684class bitset&lt;&gt;\uff0c\u4f60\u53ef\u4ee5\u4f20\u9012bit\u4e2a\u6570\u4f5c\u4e3atemplate\u5b9e\u53c2\u3002\u4e0b\u9762\u7684\u8bed\u53e5\u5b9a\u4e49\u4e86\u4e24\u4e2abitfield\uff1a\u4e00\u4e2a\u5e26\u670962bit\uff0c\u53e6\u5916\u4e00\u4e2a\u5e26\u670964bit\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;bitset&gt;\nusing namespace std;\n\nint main()\n{  \n    bitset&lt;32&gt;flags32 = 5;\n    cout &lt;&lt; flags32 &lt;&lt; endl;\n    \/\/display:00000000000000000000000000000101\n    bitset&lt;64&gt;flags64 = 5;\n    cout &lt;&lt; flags64 &lt;&lt; endl;\n    \/\/display:0000000000000000000000000000000000000000000000000000000000000101\n    return 0;  \n}<\/code><\/pre>\n<p>&emsp;&emsp;\u56e0\u4e3a\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u5b9e\u53c2\uff0c\u4f7f\u7528\u8fd9\u4e9bbitset\u7684\u7c7b\u578b\u662f\u4e0d\u76f8\u540c\u7684\u3002\u56e0\u6b64\u4e0d\u53ef\u4ee5\u5bf9\u4e0a\u8ff0\u4e8c\u8005\u4f7f\u7528\u8fd0\u7b97\u7b26\uff0c\u9664\u975e\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<h3>8.2 \u57fa\u7840\u7c7b\u578b\u7684\u660e\u786e\u5316\u521d\u59cb\u503c<\/h3>\n<p>&emsp;&emsp;\u5982\u679c\u4f60\u4f7f\u7528\u201c\u4e00\u4e2a\u660e\u786e\u7684\u6784\u9020\u51fd\u6570\u8c03\u7528\uff0c\u4f46\u4e0d\u7ed9\u5b9e\u53c2\u201d\u8fd9\u6837\u7684\u8bed\u6cd5\uff0c\u57fa\u7840\u7c7b\u578b\u4f1a\u88ab\u8bbe\u5b9a\u521d\u503c0\u3002<br \/>\n&emsp;&emsp;\u4e0b\u9762\u901a\u8fc7\u4e00\u6bb5\u4ee3\u7801\u6765\u8bf4\u660e\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{  \n    int a;          \/\/undefined value\n    int b = int();  \/\/initialized with zero\n    int c();        \/\/error\n    int d{};        \/\/initialized with zero(since C++11)\n    cout &lt;&lt; a &lt;&lt; &#039; &#039; &lt;&lt; b &lt;&lt; &#039; &#039; &lt;&lt; c &lt;&lt; &#039; &#039; &lt;&lt; d &lt;&lt; endl;\n    return 0;  \n}<\/code><\/pre>\n<h3>8.3 main()\u5b9a\u4e49\u5f0f<\/h3>\n<p>&emsp;&emsp;main\u51fd\u6570\u53ea\u6709\u4e24\u79cd\u5b9a\u4e49\u5f0f\u5177\u5907\u53ef\u79fb\u690d\u6027\uff0c\u90a3\u5c31\u662f\uff1a<\/p>\n<pre><code class=\"language-cpp\">int main()\n{  \n\n}\n\/\/\u548c\nint main(int argc, char** argv)\n{  \n\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u6ce8\u610f\u5176\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u662fint\u3002<br \/>\n&emsp;&emsp;\u4f60\u53ef\u4ee5\uff08\u4f46\u4e0d\u5fc5\u8981\uff09\u4ee5\u4e00\u4e2areturn\u7ed3\u675fmain()\u3002\u4e0d\u540c\u4e8ec\uff0cc++\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9690\u6666\u7684return 0;\u4e8emain()\u7684\u7ec8\u70b9\u3002\u8fd9\u610f\u5473\u7740\u5728\u7a0b\u5e8f\u79bb\u5f00main()\u7684\u65f6\u5019\u4e0d\u7528\u5199return\u8bed\u53e5\u30020\u610f\u5916\u7684\u4efb\u4f55\u503c\u90fd\u4ee3\u8868\u7740\u67d0\u79cd\u5931\u8d25\u3002<br \/>\n&emsp;&emsp;\u5982\u679c\u4e0d\u60f3\u4ee5\u201cmain()\u8fd4\u56de\u201d\u65b9\u5f0f\u7ed3\u675fc++\u7a0b\u5e8f\uff0c\u901a\u5e38\u5e94\u8be5\u8c03\u7528exit()\u3001quick_exit()\uff08c++11\uff09\u6216terminate()\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4e24\u4e2a\u7ec6\u8282 1.1 Template\u8868\u8fbe\u5f0f\u4e2d\u7684\u7a7a\u683c vector&lt;vector&lt;int&gt; &gt;; \/\/\u5728\u6240 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/235"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":434,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions\/434"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}