{"id":94101,"date":"2026-08-13T19:08:57","date_gmt":"2026-08-13T11:08:57","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/94101.html"},"modified":"2026-08-13T19:08:57","modified_gmt":"2026-08-13T11:08:57","slug":"%e5%af%b9%e4%ba%8ec%ef%bc%9ac11%e4%b8%adlambda%e3%80%81function%e3%80%81bind%e7%9a%84%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/94101.html","title":{"rendered":"\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790"},"content":{"rendered":"<h2>\u5f00\u7bc7\u4ecb\u7ecd&#xff1a;<\/h2>\n<p>hello \u5927\u5bb6&#xff0c;\u672c\u7bc7\u535a\u5ba2\u6211\u4eec\u6765\u5b66\u4e60C&#043;&#043;11\u4e2d\u7684lambda\u3001function\u3001bind\u3002<\/p>\n<p>\u5728 C&#043;&#043;11 \u6807\u51c6\u8bde\u751f\u4e4b\u524d&#xff0c;\u5f00\u53d1\u8005\u5728\u5904\u7406\u8f7b\u91cf\u7ea7\u51fd\u6570\u903b\u8f91\u3001\u7edf\u4e00\u53ef\u8c03\u7528\u5bf9\u8c61\u7c7b\u578b\u3001\u8c03\u6574\u51fd\u6570\u53c2\u6570\u683c\u5f0f\u7b49\u573a\u666f\u65f6&#xff0c;\u5f80\u5f80\u9762\u4e34\u8bf8\u591a\u63a3\u8098&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u89c4\u5219\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u4eff\u51fd\u6570\u7c7b&#xff0c;\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u9700\u8981\u590d\u6742\u7684\u6a21\u677f\u7279\u5316&#xff0c;\u8c03\u6574\u51fd\u6570\u53c2\u6570\u987a\u5e8f\u5219\u9700\u8981\u91cd\u5199\u51fd\u6570\u91cd\u8f7d\u7248\u672c\u3002\u8fd9\u4e9b\u7e41\u7410\u7684\u64cd\u4f5c\u4e0d\u4ec5\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u5197\u4f59\u5ea6&#xff0c;\u8fd8\u964d\u4f4e\u4e86\u5f00\u53d1\u6548\u7387\u4e0e\u53ef\u8bfb\u6027\u3002<\/p>\n<p>C&#043;&#043;11 \u5f15\u5165\u7684 Lambda \u8868\u8fbe\u5f0f\u3001std::function \u5305\u88c5\u5668\u4e0e std::bind \u9002\u914d\u5668&#xff0c;\u5f7b\u5e95\u6539\u53d8\u4e86\u8fd9\u4e00\u5c40\u9762\u3002\u5b83\u4eec\u4f5c\u4e3a\u73b0\u4ee3 C&#043;&#043; \u4e2d\u5904\u7406\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u6838\u5fc3\u5de5\u5177&#xff0c;\u5206\u522b\u4ece\u8f7b\u91cf\u7ea7\u51fd\u6570\u5b9a\u4e49\u3001\u53ef\u8c03\u7528\u5bf9\u8c61\u7c7b\u578b\u7edf\u4e00\u3001\u51fd\u6570\u53c2\u6570\u9002\u914d\u4e09\u4e2a\u7ef4\u5ea6&#xff0c;\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u7b80\u6d01\u3001\u7075\u6d3b\u3001\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<h3>\u4e00\u3001Lambda \u8868\u8fbe\u5f0f&#xff1a;\u8f7b\u91cf\u7ea7\u533f\u540d\u51fd\u6570\u5bf9\u8c61\u7684\u4f18\u96c5\u5b9e\u73b0<\/h3>\n<p>Lambda \u8868\u8fbe\u5f0f\u662f C&#043;&#043;11 \u5f15\u5165\u7684\u533f\u540d\u51fd\u6570\u5bf9\u8c61&#xff0c;\u5176\u6838\u5fc3\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8&#xff0c;\u65e0\u9700\u50cf\u666e\u901a\u51fd\u6570\u90a3\u6837\u5728\u5168\u5c40\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u58f0\u660e&#xff0c;\u540c\u65f6\u652f\u6301\u6355\u6349\u5916\u5c42\u4f5c\u7528\u57df\u7684\u53d8\u91cf&#xff0c;\u6781\u5927\u5730\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u4e0e\u7075\u6d3b\u6027\u3002\u4ece\u5e95\u5c42\u539f\u7406\u6765\u770b&#xff0c;Lambda \u8868\u8fbe\u5f0f\u5e76\u975e\u4e00\u79cd\u5168\u65b0\u7684\u8bed\u8a00\u7279\u6027&#xff0c;\u800c\u662f\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7684\u4eff\u51fd\u6570\u7c7b\u7684 \u201c\u8bed\u6cd5\u7cd6\u201d&#xff0c;\u4f46\u8fd9\u5c42\u8bed\u6cd5\u7cd6\u5374\u8ba9\u5f00\u53d1\u8005\u6446\u8131\u4e86\u624b\u52a8\u5b9a\u4e49\u4eff\u51fd\u6570\u7684\u7e41\u7410\u6d41\u7a0b\u3002<\/p>\n<h4>1.1 Lambda \u8868\u8fbe\u5f0f\u7684\u5b8c\u6574\u8bed\u6cd5\u7ed3\u6784<\/h4>\n<p>Lambda \u8868\u8fbe\u5f0f\u7684\u6807\u51c6\u8bed\u6cd5\u683c\u5f0f\u4e3a&#xff1a;[capture-list] (parameters) -&gt; return-type { function-body }\u3002\u8fd9\u4e94\u4e2a\u90e8\u5206\u5404\u53f8\u5176\u804c&#xff0c;\u5171\u540c\u6784\u6210\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684 Lambda \u8868\u8fbe\u5f0f&#xff0c;\u5176\u4e2d\u90e8\u5206\u7ec4\u4ef6\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u53ef\u4ee5\u7701\u7565\u3002<\/p>\n<h5>1.1.1 \u6355\u6349\u5217\u8868 [capture-list]&#xff1a;Lambda \u7684\u6838\u5fc3\u6807\u8bc6\u4e0e\u53d8\u91cf\u6355\u6349\u5165\u53e3<\/h5>\n<p>\u6355\u6349\u5217\u8868\u662f Lambda \u8868\u8fbe\u5f0f\u7684\u8d77\u59cb\u6807\u8bc6&#xff0c;\u4e5f\u662f\u5176\u6700\u6838\u5fc3\u3001\u6700\u6613\u51fa\u9519\u7684\u90e8\u5206&#xff0c;\u5176\u4f5c\u7528\u662f\u6355\u6349 Lambda \u5b9a\u4e49\u4f4d\u7f6e\u7684\u5916\u5c42\u4f5c\u7528\u57df\u5c40\u90e8\u53d8\u91cf&#xff0c;\u4f9b\u51fd\u6570\u4f53\u5185\u90e8\u4f7f\u7528\u3002\u6355\u6349\u5217\u8868\u7684\u4f7f\u7528\u9075\u5faa\u4e25\u683c\u7684\u89c4\u5219&#xff0c;\u5177\u4f53\u53ef\u5206\u4e3a\u57fa\u7840\u89c4\u5219\u4e0e\u6355\u6349\u65b9\u5f0f\u4e24\u5927\u6a21\u5757\u3002<\/p>\n<p>\u57fa\u7840\u89c4\u5219<\/p>\n<ul>\n<li>\u6355\u6349\u5217\u8868\u5fc5\u987b\u4f5c\u4e3a Lambda \u8868\u8fbe\u5f0f\u7684\u5f00\u5934&#xff0c;\u54ea\u6015\u4e0d\u9700\u8981\u6355\u6349\u4efb\u4f55\u53d8\u91cf&#xff0c;\u7a7a\u7684 [] \u4e5f\u4e0d\u80fd\u7701\u7565 \u2014\u2014 \u8fd9\u662f\u7f16\u8bd1\u5668\u8bc6\u522b Lambda \u8868\u8fbe\u5f0f\u7684\u5173\u952e\u6807\u5fd7\u3002<\/li>\n<li>\u6355\u6349\u5217\u8868\u53ea\u80fd\u6355\u6349 Lambda \u5b9a\u4e49\u4f4d\u7f6e\u4e4b\u524d \u7684\u5c40\u90e8\u53d8\u91cf&#xff0c;\u65e0\u6cd5\u6355\u6349\u5b9a\u4e49\u4e4b\u540e\u58f0\u660e\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u8fd9\u662f\u56e0\u4e3a\u7f16\u8bd1\u5668\u7684\u7f16\u8bd1\u6d41\u7a0b\u662f\u81ea\u4e0a\u800c\u4e0b\u7684&#xff0c;\u540e\u7eed\u58f0\u660e\u7684\u53d8\u91cf\u5728 Lambda \u5b9a\u4e49\u65f6\u8fd8\u672a\u8fdb\u5165\u4f5c\u7528\u57df\u3002<\/li>\n<li>\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u4e0d\u9700\u8981\u6355\u6349&#xff0c;\u53ef\u76f4\u63a5\u5728 Lambda \u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u3002\u539f\u56e0\u5728\u4e8e\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u8d2f\u7a7f\u6574\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u671f&#xff0c;\u4f5c\u7528\u57df\u8986\u76d6\u5176\u5b9a\u4e49\u4f4d\u7f6e\u4e4b\u540e\u7684\u6240\u6709\u4ee3\u7801&#xff1b;\u5168\u5c40\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u5219\u8986\u76d6\u6574\u4e2a\u7a0b\u5e8f&#xff0c;\u4e8c\u8005\u90fd\u4e0d\u53d7\u5c40\u90e8\u4f5c\u7528\u57df\u7684\u9650\u5236\u3002<\/li>\n<li>\u5982\u679c Lambda \u8868\u8fbe\u5f0f\u5b9a\u4e49\u5728\u5168\u5c40\u4f5c\u7528\u57df&#xff08;\u800c\u975e\u51fd\u6570\u5185\u90e8&#xff09;&#xff0c;\u6355\u6349\u5217\u8868\u5fc5\u987b\u4e3a\u7a7a\u3002\u56e0\u4e3a\u5168\u5c40\u4f5c\u7528\u57df\u4e0d\u5b58\u5728 \u201c\u5916\u5c42\u5c40\u90e8\u53d8\u91cf\u201d&#xff0c;\u6ca1\u6709\u53ef\u6355\u6349\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u4e09\u79cd\u6355\u6349\u65b9\u5f0f\u6355\u6349\u5217\u8868\u652f\u6301\u4e09\u79cd\u6355\u6349\u65b9\u5f0f&#xff0c;\u5206\u522b\u5bf9\u5e94\u4e0d\u540c\u7684\u4f7f\u7528\u573a\u666f&#xff0c;\u5f00\u53d1\u8005\u53ef\u6839\u636e\u9700\u6c42\u7075\u6d3b\u9009\u62e9\u3002<\/p>\n<p>\u65b9\u5f0f\u4e00&#xff1a;\u663e\u5f0f\u6355\u6349 \u2014\u2014 \u7cbe\u51c6\u63a7\u5236\u5355\u4e2a\u53d8\u91cf\u7684\u8bbf\u95ee\u6743\u9650\u663e\u5f0f\u6355\u6349\u662f\u6307\u5728\u6355\u6349\u5217\u8868\u4e2d\u660e\u786e\u6307\u5b9a\u6bcf\u4e2a\u53d8\u91cf\u7684\u6355\u6349\u7c7b\u578b&#xff0c;\u591a\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7528\u9017\u53f7\u5206\u9694\u3002\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u7cbe\u51c6\u63a7\u5236\u6bcf\u4e2a\u53d8\u91cf\u7684\u8bbf\u95ee\u6743\u9650&#xff0c;\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u4e0d\u540c\u53d8\u91cf\u91c7\u53d6\u4e0d\u540c\u6355\u6349\u7b56\u7565\u7684\u573a\u666f\u3002\u663e\u5f0f\u6355\u6349\u7684\u6838\u5fc3\u8bed\u6cd5\u89c4\u5219\u662f&#xff1a;\u53d8\u91cf\u540d\u524d\u4e0d\u52a0 &amp; \u8868\u793a\u503c\u6355\u6349&#xff0c;\u52a0 &amp; \u8868\u793a\u5f15\u7528\u6355\u6349\u3002<\/p>\n<ul>\n<li>\u503c\u6355\u6349\u7684\u672c\u8d28\u662f\u62f7\u8d1d\u5916\u5c42\u53d8\u91cf\u7684\u526f\u672c\u5230 Lambda \u8868\u8fbe\u5f0f\u5185\u90e8\u3002\u7531\u4e8e\u662f\u526f\u672c&#xff0c;Lambda \u51fd\u6570\u4f53\u5185\u90e8\u5bf9\u8be5\u53d8\u91cf\u7684\u4fee\u6539\u4e0d\u4f1a\u5f71\u54cd\u5916\u5c42\u539f\u53d8\u91cf\u3002\u540c\u65f6&#xff0c;\u503c\u6355\u6349\u7684\u53d8\u91cf\u9ed8\u8ba4\u662f\u53ea\u8bfb\u7684&#xff0c;\u65e0\u6cd5\u5728\u51fd\u6570\u4f53\u5185\u90e8\u4fee\u6539 \u2014\u2014 \u8fd9\u662f\u56e0\u4e3a\u7f16\u8bd1\u5668\u751f\u6210\u7684\u4eff\u51fd\u6570\u7c7b\u4e2d&#xff0c;\u503c\u6355\u6349\u7684\u53d8\u91cf\u4f1a\u88ab\u58f0\u660e\u4e3a\u666e\u901a\u6210\u5458\u53d8\u91cf&#xff0c;\u800c\u4eff\u51fd\u6570\u7684 operator () \u8fd0\u7b97\u7b26\u9ed8\u8ba4\u662f const \u6210\u5458\u51fd\u6570&#xff0c;const \u6210\u5458\u51fd\u6570\u65e0\u6cd5\u4fee\u6539\u7c7b\u7684\u666e\u901a\u6210\u5458\u53d8\u91cf\u3002<\/li>\n<li>\u5f15\u7528\u6355\u6349\u7684\u672c\u8d28\u662f\u4e3a\u5916\u5c42\u53d8\u91cf\u5efa\u7acb\u4e00\u4e2a\u5f15\u7528\u522b\u540d&#xff0c;Lambda \u51fd\u6570\u4f53\u5185\u90e8\u5bf9\u8be5\u53d8\u91cf\u7684\u6240\u6709\u64cd\u4f5c&#xff0c;\u90fd\u662f\u76f4\u63a5\u4f5c\u7528\u4e8e\u5916\u5c42\u539f\u53d8\u91cf\u3002\u56e0\u6b64&#xff0c;\u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539&#xff0c;\u4e14\u4fee\u6539\u7ed3\u679c\u4f1a\u540c\u6b65\u5230\u5916\u5c42\u4f5c\u7528\u57df\u3002<\/li>\n<\/ul>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u663e\u5f0f\u6355\u6349\u7684\u53d8\u91cf\u5fc5\u987b\u662f Lambda \u5b9a\u4e49\u4f4d\u7f6e\u4e4b\u524d\u5df2\u58f0\u660e\u7684\u5c40\u90e8\u53d8\u91cf&#xff0c;\u672a\u88ab\u6355\u6349\u7684\u53d8\u91cf\u82e5\u5728\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528&#xff0c;\u4f1a\u76f4\u63a5\u89e6\u53d1\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u663e\u5f0f\u6355\u6349<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    int a &#061; 10;<br \/>\n    int b &#061; 20;<\/p>\n<p>    \/\/ \u663e\u5f0f\u503c\u6355\u6349 a&#xff0c;\u5f15\u7528\u6355\u6349 b<br \/>\n    auto lambda &#061; [a, &amp;b]() {<br \/>\n        \/\/ a &#061; 100; \/\/ \u7f16\u8bd1\u9519\u8bef&#xff1a;\u503c\u6355\u6349\u7684\u53d8\u91cf\u9ed8\u8ba4\u53ea\u8bfb<br \/>\n        b &#061; 200;   \/\/ \u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u53ef\u4fee\u6539&#xff0c;\u4f1a\u5f71\u54cd\u5916\u5c42\u53d8\u91cf<br \/>\n        cout &lt;&lt; &#034;Lambda \u5185\u90e8: a &#061; &#034; &lt;&lt; a &lt;&lt; &#034;, b &#061; &#034; &lt;&lt; b &lt;&lt; endl;<br \/>\n    };<\/p>\n<p>    lambda();<br \/>\n    cout &lt;&lt; &#034;Lambda \u5916\u90e8: a &#061; &#034; &lt;&lt; a &lt;&lt; &#034;, b &#061; &#034; &lt;&lt; b &lt;&lt; endl; \/\/ a&#061;10, b&#061;200<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u65b9\u5f0f\u4e8c&#xff1a;\u9690\u5f0f\u6355\u6349 \u2014\u2014 \u7f16\u8bd1\u5668\u81ea\u52a8\u6355\u6349\u6240\u9700\u53d8\u91cf\u5f53\u9700\u8981\u6355\u6349\u7684\u53d8\u91cf\u6570\u91cf\u8f83\u591a&#xff0c;\u4e14\u6240\u6709\u53d8\u91cf\u7684\u6355\u6349\u65b9\u5f0f\u4e00\u81f4\u65f6&#xff0c;\u663e\u5f0f\u6355\u6349\u4f1a\u663e\u5f97\u5197\u4f59\u3002\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u9690\u5f0f\u6355\u6349&#xff0c;\u5373\u901a\u8fc7 &#061; \u6216 &amp; \u6307\u5b9a\u9ed8\u8ba4\u6355\u6349\u89c4\u5219&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u6355\u6349\u51fd\u6570\u4f53\u4e2d\u7528\u5230\u7684\u6240\u6709\u5916\u5c42\u5c40\u90e8\u53d8\u91cf&#xff0c;\u672a\u7528\u5230\u7684\u53d8\u91cf\u4e0d\u4f1a\u88ab\u6355\u6349&#xff0c;\u4ece\u800c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6027\u80fd\u5f00\u9500\u3002\u9690\u5f0f\u6355\u6349\u7684\u6838\u5fc3\u8bed\u6cd5\u89c4\u5219\u662f&#xff1a;<\/p>\n<ul>\n<li>[&#061;] \u8868\u793a\u9690\u5f0f\u503c\u6355\u6349&#xff0c;\u51fd\u6570\u4f53\u4e2d\u7528\u5230\u7684\u6240\u6709\u5916\u5c42\u5c40\u90e8\u53d8\u91cf&#xff0c;\u90fd\u4ee5\u503c\u6355\u6349\u7684\u65b9\u5f0f\u83b7\u53d6\u526f\u672c\u3002\u503c\u6355\u6349\u7684\u53d8\u91cf\u540c\u6837\u9ed8\u8ba4\u53ea\u8bfb&#xff0c;\u65e0\u6cd5\u5728\u51fd\u6570\u4f53\u5185\u90e8\u4fee\u6539\u3002<\/li>\n<li>[&amp;] \u8868\u793a\u9690\u5f0f\u5f15\u7528\u6355\u6349&#xff0c;\u51fd\u6570\u4f53\u4e2d\u7528\u5230\u7684\u6240\u6709\u5916\u5c42\u5c40\u90e8\u53d8\u91cf&#xff0c;\u90fd\u4ee5\u5f15\u7528\u6355\u6349\u7684\u65b9\u5f0f\u5173\u8054\u539f\u53d8\u91cf\u3002\u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539&#xff0c;\u4e14\u4fee\u6539\u7ed3\u679c\u4f1a\u540c\u6b65\u5230\u5916\u5c42\u4f5c\u7528\u57df\u3002<\/li>\n<\/ul>\n<p>\u9690\u5f0f\u6355\u6349\u7684\u4f18\u52bf\u5728\u4e8e\u7b80\u5316\u4ee3\u7801\u4e66\u5199&#xff0c;\u540c\u65f6\u4fdd\u8bc1\u6355\u6349\u7684\u7cbe\u51c6\u6027 \u2014\u2014 \u7f16\u8bd1\u5668\u53ea\u4f1a\u6355\u6349\u51fd\u6570\u4f53\u4e2d\u5b9e\u9645\u7528\u5230\u7684\u53d8\u91cf&#xff0c;\u4e0d\u4f1a\u591a\u4f59\u6355\u6349\u672a\u4f7f\u7528\u7684\u53d8\u91cf\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u9690\u5f0f\u6355\u6349<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    int x &#061; 5, y &#061; 8, z &#061; 10;<\/p>\n<p>    \/\/ \u9690\u5f0f\u503c\u6355\u6349&#xff1a;\u53ea\u6355\u6349\u7528\u5230\u7684 x \u548c y&#xff0c;z \u4e0d\u4f1a\u88ab\u6355\u6349<br \/>\n    auto lambda1 &#061; [&#061;]() {<br \/>\n        \/\/ x &#061; 100; \/\/ \u7f16\u8bd1\u9519\u8bef&#xff1a;\u503c\u6355\u6349\u9ed8\u8ba4\u53ea\u8bfb<br \/>\n        cout &lt;&lt; &#034;lambda1: x &#061; &#034; &lt;&lt; x &lt;&lt; &#034;, y &#061; &#034; &lt;&lt; y &lt;&lt; endl;<br \/>\n    };<\/p>\n<p>    \/\/ \u9690\u5f0f\u5f15\u7528\u6355\u6349&#xff1a;\u4fee\u6539\u4f1a\u5f71\u54cd\u5916\u5c42\u53d8\u91cf<br \/>\n    auto lambda2 &#061; [&amp;]() {<br \/>\n        x &#061; 100;<br \/>\n        y &#061; 200;<br \/>\n        cout &lt;&lt; &#034;lambda2: x &#061; &#034; &lt;&lt; x &lt;&lt; &#034;, y &#061; &#034; &lt;&lt; y &lt;&lt; endl;<br \/>\n    };<\/p>\n<p>    lambda1(); \/\/ \u8f93\u51fa&#xff1a;lambda1: x &#061; 5, y &#061; 8<br \/>\n    lambda2(); \/\/ \u8f93\u51fa&#xff1a;lambda2: x &#061; 100, y &#061; 200<br \/>\n    cout &lt;&lt; &#034;\u5916\u90e8: x &#061; &#034; &lt;&lt; x &lt;&lt; &#034;, y &#061; &#034; &lt;&lt; y &lt;&lt; &#034;, z &#061; &#034; &lt;&lt; z &lt;&lt; endl; \/\/ x&#061;100, y&#061;200, z&#061;10<br \/>\n    return 0;<br \/>\n}<\/p>\n<p>\u65b9\u5f0f\u4e09&#xff1a;\u6df7\u5408\u6355\u6349 \u2014\u2014 \u9690\u5f0f\u4e0e\u663e\u5f0f\u7684\u7ed3\u5408\u4f7f\u7528\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u5e38\u5e38\u4f1a\u9047\u5230 \u201c\u5927\u90e8\u5206\u53d8\u91cf\u91c7\u7528\u540c\u4e00\u79cd\u6355\u6349\u65b9\u5f0f&#xff0c;\u4e2a\u522b\u53d8\u91cf\u9700\u8981\u7279\u6b8a\u5904\u7406\u201d \u7684\u573a\u666f&#xff0c;\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528\u6df7\u5408\u6355\u6349&#xff0c;\u5373\u9690\u5f0f\u6355\u6349\u4e0e\u663e\u5f0f\u6355\u6349\u7ed3\u5408\u4f7f\u7528\u3002\u6df7\u5408\u6355\u6349\u9075\u5faa\u4e25\u683c\u7684\u8bed\u6cd5\u89c4\u5219&#xff0c;\u82e5\u8fdd\u53cd\u89c4\u5219\u4f1a\u76f4\u63a5\u89e6\u53d1\u7f16\u8bd1\u9519\u8bef\u3002\u6df7\u5408\u6355\u6349\u7684\u6838\u5fc3\u89c4\u5219&#xff1a;<\/p>\n<ul>\n<li>\u6355\u6349\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5fc5\u987b\u662f &#061; \u6216 &amp;&#xff0c;\u7528\u4e8e\u6307\u5b9a\u9ed8\u8ba4\u7684\u6355\u6349\u65b9\u5f0f\u3002<\/li>\n<li>\u82e5\u9ed8\u8ba4\u6355\u6349\u65b9\u5f0f\u662f &#061;&#xff08;\u9690\u5f0f\u503c\u6355\u6349&#xff09;&#xff0c;\u540e\u7eed\u663e\u5f0f\u6307\u5b9a\u7684\u53d8\u91cf\u5fc5\u987b\u91c7\u7528\u5f15\u7528\u6355\u6349&#xff08;\u53d8\u91cf\u540d\u524d\u52a0 &amp;&#xff09;&#xff0c;\u4e0d\u80fd\u518d\u4f7f\u7528\u503c\u6355\u6349\u3002\u8fd9\u662f\u4e3a\u4e86\u907f\u514d\u8bed\u6cd5\u6b67\u4e49&#xff0c;\u660e\u786e\u533a\u5206\u9ed8\u8ba4\u6355\u6349\u4e0e\u7279\u6b8a\u6355\u6349\u7684\u53d8\u91cf\u3002<\/li>\n<li>\u82e5\u9ed8\u8ba4\u6355\u6349\u65b9\u5f0f\u662f &amp;&#xff08;\u9690\u5f0f\u5f15\u7528\u6355\u6349&#xff09;&#xff0c;\u540e\u7eed\u663e\u5f0f\u6307\u5b9a\u7684\u53d8\u91cf\u5fc5\u987b\u91c7\u7528\u503c\u6355\u6349&#xff08;\u53d8\u91cf\u540d\u524d\u4e0d\u52a0 &amp;&#xff09;&#xff0c;\u4e0d\u80fd\u518d\u4f7f\u7528\u5f15\u7528\u6355\u6349\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982&#xff0c;[&#061;, &amp;a] \u8868\u793a\u9ed8\u8ba4\u5bf9\u6240\u6709\u7528\u5230\u7684\u53d8\u91cf\u8fdb\u884c\u503c\u6355\u6349&#xff0c;\u552f\u72ec\u53d8\u91cf a \u91c7\u7528\u5f15\u7528\u6355\u6349&#xff1b;[&amp;, b] \u8868\u793a\u9ed8\u8ba4\u5bf9\u6240\u6709\u7528\u5230\u7684\u53d8\u91cf\u8fdb\u884c\u5f15\u7528\u6355\u6349&#xff0c;\u552f\u72ec\u53d8\u91cf b \u91c7\u7528\u503c\u6355\u6349\u3002\u6df7\u5408\u6355\u6349\u517c\u987e\u4e86\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u4e0e\u53d8\u91cf\u63a7\u5236\u7684\u7cbe\u51c6\u6027&#xff0c;\u662f\u5b9e\u9645\u5f00\u53d1\u4e2d\u4f7f\u7528\u9891\u7387\u8f83\u9ad8\u7684\u6355\u6349\u65b9\u5f0f\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u6df7\u5408\u6355\u6349<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    int m &#061; 1, n &#061; 2, p &#061; 3;<\/p>\n<p>    \/\/ \u9ed8\u8ba4\u503c\u6355\u6349&#xff0c;p \u5355\u72ec\u5f15\u7528\u6355\u6349<br \/>\n    auto lambda1 &#061; [&#061;, &amp;p]() {<br \/>\n        \/\/ m &#061; 10; \/\/ \u7f16\u8bd1\u9519\u8bef&#xff1a;\u9ed8\u8ba4\u503c\u6355\u6349\u53ea\u8bfb<br \/>\n        p &#061; 30;   \/\/ p \u662f\u5f15\u7528\u6355\u6349&#xff0c;\u53ef\u4fee\u6539<br \/>\n        cout &lt;&lt; &#034;lambda1: m &#061; &#034; &lt;&lt; m &lt;&lt; &#034;, n &#061; &#034; &lt;&lt; n &lt;&lt; &#034;, p &#061; &#034; &lt;&lt; p &lt;&lt; endl;<br \/>\n    };<\/p>\n<p>    \/\/ \u9ed8\u8ba4\u5f15\u7528\u6355\u6349&#xff0c;n \u5355\u72ec\u503c\u6355\u6349<br \/>\n    auto lambda2 &#061; [&amp;, n]() {<br \/>\n        m &#061; 10;   \/\/ \u5f15\u7528\u6355\u6349\u53ef\u4fee\u6539<br \/>\n        \/\/ n &#061; 20; \/\/ \u7f16\u8bd1\u9519\u8bef&#xff1a;n \u662f\u503c\u6355\u6349\u53ea\u8bfb<br \/>\n        cout &lt;&lt; &#034;lambda2: m &#061; &#034; &lt;&lt; m &lt;&lt; &#034;, n &#061; &#034; &lt;&lt; n &lt;&lt; &#034;, p &#061; &#034; &lt;&lt; p &lt;&lt; endl;<br \/>\n    };<\/p>\n<p>    lambda1(); \/\/ \u8f93\u51fa&#xff1a;lambda1: m &#061; 1, n &#061; 2, p &#061; 30<br \/>\n    lambda2(); \/\/ \u8f93\u51fa&#xff1a;lambda2: m &#061; 10, n &#061; 2, p &#061; 30<br \/>\n    cout &lt;&lt; &#034;\u5916\u90e8: m &#061; &#034; &lt;&lt; m &lt;&lt; &#034;, n &#061; &#034; &lt;&lt; n &lt;&lt; &#034;, p &#061; &#034; &lt;&lt; p &lt;&lt; endl; \/\/ m&#061;10, n&#061;2, p&#061;30<br \/>\n    return 0;<br \/>\n}<\/p>\n<h5>1.1.2 \u53c2\u6570\u5217\u8868 (parameters)&#xff1a;\u4e0e\u666e\u901a\u51fd\u6570\u4e00\u81f4\u7684\u53c2\u6570\u63a5\u6536\u5165\u53e3<\/h5>\n<p>\u53c2\u6570\u5217\u8868\u7684\u4f5c\u7528\u662f\u63a5\u6536\u5916\u90e8\u4f20\u5165\u7684\u53c2\u6570&#xff0c;\u5176\u8bed\u6cd5\u683c\u5f0f\u4e0e\u666e\u901a\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u5b8c\u5168\u4e00\u81f4&#xff0c;\u652f\u6301\u666e\u901a\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u7b49\u7279\u6027\u3002\u53c2\u6570\u5217\u8868\u7684\u7701\u7565\u89c4\u5219\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c Lambda \u8868\u8fbe\u5f0f\u4e0d\u9700\u8981\u63a5\u6536\u5916\u90e8\u53c2\u6570&#xff0c;\u53c2\u6570\u5217\u8868\u53ef\u4ee5\u8fde\u540c\u62ec\u53f7 () \u4e00\u8d77\u7701\u7565\u3002\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u7b80\u5355\u7684\u6253\u5370\u51fd\u6570\u53ef\u4ee5\u5199\u6210 [] { cout &lt;&lt; &#034;Hello Lambda&#034; &lt;&lt; endl; }\u3002<\/li>\n<li>\u5982\u679c Lambda \u8868\u8fbe\u5f0f\u4f7f\u7528\u4e86 mutable \u4fee\u9970\u7b26&#xff0c;\u5373\u4f7f\u6ca1\u6709\u53c2\u6570&#xff0c;\u53c2\u6570\u5217\u8868\u7684\u62ec\u53f7 () \u4e5f\u4e0d\u80fd\u7701\u7565\u3002\u8fd9\u662f mutable \u4fee\u9970\u7b26\u7684\u5f3a\u5236\u8981\u6c42&#xff0c;\u76ee\u7684\u662f\u660e\u786e\u533a\u5206\u4fee\u9970\u7b26\u7684\u4f5c\u7528\u8303\u56f4\u3002<\/li>\n<\/ul>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u53c2\u6570\u5217\u8868\u4f7f\u7528<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u7701\u7565\u53c2\u6570\u5217\u8868&#xff1a;\u65e0\u53c2\u6570<br \/>\n    auto print &#061; [] { cout &lt;&lt; &#034;Hello Lambda!&#034; &lt;&lt; endl; };<br \/>\n    print();<\/p>\n<p>    \/\/ \u5e26\u53c2\u6570\u5217\u8868&#xff1a;\u666e\u901a\u53c2\u6570 &#043; \u9ed8\u8ba4\u53c2\u6570<br \/>\n    auto add &#061; [](int a, int b &#061; 10) { return a &#043; b; };<br \/>\n    cout &lt;&lt; &#034;add(5) &#061; &#034; &lt;&lt; add(5) &lt;&lt; endl;    \/\/ \u8f93\u51fa&#xff1a;15<br \/>\n    cout &lt;&lt; &#034;add(5, 20) &#061; &#034; &lt;&lt; add(5, 20) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;25<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>1.1.3 \u8fd4\u56de\u503c\u7c7b\u578b -&gt; return-type&#xff1a;\u53ef\u63a8\u5bfc\u7684\u7c7b\u578b\u58f0\u660e<\/h5>\n<p>\u8fd4\u56de\u503c\u7c7b\u578b\u7528\u4e8e\u6307\u5b9a Lambda \u8868\u8fbe\u5f0f\u7684\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u91c7\u7528 C&#043;&#043;11 \u5f15\u5165\u7684 \u201c\u8ffd\u8e2a\u8fd4\u56de\u7c7b\u578b\u201d \u8bed\u6cd5&#xff0c;\u5373\u8fd4\u56de\u503c\u7c7b\u578b\u5199\u5728\u53c2\u6570\u5217\u8868\u4e4b\u540e&#xff0c;\u4ee5 -&gt; \u5f00\u5934\u3002\u8fd4\u56de\u503c\u7c7b\u578b\u7684\u7701\u7565\u89c4\u5219\u8f83\u4e3a\u7075\u6d3b&#xff0c;\u5177\u4f53\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c Lambda \u8868\u8fbe\u5f0f\u6ca1\u6709\u8fd4\u56de\u503c&#xff08;\u51fd\u6570\u4f53\u4e2d\u65e0 return \u8bed\u53e5&#xff0c;\u6216 return \u8bed\u53e5\u65e0\u8fd4\u56de\u503c&#xff09;&#xff0c;\u8fd4\u56de\u503c\u7c7b\u578b\u90e8\u5206\u53ef\u4ee5\u76f4\u63a5\u7701\u7565&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u9ed8\u8ba4\u5c06\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u5bfc\u4e3a void\u3002<\/li>\n<li>\u5982\u679c Lambda \u8868\u8fbe\u5f0f\u6709\u8fd4\u56de\u503c&#xff0c;\u4e14\u6240\u6709 return \u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b\u4e00\u81f4&#xff0c;\u8fd4\u56de\u503c\u7c7b\u578b\u90e8\u5206\u53ef\u4ee5\u7701\u7565&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u63a8\u5bfc\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u4f8b\u5982&#xff0c;[](int a, int b) { return a &#043; b; } \u4f1a\u88ab\u7f16\u8bd1\u5668\u81ea\u52a8\u63a8\u5bfc\u4e3a\u8fd4\u56de int \u7c7b\u578b\u3002<\/li>\n<li>\u5982\u679c Lambda \u8868\u8fbe\u5f0f\u6709\u591a\u4e2a return \u8bed\u53e5&#xff0c;\u4e14\u8fd4\u56de\u7c7b\u578b\u4e0d\u540c&#xff08;\u4f8b\u5982\u4e00\u4e2a return \u8fd4\u56de int \u7c7b\u578b&#xff0c;\u53e6\u4e00\u4e2a return \u8fd4\u56de double \u7c7b\u578b&#xff09;&#xff0c;\u5fc5\u987b\u663e\u5f0f\u6307\u5b9a\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u5426\u5219\u7f16\u8bd1\u5668\u65e0\u6cd5\u5b8c\u6210\u7c7b\u578b\u63a8\u5bfc&#xff0c;\u4f1a\u89e6\u53d1\u7f16\u8bd1\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u8fd4\u56de\u503c\u7c7b\u578b\u63a8\u5bfc<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u65e0\u8fd4\u56de\u503c&#xff1a;\u7701\u7565\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u9ed8\u8ba4 void<br \/>\n    auto func1 &#061; []() { cout &lt;&lt; &#034;\u65e0\u8fd4\u56de\u503c Lambda&#034; &lt;&lt; endl; };<br \/>\n    func1();<\/p>\n<p>    \/\/ \u5355\u4e00\u8fd4\u56de\u7c7b\u578b&#xff1a;\u7701\u7565\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u81ea\u52a8\u63a8\u5bfc\u4e3a int<br \/>\n    auto func2 &#061; [](int a, int b) { return a * b; };<br \/>\n    cout &lt;&lt; &#034;func2(3,4) &#061; &#034; &lt;&lt; func2(3,4) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;12<\/p>\n<p>    \/\/ \u591a\u8fd4\u56de\u7c7b\u578b&#xff1a;\u5fc5\u987b\u663e\u5f0f\u6307\u5b9a\u8fd4\u56de\u503c\u7c7b\u578b<br \/>\n    auto func3 &#061; [](int a) -&gt; double {<br \/>\n        if (a &gt; 0) return 3.14;<br \/>\n        else return 10; \/\/ 10 \u4f1a\u88ab\u8f6c\u6362\u4e3a double \u7c7b\u578b<br \/>\n    };<br \/>\n    cout &lt;&lt; &#034;func3(5) &#061; &#034; &lt;&lt; func3(5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;3.14<br \/>\n    cout &lt;&lt; &#034;func3(-5) &#061; &#034; &lt;&lt; func3(-5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;10<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>1.1.4 \u51fd\u6570\u4f53 {function-body}&#xff1a;Lambda \u7684\u6838\u5fc3\u903b\u8f91\u5b9e\u73b0<\/h5>\n<p>\u51fd\u6570\u4f53\u662f Lambda \u8868\u8fbe\u5f0f\u7684\u6838\u5fc3\u903b\u8f91\u6240\u5728&#xff0c;\u5176\u8bed\u6cd5\u683c\u5f0f\u4e0e\u666e\u901a\u51fd\u6570\u7684\u51fd\u6570\u4f53\u5b8c\u5168\u4e00\u81f4&#xff0c;\u652f\u6301\u53d8\u91cf\u5b9a\u4e49\u3001\u5faa\u73af\u3001\u5206\u652f\u3001\u51fd\u6570\u8c03\u7528\u7b49\u6240\u6709\u666e\u901a\u51fd\u6570\u652f\u6301\u7684\u8bed\u6cd5\u7ed3\u6784\u3002\u51fd\u6570\u4f53\u7684\u4f7f\u7528\u89c4\u5219\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u51fd\u6570\u4f53\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u53d8\u91cf\u5305\u62ec&#xff1a;\u53c2\u6570\u5217\u8868\u4e2d\u7684\u53c2\u6570\u3001\u6355\u6349\u5217\u8868\u4e2d\u6355\u6349\u7684\u53d8\u91cf\u3001\u5168\u5c40\u53d8\u91cf\u4e0e\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n<li>\u5373\u4f7f\u51fd\u6570\u4f53\u4e3a\u7a7a&#xff0c;\u5927\u62ec\u53f7 {} \u4e5f\u4e0d\u80fd\u7701\u7565\u3002\u8fd9\u662f Lambda \u8868\u8fbe\u5f0f\u8bed\u6cd5\u7ed3\u6784\u7684\u5f3a\u5236\u8981\u6c42&#xff0c;\u76ee\u7684\u662f\u660e\u786e\u51fd\u6570\u4f53\u7684\u8fb9\u754c\u3002<\/li>\n<\/ul>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u51fd\u6570\u4f53\u4f7f\u7528<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\nusing namespace std;<\/p>\n<p>int global_var &#061; 100; \/\/ \u5168\u5c40\u53d8\u91cf<\/p>\n<p>int main() {<br \/>\n    static int static_var &#061; 200; \/\/ \u9759\u6001\u5c40\u90e8\u53d8\u91cf<br \/>\n    vector&lt;int&gt; vec{1,2,3,4,5};<\/p>\n<p>    \/\/ \u51fd\u6570\u4f53\u5305\u542b\u5faa\u73af\u3001\u5206\u652f\u3001\u5168\u5c40\/\u9759\u6001\u53d8\u91cf\u4f7f\u7528<br \/>\n    auto process &#061; [vec]() {<br \/>\n        int sum &#061; 0;<br \/>\n        for (int num : vec) {<br \/>\n            if (num % 2 &#061;&#061; 0) {<br \/>\n                sum &#043;&#061; num &#043; global_var &#043; static_var;<br \/>\n            }<br \/>\n        }<br \/>\n        cout &lt;&lt; &#034;sum &#061; &#034; &lt;&lt; sum &lt;&lt; endl;<br \/>\n    };<\/p>\n<p>    process(); \/\/ \u8f93\u51fa&#xff1a;sum &#061; (2&#043;4) &#043; 100*2 &#043; 200*2 &#061; 6 &#043; 200 &#043; 400 &#061; 606<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>1.2 \u5173\u952e\u4fee\u9970\u7b26&#xff1a;mutable\u2014\u2014 \u89e3\u9664\u503c\u6355\u6349\u7684\u53ea\u8bfb\u9650\u5236<\/h4>\n<p>mutable \u662f Lambda \u8868\u8fbe\u5f0f\u7684\u53ef\u9009\u4fee\u9970\u7b26&#xff0c;\u5176\u6838\u5fc3\u4f5c\u7528\u662f\u53d6\u6d88\u503c\u6355\u6349\u53d8\u91cf\u7684 const \u5c5e\u6027&#xff0c;\u5141\u8bb8\u5728\u51fd\u6570\u4f53\u4e2d\u4fee\u6539\u503c\u6355\u6349\u7684\u53d8\u91cf\u3002mutable \u4fee\u9970\u7b26\u7684\u4f7f\u7528\u89c4\u5219\u4e0e\u6ce8\u610f\u4e8b\u9879\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\u4f5c\u7528\u8303\u56f4&#xff1a;mutable \u4ec5\u5bf9\u503c\u6355\u6349\u7684\u53d8\u91cf\u6709\u6548&#xff0c;\u5bf9\u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u65e0\u5f71\u54cd\u3002\u56e0\u4e3a\u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u672c\u8eab\u5c31\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539&#xff0c;\u4e0d\u9700\u8981\u989d\u5916\u7684\u4fee\u9970\u7b26\u3002<\/li>\n<li>\u6838\u5fc3\u6ce8\u610f\u70b9&#xff1a;mutable \u5141\u8bb8\u4fee\u6539\u7684\u662f\u503c\u6355\u6349\u7684\u526f\u672c&#xff0c;\u4e0d\u4f1a\u5f71\u54cd\u5916\u5c42\u539f\u53d8\u91cf\u3002\u8fd9\u662f\u56e0\u4e3a\u503c\u6355\u6349\u7684\u672c\u8d28\u662f\u62f7\u8d1d&#xff0c;\u526f\u672c\u4e0e\u539f\u53d8\u91cf\u662f\u4e24\u4e2a\u76f8\u4e92\u72ec\u7acb\u7684\u5bf9\u8c61\u3002\u4f8b\u5982&#xff0c;\u5916\u5c42\u6709\u4e00\u4e2a\u53d8\u91cf int a &#061; 10&#xff0c;Lambda \u8868\u8fbe\u5f0f\u00a0a\u00a0mutable {a &#061; 20;} \u8c03\u7528\u540e&#xff0c;\u526f\u672c a \u7684\u503c\u53d8\u4e3a 20&#xff0c;\u4f46\u5916\u5c42\u539f\u53d8\u91cf a \u7684\u503c\u4ecd\u7136\u662f 10\u3002<\/li>\n<li>\u8bed\u6cd5\u8981\u6c42&#xff1a;\u4f7f\u7528 mutable \u4fee\u9970\u7b26\u65f6&#xff0c;\u53c2\u6570\u5217\u8868\u7684\u62ec\u53f7 () \u4e0d\u80fd\u7701\u7565&#xff0c;\u5373\u4f7f Lambda \u8868\u8fbe\u5f0f\u6ca1\u6709\u53c2\u6570\u3002<\/li>\n<\/ul>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;mutable \u4fee\u9970\u7b26<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    int val &#061; 10;<\/p>\n<p>    \/\/ mutable \u4fee\u9970&#xff1a;\u5141\u8bb8\u4fee\u6539\u503c\u6355\u6349\u7684\u526f\u672c<br \/>\n    auto lambda &#061; [val]() mutable {<br \/>\n        val &#061; 100;<br \/>\n        cout &lt;&lt; &#034;Lambda \u5185\u90e8 val &#061; &#034; &lt;&lt; val &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;100<br \/>\n    };<\/p>\n<p>    lambda();<br \/>\n    cout &lt;&lt; &#034;Lambda \u5916\u90e8 val &#061; &#034; &lt;&lt; val &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;10&#xff08;\u539f\u53d8\u91cf\u672a\u53d8&#xff09;<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4>1.3 Lambda \u8868\u8fbe\u5f0f\u7684\u5e95\u5c42\u539f\u7406&#xff1a;\u7f16\u8bd1\u5668\u751f\u6210\u7684\u533f\u540d\u4eff\u51fd\u6570<\/h4>\n<p>\u4ece\u8bed\u6cd5\u5c42\u9762\u770b&#xff0c;Lambda \u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f&#xff1b;\u4f46\u4ece\u5e95\u5c42\u5b9e\u73b0\u6765\u770b&#xff0c;Lambda \u8868\u8fbe\u5f0f\u7684\u672c\u8d28\u662f\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7684\u533f\u540d\u4eff\u51fd\u6570\u7c7b\u3002\u5f53\u5f00\u53d1\u8005\u7f16\u5199\u4e00\u4e2a Lambda \u8868\u8fbe\u5f0f\u65f6&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u5728\u7f16\u8bd1\u9636\u6bb5\u81ea\u52a8\u5b8c\u6210\u4ee5\u4e0b\u5de5\u4f5c&#xff1a;<\/p>\n<li>\u751f\u6210\u552f\u4e00\u7684\u4eff\u51fd\u6570\u7c7b\u540d&#xff1a;\u7f16\u8bd1\u5668\u4f1a\u6839\u636e Lambda \u8868\u8fbe\u5f0f\u7684\u5b9a\u4e49\u4f4d\u7f6e\u3001\u884c\u53f7\u7b49\u4fe1\u606f&#xff0c;\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684\u4eff\u51fd\u6570\u7c7b\u540d&#xff08;\u4f8b\u5982 __lambda_123&#xff09;&#xff0c;\u786e\u4fdd\u4e0d\u540c\u7684 Lambda \u8868\u8fbe\u5f0f\u5bf9\u5e94\u4e0d\u540c\u7684\u7c7b\u7c7b\u578b\u3002<\/li>\n<li>\u5b9a\u4e49\u7c7b\u7684\u6210\u5458\u53d8\u91cf&#xff1a;\u6355\u6349\u5217\u8868\u4e2d\u6355\u6349\u7684\u53d8\u91cf\u4f1a\u88ab\u8f6c\u5316\u4e3a\u4eff\u51fd\u6570\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u3002\u503c\u6355\u6349\u7684\u53d8\u91cf\u5bf9\u5e94\u666e\u901a\u6210\u5458\u53d8\u91cf&#xff0c;\u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u5bf9\u5e94\u5f15\u7528\u7c7b\u578b\u6210\u5458\u53d8\u91cf\u3002<\/li>\n<li>\u5b9e\u73b0\u7c7b\u7684\u6784\u9020\u51fd\u6570&#xff1a;\u6784\u9020\u51fd\u6570\u7684\u53c2\u6570\u4e3a\u6355\u6349\u5217\u8868\u4e2d\u7684\u53d8\u91cf&#xff0c;\u7528\u4e8e\u521d\u59cb\u5316\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u3002\u503c\u6355\u6349\u7684\u53d8\u91cf\u901a\u8fc7\u62f7\u8d1d\u521d\u59cb\u5316&#xff0c;\u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u901a\u8fc7\u5f15\u7528\u521d\u59cb\u5316\u3002<\/li>\n<li>\u91cd\u8f7d operator () \u8fd0\u7b97\u7b26&#xff1a;Lambda \u8868\u8fbe\u5f0f\u7684\u53c2\u6570\u5217\u8868\u3001\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u4f53&#xff0c;\u4f1a\u88ab\u76f4\u63a5\u8f6c\u5316\u4e3a operator () \u8fd0\u7b97\u7b26\u7684\u53c2\u6570\u5217\u8868\u3001\u8fd4\u56de\u503c\u7c7b\u578b\u3001\u51fd\u6570\u4f53\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;operator () \u8fd0\u7b97\u7b26\u662f const \u6210\u5458\u51fd\u6570&#xff0c;\u8fd9\u4e5f\u662f\u503c\u6355\u6349\u53d8\u91cf\u9ed8\u8ba4\u53ea\u8bfb\u7684\u6839\u672c\u539f\u56e0\u3002<\/li>\n<li>\u5b9e\u4f8b\u5316\u4eff\u51fd\u6570\u5bf9\u8c61&#xff1a;\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u521b\u5efa\u8be5\u4eff\u51fd\u6570\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61&#xff0c;\u5f53\u8c03\u7528 Lambda \u8868\u8fbe\u5f0f\u65f6&#xff0c;\u672c\u8d28\u4e0a\u662f\u8c03\u7528\u8be5\u5bf9\u8c61\u7684 operator () \u8fd0\u7b97\u7b26\u3002<\/li>\n<p>\u5f53 Lambda \u8868\u8fbe\u5f0f\u4f7f\u7528 mutable \u4fee\u9970\u7b26\u65f6&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u53bb\u6389 operator () \u8fd0\u7b97\u7b26\u7684 const \u5c5e\u6027&#xff0c;\u4ece\u800c\u5141\u8bb8\u4fee\u6539\u503c\u6355\u6349\u7684\u6210\u5458\u53d8\u91cf\u3002\u8fd9\u4e00\u5e95\u5c42\u539f\u7406\u63ed\u793a\u4e86 Lambda \u8868\u8fbe\u5f0f\u4e0e\u4eff\u51fd\u6570\u7684\u672c\u8d28\u8054\u7cfb&#xff0c;\u4e5f\u89e3\u91ca\u4e86 Lambda \u8868\u8fbe\u5f0f\u7684\u6240\u6709\u8bed\u6cd5\u89c4\u5219\u4e0e\u7279\u6027\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;Lambda \u4e0e\u4eff\u51fd\u6570\u7b49\u4ef7\u6027<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u624b\u52a8\u5b9e\u73b0\u7684\u4eff\u51fd\u6570&#xff08;\u7b49\u4ef7\u4e8e\u7f16\u8bd1\u5668\u4e3a Lambda \u751f\u6210\u7684\u7c7b&#xff09;<br \/>\nclass MyLambda {<br \/>\nprivate:<br \/>\n    int a;       \/\/ \u503c\u6355\u6349\u7684\u53d8\u91cf -&gt; \u666e\u901a\u6210\u5458\u53d8\u91cf<br \/>\n    int&amp; b;      \/\/ \u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf -&gt; \u5f15\u7528\u6210\u5458\u53d8\u91cf<br \/>\npublic:<br \/>\n    \/\/ \u6784\u9020\u51fd\u6570&#xff1a;\u521d\u59cb\u5316\u6355\u6349\u7684\u53d8\u91cf<br \/>\n    MyLambda(int a_, int&amp; b_) : a(a_), b(b_) {}<\/p>\n<p>    \/\/ operator()&#xff1a;\u5bf9\u5e94 Lambda \u7684\u51fd\u6570\u4f53<br \/>\n    void operator()() const { \/\/ \u9ed8\u8ba4 const&#xff0c;\u5bf9\u5e94\u503c\u6355\u6349\u53ea\u8bfb<br \/>\n        \/\/ a &#061; 100; \/\/ \u7f16\u8bd1\u9519\u8bef&#xff1a;const \u6210\u5458\u51fd\u6570\u4e0d\u80fd\u4fee\u6539\u666e\u901a\u6210\u5458\u53d8\u91cf<br \/>\n        b &#061; 200;<br \/>\n        cout &lt;&lt; &#034;\u4eff\u51fd\u6570: a &#061; &#034; &lt;&lt; a &lt;&lt; &#034;, b &#061; &#034; &lt;&lt; b &lt;&lt; endl;<br \/>\n    }<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    int x &#061; 10, y &#061; 20;<\/p>\n<p>    \/\/ \u624b\u52a8\u4eff\u51fd\u6570\u8c03\u7528<br \/>\n    MyLambda func(x, y);<br \/>\n    func();<\/p>\n<p>    \/\/ \u7b49\u4ef7\u7684 Lambda \u8868\u8fbe\u5f0f<br \/>\n    auto lambda &#061; [x, &amp;y]() {<br \/>\n        \/\/ x &#061; 100; \/\/ \u7f16\u8bd1\u9519\u8bef&#xff1a;\u503c\u6355\u6349\u9ed8\u8ba4\u53ea\u8bfb<br \/>\n        y &#061; 200;<br \/>\n        cout &lt;&lt; &#034;Lambda: x &#061; &#034; &lt;&lt; x &lt;&lt; &#034;, y &#061; &#034; &lt;&lt; y &lt;&lt; endl;<br \/>\n    };<br \/>\n    lambda();<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>1.4 Lambda \u8868\u8fbe\u5f0f\u7684\u6838\u5fc3\u5e94\u7528\u573a\u666f<\/h4>\n<p>Lambda \u8868\u8fbe\u5f0f\u7684\u51fa\u73b0&#xff0c;\u6781\u5927\u5730\u7b80\u5316\u4e86 C&#043;&#043; \u4e2d\u8f7b\u91cf\u7ea7\u51fd\u6570\u903b\u8f91\u7684\u5b9e\u73b0&#xff0c;\u5176\u6838\u5fc3\u5e94\u7528\u573a\u666f\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762&#xff1a;<\/p>\n<h5>1.4.1 STL \u7b97\u6cd5\u7684\u81ea\u5b9a\u4e49\u903b\u8f91<\/h5>\n<p>STL \u4e2d\u7684\u8bb8\u591a\u7b97\u6cd5&#xff08;\u5982 sort\u3001find_if\u3001for_each \u7b49&#xff09;\u90fd\u652f\u6301\u4f20\u5165\u81ea\u5b9a\u4e49\u903b\u8f91&#xff0c;\u7528\u4e8e\u5b9e\u73b0\u4e2a\u6027\u5316\u7684\u6392\u5e8f\u3001\u67e5\u627e\u3001\u904d\u5386\u7b49\u529f\u80fd\u3002\u5728 Lambda \u8868\u8fbe\u5f0f\u51fa\u73b0\u4e4b\u524d&#xff0c;\u5b9e\u73b0\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u903b\u8f91\u9700\u8981\u624b\u52a8\u5b9a\u4e49\u4eff\u51fd\u6570\u7c7b&#xff0c;\u4ee3\u7801\u5197\u4f59\u4e14\u53ef\u8bfb\u6027\u5dee\u3002Lambda \u8868\u8fbe\u5f0f\u5219\u53ef\u4ee5\u76f4\u63a5\u5728\u7b97\u6cd5\u53c2\u6570\u4e2d\u5b9a\u4e49\u81ea\u5b9a\u4e49\u903b\u8f91&#xff0c;\u6781\u5927\u5730\u7b80\u5316\u4e86\u4ee3\u7801\u4e66\u5199\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;STL \u7b97\u6cd5\u4e2d\u4f7f\u7528 Lambda<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;algorithm&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u5546\u54c1\u7ed3\u6784\u4f53<br \/>\nstruct Goods {<br \/>\n    string name;<br \/>\n    double price;<br \/>\n    Goods(string n, double p) : name(n), price(p) {}<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    vector&lt;Goods&gt; goodsList{<br \/>\n        {&#034;\u82f9\u679c&#034;, 5.99},<br \/>\n        {&#034;\u9999\u8549&#034;, 3.50},<br \/>\n        {&#034;\u6a59\u5b50&#034;, 4.80},<br \/>\n        {&#034;\u8461\u8404&#034;, 8.99}<br \/>\n    };<\/p>\n<p>    \/\/ \u6309\u4ef7\u683c\u5347\u5e8f\u6392\u5e8f&#xff1a;\u76f4\u63a5\u4f7f\u7528 Lambda \u4f5c\u4e3a\u6392\u5e8f\u89c4\u5219<br \/>\n    sort(goodsList.begin(), goodsList.end(),<br \/>\n         [](const Goods&amp; g1, const Goods&amp; g2) {<br \/>\n             return g1.price &lt; g2.price;<br \/>\n         });<\/p>\n<p>    \/\/ \u904d\u5386\u8f93\u51fa<br \/>\n    cout &lt;&lt; &#034;\u6309\u4ef7\u683c\u5347\u5e8f\u6392\u5e8f&#xff1a;&#034; &lt;&lt; endl;<br \/>\n    for_each(goodsList.begin(), goodsList.end(),<br \/>\n             [](const Goods&amp; g) {<br \/>\n                 cout &lt;&lt; g.name &lt;&lt; &#034;: &#034; &lt;&lt; g.price &lt;&lt; endl;<br \/>\n             });<\/p>\n<p>    \/\/ \u67e5\u627e\u4ef7\u683c\u5927\u4e8e 5 \u7684\u5546\u54c1<br \/>\n    auto it &#061; find_if(goodsList.begin(), goodsList.end(),<br \/>\n                      [](const Goods&amp; g) {<br \/>\n                          return g.price &gt; 5.0;<br \/>\n                      });<br \/>\n    if (it !&#061; goodsList.end()) {<br \/>\n        cout &lt;&lt; &#034;\\\\n\u7b2c\u4e00\u4e2a\u4ef7\u683c\u5927\u4e8e 5 \u7684\u5546\u54c1&#xff1a;&#034; &lt;&lt; it-&gt;name &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>1.4.2 \u7ebf\u7a0b\u51fd\u6570\u7684\u81ea\u5b9a\u4e49\u6267\u884c\u903b\u8f91<\/h5>\n<p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d&#xff0c;\u521b\u5efa\u7ebf\u7a0b\u65f6\u9700\u8981\u4f20\u5165\u7ebf\u7a0b\u7684\u6267\u884c\u51fd\u6570\u3002\u4f7f\u7528\u666e\u901a\u51fd\u6570\u4f5c\u4e3a\u7ebf\u7a0b\u51fd\u6570\u65f6&#xff0c;\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf&#xff1b;\u4f7f\u7528\u4eff\u51fd\u6570\u5219\u9700\u8981\u5b9a\u4e49\u989d\u5916\u7684\u7c7b\u3002Lambda \u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7\u6355\u6349\u5217\u8868\u76f4\u63a5\u6355\u6349\u5916\u5c42\u5c40\u90e8\u53d8\u91cf&#xff0c;\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u7684\u6267\u884c\u903b\u8f91&#xff0c;\u65e0\u9700\u989d\u5916\u5b9a\u4e49\u7c7b\u6216\u51fd\u6570\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u7ebf\u7a0b\u4e2d\u4f7f\u7528 Lambda<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;thread&gt;<br \/>\n#include &lt;vector&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    vector&lt;thread&gt; threads;<br \/>\n    int threadCount &#061; 3;<\/p>\n<p>    \/\/ \u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u6253\u5370\u6307\u5b9a\u6b21\u6570\u7684\u4fe1\u606f<br \/>\n    for (int i &#061; 0; i &lt; threadCount; &#043;&#043;i) {<br \/>\n        threads.emplace_back([i]() { \/\/ \u6355\u6349\u5f53\u524d\u7ebf\u7a0b ID<br \/>\n            for (int j &#061; 0; j &lt; 3; &#043;&#043;j) {<br \/>\n                cout &lt;&lt; &#034;Thread &#034; &lt;&lt; i &lt;&lt; &#034; is running: &#034; &lt;&lt; j &lt;&lt; endl;<br \/>\n                \/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c<br \/>\n                this_thread::sleep_for(chrono::milliseconds(100));<br \/>\n            }<br \/>\n        });<br \/>\n    }<\/p>\n<p>    \/\/ \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210<br \/>\n    for (auto&amp; t : threads) {<br \/>\n        t.join();<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>1.4.3 \u667a\u80fd\u6307\u9488\u7684\u81ea\u5b9a\u4e49\u5220\u9664\u5668<\/h5>\n<p>C&#043;&#043; \u4e2d\u7684\u667a\u80fd\u6307\u9488&#xff08;\u5982 unique_ptr\u3001shared_ptr&#xff09;\u9ed8\u8ba4\u4f7f\u7528 delete \u8fd0\u7b97\u7b26\u91ca\u653e\u8d44\u6e90&#xff0c;\u4f46\u5bf9\u4e8e\u4e00\u4e9b\u7279\u6b8a\u8d44\u6e90&#xff08;\u5982\u52a8\u6001\u6570\u7ec4\u3001\u6587\u4ef6\u6307\u9488\u3001\u7f51\u7edc\u8fde\u63a5\u7b49&#xff09;&#xff0c;\u9700\u8981\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u3002Lambda \u8868\u8fbe\u5f0f\u53ef\u4ee5\u7b80\u6d01\u5730\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5220\u9664\u5668&#xff0c;\u65e0\u9700\u5b9a\u4e49\u989d\u5916\u7684\u51fd\u6570\u6216\u4eff\u51fd\u6570\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u667a\u80fd\u6307\u9488\u81ea\u5b9a\u4e49\u5220\u9664\u5668<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\n#include &lt;cstdio&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u7ba1\u7406\u52a8\u6001\u6570\u7ec4\u7684 unique_ptr<br \/>\n    unique_ptr&lt;int, function&lt;void(int*)&gt;&gt; arrPtr(<br \/>\n        new int[5]{1,2,3,4,5},<br \/>\n        [](int* p) { \/\/ Lambda \u4f5c\u4e3a\u5220\u9664\u5668<br \/>\n            cout &lt;&lt; &#034;\u91ca\u653e\u52a8\u6001\u6570\u7ec4&#8230;&#034; &lt;&lt; endl;<br \/>\n            delete[] p;<br \/>\n        }<br \/>\n    );<\/p>\n<p>    \/\/ 2. \u7ba1\u7406\u6587\u4ef6\u6307\u9488\u7684 shared_ptr<br \/>\n    FILE* fp &#061; fopen(&#034;test.txt&#034;, &#034;w&#034;);<br \/>\n    if (fp) {<br \/>\n        shared_ptr&lt;FILE&gt; filePtr(<br \/>\n            fp,<br \/>\n            [](FILE* p) { \/\/ Lambda \u4f5c\u4e3a\u5220\u9664\u5668<br \/>\n                cout &lt;&lt; &#034;\u5173\u95ed\u6587\u4ef6&#8230;&#034; &lt;&lt; endl;<br \/>\n                fclose(p);<br \/>\n            }<br \/>\n        );<br \/>\n        fprintf(filePtr.get(), &#034;Hello Lambda!&#034;);<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>1.4.4 \u56de\u8c03\u51fd\u6570\u7684\u7b80\u5316\u5b9e\u73b0<\/h5>\n<p>\u5728\u5f02\u6b65\u7f16\u7a0b\u6216\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u4e2d&#xff0c;\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5e38\u7528\u7684\u7f16\u7a0b\u6a21\u5f0f\u3002\u4f7f\u7528\u666e\u901a\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u65f6&#xff0c;\u4f20\u9012\u53c2\u6570\u8f83\u4e3a\u7e41\u7410&#xff1b;\u4f7f\u7528\u4eff\u51fd\u6570\u5219\u9700\u8981\u5b9a\u4e49\u989d\u5916\u7684\u7c7b\u3002Lambda \u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7\u6355\u6349\u5217\u8868\u76f4\u63a5\u6355\u6349\u6240\u9700\u7684\u53c2\u6570&#xff0c;\u7b80\u5316\u56de\u8c03\u51fd\u6570\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u56de\u8c03\u51fd\u6570\u4e2d\u4f7f\u7528 Lambda<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;string&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u6a21\u62df\u7528\u6237\u6570\u636e<br \/>\nstruct UserData {<br \/>\n    string userId;<br \/>\n    string userName;<br \/>\n};<\/p>\n<p>\/\/ \u6a21\u62df\u54cd\u5e94\u6570\u636e<br \/>\nstruct Response {<br \/>\n    int code;<br \/>\n    string data;<br \/>\n};<\/p>\n<p>\/\/ \u6a21\u62df\u5f02\u6b65\u8bf7\u6c42\u51fd\u6570<br \/>\nvoid asyncRequest(const string&amp; url, function&lt;void(const Response&amp;)&gt; callback) {<br \/>\n    \/\/ \u6a21\u62df\u7f51\u7edc\u8bf7\u6c42\u8017\u65f6<br \/>\n    this_thread::sleep_for(chrono::milliseconds(500));<\/p>\n<p>    \/\/ \u6784\u9020\u54cd\u5e94\u6570\u636e<br \/>\n    Response resp{200, &#034;Success: &#034; &#043; url};<\/p>\n<p>    \/\/ \u8c03\u7528\u56de\u8c03\u51fd\u6570<br \/>\n    callback(resp);<br \/>\n}<\/p>\n<p>\/\/ \u5904\u7406\u54cd\u5e94<br \/>\nvoid processResponse(const UserData&amp; user, const Response&amp; resp) {<br \/>\n    cout &lt;&lt; &#034;\u7528\u6237 &#034; &lt;&lt; user.userId &lt;&lt; &#034;(&#034; &lt;&lt; user.userName &lt;&lt; &#034;) \u7684\u8bf7\u6c42\u7ed3\u679c&#xff1a;&#034; &lt;&lt; endl;<br \/>\n    cout &lt;&lt; &#034;\u72b6\u6001\u7801&#xff1a;&#034; &lt;&lt; resp.code &lt;&lt; &#034;, \u6570\u636e&#xff1a;&#034; &lt;&lt; resp.data &lt;&lt; endl;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    UserData user{&#034;1001&#034;, &#034;\u5f20\u4e09&#034;};<br \/>\n    string url &#061; &#034;https:\/\/example.com\/api\/data&#034;;<\/p>\n<p>    \/\/ Lambda \u4f5c\u4e3a\u56de\u8c03\u51fd\u6570&#xff0c;\u6355\u6349 user \u53d8\u91cf<br \/>\n    asyncRequest(url, [user](const Response&amp; resp) {<br \/>\n        processResponse(user, resp);<br \/>\n    });<\/p>\n<p>    \/\/ \u7b49\u5f85\u56de\u8c03\u6267\u884c\u5b8c\u6210<br \/>\n    this_thread::sleep_for(chrono::seconds(1));<br \/>\n    return 0;<br \/>\n}<\/p>\n<h3>\u4e8c\u3001std::function \u5305\u88c5\u5668&#xff1a;\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u7edf\u4e00\u795e\u5668<\/h3>\n<p>\u5728 C&#043;&#043; \u4e2d&#xff0c;\u53ef\u8c03\u7528\u5bf9\u8c61\u662f\u4e00\u4e2a\u5e7f\u4e49\u7684\u6982\u5ff5&#xff0c;\u5305\u62ec\u666e\u901a\u51fd\u6570\u3001\u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u3001Lambda \u8868\u8fbe\u5f0f\u3001\u7c7b\u6210\u5458\u51fd\u6570\u7b49\u3002\u8fd9\u4e9b\u53ef\u8c03\u7528\u5bf9\u8c61\u867d\u7136\u90fd\u53ef\u4ee5\u901a\u8fc7 () \u8fd0\u7b97\u7b26\u8c03\u7528&#xff0c;\u4f46\u5b83\u4eec\u7684\u5e95\u5c42\u7c7b\u578b\u5374\u5b58\u5728\u672c\u8d28\u5dee\u5f02&#xff1a;\u666e\u901a\u51fd\u6570\u7684\u7c7b\u578b\u7531\u51fd\u6570\u7b7e\u540d\u51b3\u5b9a&#xff0c;\u4eff\u51fd\u6570\u7684\u7c7b\u578b\u662f\u81ea\u5b9a\u4e49\u7c7b\u7c7b\u578b&#xff0c;Lambda \u8868\u8fbe\u5f0f\u7684\u7c7b\u578b\u662f\u7f16\u8bd1\u5668\u751f\u6210\u7684\u533f\u540d\u7c7b\u7c7b\u578b\u3002\u8fd9\u79cd\u7c7b\u578b\u5dee\u5f02\u5bfc\u81f4\u4e86\u4e00\u4e2a\u4e25\u91cd\u7684\u95ee\u9898&#xff1a;\u65e0\u6cd5\u7528\u7edf\u4e00\u7684\u7c7b\u578b\u58f0\u660e\u3001\u5b58\u50a8\u3001\u4f20\u9012\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002<\/p>\n<p>C&#043;&#043;11 \u5f15\u5165\u7684 std::function \u5305\u88c5\u5668&#xff0c;\u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002std::function \u662f\u4e00\u79cd\u7c7b\u578b\u5b89\u5168\u7684\u901a\u7528\u53ef\u8c03\u7528\u5bf9\u8c61\u5305\u88c5\u5668&#xff0c;\u5176\u6838\u5fc3\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u5305\u88c5\u4efb\u610f\u7b26\u5408\u7279\u5b9a\u7b7e\u540d\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u5b9e\u73b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u7edf\u4e00&#xff0c;\u6781\u5927\u5730\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u4e0e\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<h4>2.1 std::function \u7684\u6a21\u677f\u7ed3\u6784\u4e0e\u6838\u5fc3\u7279\u6027<\/h4>\n<p>std::function \u662f\u4e00\u4e2a\u6a21\u677f\u7c7b&#xff0c;\u5b9a\u4e49\u5728 &lt;functional&gt; \u5934\u6587\u4ef6\u4e2d\u3002\u5176\u6838\u5fc3\u6a21\u677f\u7ed3\u6784\u5982\u4e0b&#xff1a;<\/p>\n<p>template &lt;class T&gt;<br \/>\nclass function; \/\/ \u672a\u5b9a\u4e49\u7684\u57fa\u7840\u6a21\u677f<\/p>\n<p>template &lt;class Ret, class&#8230; Args&gt;<br \/>\nclass function&lt;Ret(Args&#8230;)&gt;; \/\/ \u7279\u5316\u6a21\u677f<\/p>\n<p>\u5176\u4e2d&#xff0c;Ret \u8868\u793a\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;Args&#8230; \u8868\u793a\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u7c7b\u578b\u5217\u8868\u3002Ret (Args&#8230;) \u88ab\u79f0\u4e3a\u51fd\u6570\u7b7e\u540d&#xff0c;\u5b83\u51b3\u5b9a\u4e86 std::function \u53ef\u4ee5\u5305\u88c5\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b \u2014\u2014 \u53ea\u6709\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u53c2\u6570\u7c7b\u578b\u5217\u8868\u5b8c\u5168\u5339\u914d\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u624d\u80fd\u88ab\u8be5 std::function \u5305\u88c5\u3002<\/p>\n<p>std::function \u7684\u6838\u5fc3\u7279\u6027\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762&#xff1a;<\/p>\n<h5>2.1.1 \u7c7b\u578b\u7edf\u4e00&#xff1a;\u6253\u7834\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u58c1\u5792<\/h5>\n<p>\u8fd9\u662f std::function \u6700\u6838\u5fc3\u7684\u7279\u6027\u3002\u65e0\u8bba\u5e95\u5c42\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u5982\u4f55&#xff08;\u666e\u901a\u51fd\u6570\u3001\u4eff\u51fd\u6570\u3001Lambda \u8868\u8fbe\u5f0f\u7b49&#xff09;&#xff0c;\u53ea\u8981\u5176\u51fd\u6570\u7b7e\u540d\u4e0e std::function \u7684\u6a21\u677f\u53c2\u6570\u5339\u914d&#xff0c;\u5c31\u53ef\u4ee5\u88ab std::function \u5305\u88c5&#xff0c;\u5e76\u4e14\u5305\u88c5\u540e\u7684\u5bf9\u8c61\u5177\u6709\u7edf\u4e00\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u666e\u901a\u51fd\u6570 bool func1 (int a, int b)\u3001\u4e00\u4e2a\u4eff\u51fd\u6570 struct Func { bool operator ()(int a, int b) { return a &gt; b; } }\u3001\u4e00\u4e2a Lambda \u8868\u8fbe\u5f0f [](int a, int b) { return a &lt; b; }&#xff0c;\u5b83\u4eec\u7684\u51fd\u6570\u7b7e\u540d\u90fd\u662f bool (int, int)&#xff0c;\u56e0\u6b64\u90fd\u53ef\u4ee5\u88ab std::function&lt;bool (int, int)&gt; \u5305\u88c5&#xff0c;\u5305\u88c5\u540e\u7684\u5bf9\u8c61\u7c7b\u578b\u5b8c\u5168\u4e00\u81f4\u3002<\/p>\n<p>\u8fd9\u79cd\u7c7b\u578b\u7edf\u4e00\u7684\u7279\u6027&#xff0c;\u89e3\u51b3\u4e86\u4e0d\u540c\u7c7b\u578b\u53ef\u8c03\u7528\u5bf9\u8c61\u65e0\u6cd5\u7edf\u4e00\u5b58\u50a8\u3001\u4f20\u9012\u7684\u95ee\u9898\u3002\u4f8b\u5982&#xff0c;\u53ef\u4ee5\u5c06\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u5b58\u50a8\u5728\u540c\u4e00\u4e2a vector \u5bb9\u5668\u4e2d&#xff1a;vector&lt;function&lt;bool (int, int)&gt;&gt; vec {func1, Func (), [](int a, int b) { return a &lt; b; }}\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u7c7b\u578b\u7edf\u4e00\u7279\u6027<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u666e\u901a\u51fd\u6570<br \/>\nbool compareGreater(int a, int b) {<br \/>\n    return a &gt; b;<br \/>\n}<\/p>\n<p>\/\/ \u4eff\u51fd\u6570<br \/>\nstruct CompareLess {<br \/>\n    bool operator()(int a, int b) {<br \/>\n        return a &lt; b;<br \/>\n    }<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    \/\/ \u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u4f46\u7b7e\u540d\u76f8\u540c<br \/>\n    auto lambdaEqual &#061; [](int a, int b) { return a &#061;&#061; b; };<\/p>\n<p>    \/\/ \u7528 std::function \u7edf\u4e00\u7c7b\u578b<br \/>\n    vector&lt;function&lt;bool(int, int)&gt;&gt; funcList;<br \/>\n    funcList.push_back(compareGreater);   \/\/ \u666e\u901a\u51fd\u6570<br \/>\n    funcList.push_back(CompareLess());    \/\/ \u4eff\u51fd\u6570<br \/>\n    funcList.push_back(lambdaEqual);      \/\/ Lambda \u8868\u8fbe\u5f0f<\/p>\n<p>    \/\/ \u7edf\u4e00\u8c03\u7528<br \/>\n    int x &#061; 5, y &#061; 5;<br \/>\n    for (size_t i &#061; 0; i &lt; funcList.size(); &#043;&#043;i) {<br \/>\n        cout &lt;&lt; &#034;funcList[&#034; &lt;&lt; i &lt;&lt; &#034;](&#034; &lt;&lt; x &lt;&lt; &#034;,&#034; &lt;&lt; y &lt;&lt; &#034;) &#061; &#034;<br \/>\n             &lt;&lt; boolalpha &lt;&lt; funcList[i](x, y) &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>2.1.2 \u7c7b\u578b\u5b89\u5168&#xff1a;\u7f16\u8bd1\u671f\u68c0\u67e5\u51fd\u6570\u7b7e\u540d\u5339\u914d\u6027<\/h5>\n<p>\u4e0e\u51fd\u6570\u6307\u9488\u76f8\u6bd4&#xff0c;std::function \u5177\u6709\u66f4\u5f3a\u7684\u7c7b\u578b\u5b89\u5168\u6027\u3002\u51fd\u6570\u6307\u9488\u5728\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u65f6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7c7b\u578b\u4e0d\u5339\u914d\u7684\u95ee\u9898&#xff0c;\u800c std::function \u4f1a\u5728\u7f16\u8bd1\u671f\u4e25\u683c\u68c0\u67e5\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u51fd\u6570\u7b7e\u540d\u662f\u5426\u4e0e\u6a21\u677f\u53c2\u6570\u5339\u914d\u3002\u5982\u679c\u4e0d\u5339\u914d&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u76f4\u63a5\u89e6\u53d1\u7f16\u8bd1\u9519\u8bef&#xff0c;\u907f\u514d\u4e86\u8fd0\u884c\u65f6\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u82e5\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u7b7e\u540d\u4e3a int (int, int) \u7684\u51fd\u6570\u5305\u88c5\u5230 std::function&lt;bool (int, int)&gt; \u4e2d&#xff0c;\u7f16\u8bd1\u5668\u4f1a\u660e\u786e\u63d0\u793a\u7c7b\u578b\u4e0d\u5339\u914d&#xff0c;\u4ece\u800c\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u7f16\u8bd1\u9636\u6bb5\u53d1\u73b0\u95ee\u9898\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u7c7b\u578b\u5b89\u5168\u68c0\u67e5<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u7b7e\u540d&#xff1a;int(int, int)<br \/>\nint add(int a, int b) {<br \/>\n    return a &#043; b;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u6b63\u786e&#xff1a;\u7b7e\u540d\u5339\u914d<br \/>\n    function&lt;int(int, int)&gt; f1 &#061; add;<br \/>\n    cout &lt;&lt; &#034;f1(3,4) &#061; &#034; &lt;&lt; f1(3,4) &lt;&lt; endl;<\/p>\n<p>    \/\/ \u9519\u8bef&#xff1a;\u7b7e\u540d\u4e0d\u5339\u914d&#xff08;\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u540c&#xff09;&#xff0c;\u7f16\u8bd1\u5931\u8d25<br \/>\n    \/\/ function&lt;bool(int, int)&gt; f2 &#061; add;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>2.1.3 \u652f\u6301\u7a7a\u72b6\u6001&#xff1a;\u907f\u514d\u7a7a\u6307\u9488\u8c03\u7528\u98ce\u9669<\/h5>\n<p>std::function \u652f\u6301\u9ed8\u8ba4\u6784\u9020&#xff0c;\u9ed8\u8ba4\u6784\u9020\u7684 std::function \u5bf9\u8c61\u5904\u4e8e\u7a7a\u72b6\u6001&#xff0c;\u5373\u4e0d\u6301\u6709\u4efb\u4f55\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u53ef\u4ee5\u901a\u8fc7 operator bool () \u6210\u5458\u51fd\u6570\u5224\u65ad std::function \u5bf9\u8c61\u662f\u5426\u4e3a\u7a7a&#xff0c;\u4ece\u800c\u907f\u514d\u7a7a\u6307\u9488\u8c03\u7528\u7684\u98ce\u9669\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u5728\u8c03\u7528 std::function \u5bf9\u8c61\u4e4b\u524d&#xff0c;\u53ef\u4ee5\u5148\u8fdb\u884c\u7a7a\u5224\u65ad&#xff1a;if (f) { f (10, 20); }\u3002\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u76f4\u63a5\u4f7f\u7528\u51fd\u6570\u6307\u9488\u66f4\u52a0\u5b89\u5168&#xff0c;\u56e0\u4e3a\u51fd\u6570\u6307\u9488\u7684\u7a7a\u6307\u9488\u8c03\u7528\u4f1a\u76f4\u63a5\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83&#xff0c;\u800c std::function \u7684\u7a7a\u72b6\u6001\u5224\u65ad\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u7a7a\u72b6\u6001\u68c0\u67e5<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>int multiply(int a, int b) {<br \/>\n    return a * b;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u9ed8\u8ba4\u6784\u9020&#xff1a;\u7a7a\u72b6\u6001<br \/>\n    function&lt;int(int, int)&gt; f;<\/p>\n<p>    \/\/ \u7a7a\u5224\u65ad<br \/>\n    if (!f) {<br \/>\n        cout &lt;&lt; &#034;f \u4e3a\u7a7a&#xff0c;\u65e0\u6cd5\u8c03\u7528&#034; &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    \/\/ \u8d4b\u503c\u540e\u53d8\u4e3a\u975e\u7a7a<br \/>\n    f &#061; multiply;<br \/>\n    if (f) {<br \/>\n        cout &lt;&lt; &#034;f(5,6) &#061; &#034; &lt;&lt; f(5,6) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;30<br \/>\n    }<\/p>\n<p>    \/\/ \u6e05\u7a7a<br \/>\n    f &#061; nullptr;<br \/>\n    if (!f) {<br \/>\n        cout &lt;&lt; &#034;f \u5df2\u6e05\u7a7a&#xff0c;\u65e0\u6cd5\u8c03\u7528&#034; &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>2.1.4 \u517c\u5bb9\u6240\u6709\u53ef\u8c03\u7528\u5bf9\u8c61&#xff1a;\u5c24\u5176\u662f\u6709\u6355\u6349\u7684 Lambda \u8868\u8fbe\u5f0f<\/h5>\n<p>\u51fd\u6570\u6307\u9488\u7684\u4e00\u4e2a\u91cd\u8981\u5c40\u9650\u6027\u662f\u65e0\u6cd5\u76f4\u63a5\u6307\u5411\u6709\u6355\u6349\u7684 Lambda \u8868\u8fbe\u5f0f\u3002\u56e0\u4e3a\u6709\u6355\u6349\u7684 Lambda \u8868\u8fbe\u5f0f\u5bf9\u5e94\u7684\u4eff\u51fd\u6570\u7c7b\u5305\u542b\u6210\u5458\u53d8\u91cf&#xff0c;\u5176\u5927\u5c0f\u4e0d\u4e3a\u96f6&#xff0c;\u800c\u51fd\u6570\u6307\u9488\u7684\u5927\u5c0f\u662f\u56fa\u5b9a\u7684&#xff08;\u901a\u5e38\u4e3a 4 \u6216 8 \u5b57\u8282&#xff09;&#xff0c;\u65e0\u6cd5\u5b58\u50a8\u4eff\u51fd\u6570\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u3002<\/p>\n<p>std::function \u5219\u5b8c\u7f8e\u89e3\u51b3\u4e86\u8fd9\u4e00\u95ee\u9898&#xff0c;\u5b83\u53ef\u4ee5\u5305\u88c5\u4efb\u610f\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u5305\u62ec\u6709\u6355\u6349\u7684 Lambda \u8868\u8fbe\u5f0f\u3002\u8fd9\u662f\u56e0\u4e3a std::function \u5185\u90e8\u91c7\u7528\u4e86\u7c7b\u578b\u64e6\u9664\u6280\u672f&#xff0c;\u901a\u8fc7\u52a8\u6001\u5206\u914d\u5185\u5b58\u5b58\u50a8\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u526f\u672c\u6216\u5f15\u7528&#xff0c;\u4ece\u800c\u517c\u5bb9\u4e0d\u540c\u5927\u5c0f\u3001\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u5305\u88c5\u6709\u6355\u6349\u7684 Lambda<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    int base &#061; 100;<\/p>\n<p>    \/\/ \u6709\u6355\u6349\u7684 Lambda \u8868\u8fbe\u5f0f<br \/>\n    auto lambda &#061; [base](int a) {<br \/>\n        return a &#043; base;<br \/>\n    };<\/p>\n<p>    \/\/ \u51fd\u6570\u6307\u9488\u65e0\u6cd5\u6307\u5411\u6709\u6355\u6349\u7684 Lambda&#xff08;\u7f16\u8bd1\u9519\u8bef&#xff09;<br \/>\n    \/\/ int (*fp)(int) &#061; lambda;<\/p>\n<p>    \/\/ std::function \u53ef\u4ee5\u6b63\u5e38\u5305\u88c5<br \/>\n    function&lt;int(int)&gt; f &#061; lambda;<br \/>\n    cout &lt;&lt; &#034;f(50) &#061; &#034; &lt;&lt; f(50) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;150<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>2.2 std::function \u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<p>std::function \u7684\u4f7f\u7528\u65b9\u6cd5\u8f83\u4e3a\u7b80\u5355&#xff0c;\u4e3b\u8981\u5305\u62ec\u5bf9\u8c61\u7684\u521b\u5efa\u3001\u8d4b\u503c\u3001\u8c03\u7528\u4e09\u4e2a\u6b65\u9aa4&#xff0c;\u540c\u65f6\u9700\u8981\u6ce8\u610f\u5305\u88c5\u7c7b\u6210\u5458\u51fd\u6570\u65f6\u7684\u7279\u6b8a\u89c4\u5219\u3002<\/p>\n<h5>2.2.1 \u5305\u88c5\u666e\u901a\u51fd\u6570\u4e0e\u4eff\u51fd\u6570<\/h5>\n<p>\u5305\u88c5\u666e\u901a\u51fd\u6570\u4e0e\u4eff\u51fd\u6570\u7684\u65b9\u5f0f\u8f83\u4e3a\u76f4\u63a5&#xff0c;\u53ea\u9700\u8981\u5c06\u53ef\u8c03\u7528\u5bf9\u8c61\u8d4b\u503c\u7ed9 std::function \u5bf9\u8c61\u5373\u53ef\u3002\u4f8b\u5982&#xff1a;<\/p>\n<ul>\n<li>\u5305\u88c5\u666e\u901a\u51fd\u6570&#xff1a;function&lt;bool (int, int)&gt; f &#061; func1;&#xff0c;\u5176\u4e2d func1 \u662f\u4e00\u4e2a\u7b7e\u540d\u4e3a bool (int, int) \u7684\u666e\u901a\u51fd\u6570\u3002<\/li>\n<li>\u5305\u88c5\u4eff\u51fd\u6570&#xff1a;function&lt;bool (int, int)&gt; f &#061; Func ();&#xff0c;\u5176\u4e2d Func \u662f\u4e00\u4e2a\u91cd\u8f7d\u4e86 operator () \u8fd0\u7b97\u7b26\u7684\u4eff\u51fd\u6570\u7c7b\u3002<\/li>\n<\/ul>\n<p>\u8d4b\u503c\u5b8c\u6210\u540e&#xff0c;\u53ef\u4ee5\u901a\u8fc7 () \u8fd0\u7b97\u7b26\u8c03\u7528 std::function \u5bf9\u8c61&#xff0c;\u672c\u8d28\u4e0a\u662f\u8c03\u7528\u5176\u5305\u88c5\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u5305\u88c5\u666e\u901a\u51fd\u6570\u4e0e\u4eff\u51fd\u6570<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u666e\u901a\u51fd\u6570<br \/>\ndouble divide(double a, double b) {<br \/>\n    if (b &#061;&#061; 0) return 0;<br \/>\n    return a \/ b;<br \/>\n}<\/p>\n<p>\/\/ \u4eff\u51fd\u6570<br \/>\nstruct Multiply {<br \/>\n    double operator()(double a, double b) {<br \/>\n        return a * b;<br \/>\n    }<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    \/\/ \u5305\u88c5\u666e\u901a\u51fd\u6570<br \/>\n    function&lt;double(double, double)&gt; f1 &#061; divide;<br \/>\n    cout &lt;&lt; &#034;f1(10, 2) &#061; &#034; &lt;&lt; f1(10, 2) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;5<\/p>\n<p>    \/\/ \u5305\u88c5\u4eff\u51fd\u6570<br \/>\n    function&lt;double(double, double)&gt; f2 &#061; Multiply();<br \/>\n    cout &lt;&lt; &#034;f2(10, 2) &#061; &#034; &lt;&lt; f2(10, 2) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;20<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>2.2.2 \u5305\u88c5 Lambda \u8868\u8fbe\u5f0f<\/h5>\n<p>\u5305\u88c5 Lambda \u8868\u8fbe\u5f0f\u7684\u65b9\u5f0f\u4e0e\u5305\u88c5\u666e\u901a\u51fd\u6570\u3001\u4eff\u51fd\u6570\u7c7b\u4f3c&#xff0c;\u65e2\u53ef\u4ee5\u76f4\u63a5\u5c06 Lambda \u8868\u8fbe\u5f0f\u8d4b\u503c\u7ed9 std::function \u5bf9\u8c61&#xff0c;\u4e5f\u53ef\u4ee5\u5c06 Lambda \u8868\u8fbe\u5f0f\u7684\u53d8\u91cf\u8d4b\u503c\u7ed9 std::function \u5bf9\u8c61\u3002\u4f8b\u5982&#xff1a;<\/p>\n<ul>\n<li>\u76f4\u63a5\u8d4b\u503c&#xff1a;function&lt;bool (int, int)&gt; f &#061; [](int a, int b) { return a &gt; b; };<\/li>\n<li>\u901a\u8fc7\u53d8\u91cf\u8d4b\u503c&#xff1a;auto lambda &#061; [](int a, int b) { return a &lt; b; }; function&lt;bool (int, int)&gt; f &#061; lambda;<\/li>\n<\/ul>\n<p>\u65e0\u8bba\u662f\u6709\u6355\u6349\u8fd8\u662f\u65e0\u6355\u6349\u7684 Lambda \u8868\u8fbe\u5f0f&#xff0c;\u90fd\u53ef\u4ee5\u88ab std::function \u5305\u88c5&#xff0c;\u8fd9\u662f std::function \u76f8\u6bd4\u51fd\u6570\u6307\u9488\u7684\u91cd\u8981\u4f18\u52bf\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u5305\u88c5 Lambda \u8868\u8fbe\u5f0f<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u76f4\u63a5\u5305\u88c5\u65e0\u6355\u6349 Lambda<br \/>\n    function&lt;int(int, int)&gt; add &#061; [](int a, int b) {<br \/>\n        return a &#043; b;<br \/>\n    };<br \/>\n    cout &lt;&lt; &#034;add(3,4) &#061; &#034; &lt;&lt; add(3,4) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;7<\/p>\n<p>    \/\/ \u5305\u88c5\u6709\u6355\u6349 Lambda<br \/>\n    int factor &#061; 10;<br \/>\n    auto multiply &#061; [factor](int a, int b) {<br \/>\n        return (a &#043; b) * factor;<br \/>\n    };<br \/>\n    function&lt;int(int, int)&gt; f &#061; multiply;<br \/>\n    cout &lt;&lt; &#034;f(3,4) &#061; &#034; &lt;&lt; f(3,4) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;70<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>2.2.3 \u5305\u88c5\u7c7b\u6210\u5458\u51fd\u6570<\/h5>\n<p>\u5305\u88c5\u7c7b\u6210\u5458\u51fd\u6570\u65f6&#xff0c;\u9700\u8981\u6ce8\u610f\u4e24\u4e2a\u7279\u6b8a\u89c4\u5219&#xff1a;<\/p>\n<li>\u5fc5\u987b\u6307\u660e\u7c7b\u57df\u4e0e\u53d6\u5730\u5740\u7b26\u53f7&#xff1a;\u7c7b\u6210\u5458\u51fd\u6570\u7684\u5730\u5740\u9700\u8981\u901a\u8fc7 &amp; \u7c7b\u540d&#xff1a;&#xff1a;\u6210\u5458\u51fd\u6570\u540d \u7684\u65b9\u5f0f\u83b7\u53d6&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528\u6210\u5458\u51fd\u6570\u540d\u3002<\/li>\n<li>\u666e\u901a\u6210\u5458\u51fd\u6570\u5305\u542b\u9690\u85cf\u7684 this \u6307\u9488&#xff1a;\u7c7b\u7684\u666e\u901a\u6210\u5458\u51fd\u6570\u4f1a\u9690\u5f0f\u63a5\u6536\u4e00\u4e2a this \u6307\u9488\u53c2\u6570&#xff0c;\u7528\u4e8e\u6307\u5411\u8c03\u7528\u8be5\u51fd\u6570\u7684\u5bf9\u8c61\u3002\u56e0\u6b64&#xff0c;\u5728\u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570\u65f6&#xff0c;std::function \u7684\u6a21\u677f\u53c2\u6570\u9700\u8981\u5305\u542b this \u6307\u9488\u7684\u7c7b\u578b\u3002<\/li>\n<p>\u5177\u4f53\u6765\u8bf4&#xff0c;\u5305\u88c5\u7c7b\u6210\u5458\u51fd\u6570\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5&#xff1a;<\/p>\n<ul>\n<li>\u5305\u88c5\u9759\u6001\u6210\u5458\u51fd\u6570&#xff1a;\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u5305\u542b this \u6307\u9488&#xff0c;\u5176\u7b7e\u540d\u4e0e\u666e\u901a\u51fd\u6570\u4e00\u81f4\u3002\u56e0\u6b64&#xff0c;\u5305\u88c5\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u65b9\u5f0f\u4e0e\u5305\u88c5\u666e\u901a\u51fd\u6570\u76f8\u540c&#xff0c;\u4f8b\u5982&#xff1a;function&lt;int (int, int)&gt; f &#061; &amp;Plus::plusi;&#xff0c;\u5176\u4e2d plusi \u662f Plus \u7c7b\u7684\u9759\u6001\u6210\u5458\u51fd\u6570&#xff0c;\u7b7e\u540d\u4e3a int (int, int)\u3002<\/li>\n<li>\u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570&#xff1a;\u666e\u901a\u6210\u5458\u51fd\u6570\u5305\u542b this \u6307\u9488&#xff0c;\u56e0\u6b64 std::function \u7684\u6a21\u677f\u53c2\u6570\u9700\u8981\u5c06 this \u6307\u9488\u7684\u7c7b\u578b\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u4f8b\u5982&#xff0c;Plus \u7c7b\u7684\u666e\u901a\u6210\u5458\u51fd\u6570 plusd \u7684\u7b7e\u540d\u4e3a double (double, double)&#xff0c;\u5176\u5b9e\u9645\u7b7e\u540d\u4e3a double (Plus*, double, double)&#xff08;this \u6307\u9488\u7684\u7c7b\u578b\u4e3a Plus*&#xff09;\u3002\u56e0\u6b64&#xff0c;\u5305\u88c5\u8be5\u51fd\u6570\u7684\u65b9\u5f0f\u4e3a&#xff1a;function&lt;double (Plus*, double, double)&gt; f &#061; &amp;Plus::plusd;\u3002<\/li>\n<\/ul>\n<p>\u8c03\u7528\u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570\u7684 std::function \u5bf9\u8c61\u65f6&#xff0c;\u9700\u8981\u4f20\u5165\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570&#xff0c;\u4f8b\u5982&#xff1a;Plus p; f (&amp;p, 1.1, 2.2);\u3002\u9664\u4e86\u4f20\u5165\u6307\u9488&#xff0c;\u4e5f\u53ef\u4ee5\u4f20\u5165\u5bf9\u8c61\u672c\u8eab\u6216\u53f3\u503c\u5f15\u7528&#xff0c;\u6b64\u65f6 std::function \u7684\u6a21\u677f\u53c2\u6570\u9700\u8981\u76f8\u5e94\u8c03\u6574\u4e3a\u5bf9\u8c61\u7c7b\u578b\u6216\u53f3\u503c\u5f15\u7528\u7c7b\u578b\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u5305\u88c5\u7c7b\u6210\u5458\u51fd\u6570<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>class Calculator {<br \/>\npublic:<br \/>\n    \/\/ \u9759\u6001\u6210\u5458\u51fd\u6570<br \/>\n    static int addInt(int a, int b) {<br \/>\n        return a &#043; b;<br \/>\n    }<\/p>\n<p>    \/\/ \u666e\u901a\u6210\u5458\u51fd\u6570<br \/>\n    double addDouble(double a, double b) {<br \/>\n        return a &#043; b;<br \/>\n    }<\/p>\n<p>    \/\/ \u5e26\u6210\u5458\u53d8\u91cf\u7684\u666e\u901a\u6210\u5458\u51fd\u6570<br \/>\n    double multiply(double a) {<br \/>\n        return a * _factor;<br \/>\n    }<\/p>\n<p>    Calculator(double f) : _factor(f) {}<br \/>\nprivate:<br \/>\n    double _factor;<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u5305\u88c5\u9759\u6001\u6210\u5458\u51fd\u6570<br \/>\n    function&lt;int(int, int)&gt; f1 &#061; &amp;Calculator::addInt;<br \/>\n    cout &lt;&lt; &#034;f1(10,20) &#061; &#034; &lt;&lt; f1(10,20) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;30<\/p>\n<p>    \/\/ 2. \u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570&#xff08;\u4f20\u5165\u5bf9\u8c61\u6307\u9488&#xff09;<br \/>\n    function&lt;double(Calculator*, double, double)&gt; f2 &#061; &amp;Calculator::addDouble;<br \/>\n    Calculator calc(2.0);<br \/>\n    cout &lt;&lt; &#034;f2(&amp;calc, 1.5, 2.5) &#061; &#034; &lt;&lt; f2(&amp;calc, 1.5, 2.5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;4.0<\/p>\n<p>    \/\/ 3. \u5305\u88c5\u5e26\u6210\u5458\u53d8\u91cf\u7684\u666e\u901a\u6210\u5458\u51fd\u6570<br \/>\n    function&lt;double(Calculator&amp;, double)&gt; f3 &#061; &amp;Calculator::multiply;<br \/>\n    cout &lt;&lt; &#034;f3(calc, 5.0) &#061; &#034; &lt;&lt; f3(calc, 5.0) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;10.0<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>2.3 std::function \u7684\u9ad8\u7ea7\u7528\u6cd5&#xff1a;\u7c7b\u578b\u522b\u540d\u4e0e\u51fd\u6570\u53c2\u6570<\/h4>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;std::function \u7684\u4e00\u4e2a\u5e38\u7528\u9ad8\u7ea7\u7528\u6cd5\u662f\u7ed3\u5408 using \u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\u578b\u522b\u540d&#xff0c;\u4ece\u800c\u7b80\u5316\u4ee3\u7801\u4e66\u5199\u3002\u4f8b\u5982&#xff1a;using FuncType &#061; function&lt;bool (int, int)&gt;;\u3002\u5b9a\u4e49\u7c7b\u578b\u522b\u540d\u540e&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8be5\u522b\u540d\u58f0\u660e std::function \u5bf9\u8c61&#xff0c;\u4f8b\u5982&#xff1a;FuncType f &#061; [](int a, int b) { return a &gt; b; };\u3002<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e&#xff0c;\u5f53\u9700\u8981\u4fee\u6539\u51fd\u6570\u7b7e\u540d\u65f6&#xff0c;\u53ea\u9700\u8981\u4fee\u6539\u7c7b\u578b\u522b\u540d\u7684\u5b9a\u4e49&#xff0c;\u800c\u4e0d\u9700\u8981\u4fee\u6539\u6240\u6709\u4f7f\u7528\u8be5\u7c7b\u578b\u7684\u4ee3\u7801&#xff0c;\u6781\u5927\u5730\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u6b64\u5916&#xff0c;std::function \u8fd8\u5e38\u88ab\u7528\u4f5c\u51fd\u6570\u53c2\u6570\u6216\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u7528\u4e8e\u63a5\u6536\u4efb\u610f\u7b26\u5408\u7b7e\u540d\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u4e00\u4e2a std::function \u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570&#xff0c;\u5b9e\u73b0\u5bf9\u4e0d\u540c\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7edf\u4e00\u5904\u7406&#xff1a;<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u7c7b\u578b\u522b\u540d\u4e0e\u51fd\u6570\u53c2\u6570<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ \u5b9a\u4e49\u7c7b\u578b\u522b\u540d\u7b80\u5316\u4ee3\u7801<br \/>\nusing CalcFunc &#061; function&lt;int(int, int)&gt;;<\/p>\n<p>\/\/ \u63a5\u6536 std::function \u4f5c\u4e3a\u53c2\u6570<br \/>\nvoid executeCalc(CalcFunc f, int a, int b, const string&amp; desc) {<br \/>\n    if (f) {<br \/>\n        cout &lt;&lt; desc &lt;&lt; &#034;: &#034; &lt;&lt; a &lt;&lt; &#034; \u548c &#034; &lt;&lt; b &lt;&lt; &#034; \u7684\u8ba1\u7b97\u7ed3\u679c&#xff1a;&#034; &lt;&lt; f(a, b) &lt;&lt; endl;<br \/>\n    } else {<br \/>\n        cout &lt;&lt; &#034;\u51fd\u6570\u5bf9\u8c61\u4e3a\u7a7a&#xff01;&#034; &lt;&lt; endl;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u8fd4\u56de std::function \u5bf9\u8c61<br \/>\nCalcFunc getAddFunc() {<br \/>\n    return [](int a, int b) { return a &#043; b; };<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u4f7f\u7528\u7c7b\u578b\u522b\u540d\u58f0\u660e\u5bf9\u8c61<br \/>\n    CalcFunc addFunc &#061; [](int a, int b) { return a &#043; b; };<br \/>\n    CalcFunc subFunc &#061; [](int a, int b) { return a &#8211; b; };<\/p>\n<p>    \/\/ \u4f20\u9012 std::function \u4f5c\u4e3a\u53c2\u6570<br \/>\n    executeCalc(addFunc, 10, 5, &#034;\u52a0\u6cd5&#034;);<br \/>\n    executeCalc(subFunc, 10, 5, &#034;\u51cf\u6cd5&#034;);<\/p>\n<p>    \/\/ \u4f7f\u7528\u8fd4\u56de\u7684 std::function \u5bf9\u8c61<br \/>\n    CalcFunc func &#061; getAddFunc();<br \/>\n    cout &lt;&lt; &#034;getAddFunc()(20,30) &#061; &#034; &lt;&lt; func(20,30) &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>2.4 std::function \u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406&#xff1a;\u7c7b\u578b\u64e6\u9664\u6280\u672f<\/h4>\n<p>std::function \u80fd\u591f\u5305\u88c5\u4e0d\u540c\u7c7b\u578b\u3001\u4e0d\u540c\u5927\u5c0f\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u5176\u5e95\u5c42\u4f9d\u8d56\u4e8e\u7c7b\u578b\u64e6\u9664\u6280\u672f\u3002\u7c7b\u578b\u64e6\u9664\u7684\u6838\u5fc3\u601d\u60f3\u662f&#xff1a;\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u8f6c\u5316\u4e3a\u7edf\u4e00\u7684\u63a5\u53e3&#xff0c;\u901a\u8fc7\u591a\u6001\u673a\u5236\u5b9e\u73b0\u5bf9\u4e0d\u540c\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8c03\u7528\u3002<\/p>\n<p>\u5177\u4f53\u6765\u8bf4&#xff0c;std::function \u7684\u5e95\u5c42\u5b9e\u73b0\u5927\u81f4\u5206\u4e3a\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4&#xff1a;<\/p>\n<li>\u5b9a\u4e49\u62bd\u8c61\u57fa\u7c7b&#xff1a;\u62bd\u8c61\u57fa\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570 operator ()&#xff0c;\u5176\u7b7e\u540d\u4e0e std::function \u7684\u6a21\u677f\u53c2\u6570\u4e00\u81f4\u3002<\/li>\n<li>\u5b9a\u4e49\u6a21\u677f\u6d3e\u751f\u7c7b&#xff1a;\u6d3e\u751f\u7c7b\u662f\u4e00\u4e2a\u6a21\u677f\u7c7b&#xff0c;\u6a21\u677f\u53c2\u6570\u4e3a\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u6d3e\u751f\u7c7b\u7ee7\u627f\u81ea\u62bd\u8c61\u57fa\u7c7b&#xff0c;\u5e76\u5b9e\u73b0\u7eaf\u865a\u51fd\u6570 operator ()&#xff0c;\u5728\u8be5\u51fd\u6570\u4e2d\u8c03\u7528\u5305\u88c5\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002<\/li>\n<li>\u52a8\u6001\u5206\u914d\u6d3e\u751f\u7c7b\u5bf9\u8c61&#xff1a;\u5f53\u5c06\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\u8d4b\u503c\u7ed9 std::function \u65f6&#xff0c;std::function \u4f1a\u52a8\u6001\u5206\u914d\u4e00\u4e2a\u6d3e\u751f\u7c7b\u5bf9\u8c61&#xff0c;\u5c06\u53ef\u8c03\u7528\u5bf9\u8c61\u5b58\u50a8\u5728\u6d3e\u751f\u7c7b\u4e2d\u3002<\/li>\n<li>\u5b58\u50a8\u57fa\u7c7b\u6307\u9488&#xff1a;std::function \u5185\u90e8\u5b58\u50a8\u4e00\u4e2a\u6307\u5411\u62bd\u8c61\u57fa\u7c7b\u7684\u6307\u9488&#xff0c;\u901a\u8fc7\u8be5\u6307\u9488\u8c03\u7528\u7eaf\u865a\u51fd\u6570 operator ()&#xff0c;\u4ece\u800c\u5b9e\u73b0\u5bf9\u4e0d\u540c\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7edf\u4e00\u8c03\u7528\u3002<\/li>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f&#xff0c;std::function \u64e6\u9664\u4e86\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u5177\u4f53\u7c7b\u578b&#xff0c;\u53ea\u4fdd\u7559\u4e86\u5176\u51fd\u6570\u7b7e\u540d\u4fe1\u606f&#xff0c;\u4ece\u800c\u5b9e\u73b0\u4e86\u5bf9\u4e0d\u540c\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u5305\u88c5\u3002\u7c7b\u578b\u64e6\u9664\u6280\u672f\u662f std::function \u5b9e\u73b0\u7c7b\u578b\u7edf\u4e00\u7684\u6838\u5fc3&#xff0c;\u4e5f\u662f\u5176\u80fd\u591f\u517c\u5bb9\u6240\u6709\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u6839\u672c\u539f\u56e0\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u7b80\u6613\u7248\u7c7b\u578b\u64e6\u9664\u5b9e\u73b0<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;memory&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ 1. \u62bd\u8c61\u57fa\u7c7b&#xff1a;\u5b9a\u4e49\u7edf\u4e00\u63a5\u53e3<br \/>\ntemplate &lt;typename Ret, typename&#8230; Args&gt;<br \/>\nclass FuncBase {<br \/>\npublic:<br \/>\n    virtual ~FuncBase() &#061; default;<br \/>\n    virtual Ret operator()(Args&#8230; args) &#061; 0;<br \/>\n};<\/p>\n<p>\/\/ 2. \u6a21\u677f\u6d3e\u751f\u7c7b&#xff1a;\u5305\u88c5\u5177\u4f53\u7684\u53ef\u8c03\u7528\u5bf9\u8c61<br \/>\ntemplate &lt;typename F, typename Ret, typename&#8230; Args&gt;<br \/>\nclass FuncDerived : public FuncBase&lt;Ret, Args&#8230;&gt; {<br \/>\nprivate:<br \/>\n    F _func; \/\/ \u5b58\u50a8\u53ef\u8c03\u7528\u5bf9\u8c61<br \/>\npublic:<br \/>\n    FuncDerived(F&amp;&amp; func) : _func(std::move(func)) {}<\/p>\n<p>    Ret operator()(Args&#8230; args) override {<br \/>\n        return _func(std::forward&lt;Args&gt;(args)&#8230;);<br \/>\n    }<br \/>\n};<\/p>\n<p>\/\/ 3. \u7b80\u6613\u7248 function \u5305\u88c5\u5668<br \/>\ntemplate &lt;typename Signature&gt;<br \/>\nclass MyFunction;<\/p>\n<p>template &lt;typename Ret, typename&#8230; Args&gt;<br \/>\nclass MyFunction&lt;Ret(Args&#8230;)&gt; {<br \/>\nprivate:<br \/>\n    unique_ptr&lt;FuncBase&lt;Ret, Args&#8230;&gt;&gt; _ptr; \/\/ \u57fa\u7c7b\u6307\u9488<br \/>\npublic:<br \/>\n    \/\/ \u9ed8\u8ba4\u6784\u9020&#xff1a;\u7a7a\u72b6\u6001<br \/>\n    MyFunction() &#061; default;<\/p>\n<p>    \/\/ \u8d4b\u503c\u6784\u9020&#xff1a;\u63a5\u53d7\u4efb\u610f\u53ef\u8c03\u7528\u5bf9\u8c61<br \/>\n    template &lt;typename F&gt;<br \/>\n    MyFunction(F&amp;&amp; func) {<br \/>\n        _ptr &#061; make_unique&lt;FuncDerived&lt;F, Ret, Args&#8230;&gt;&gt;(std::forward&lt;F&gt;(func));<br \/>\n    }<\/p>\n<p>    \/\/ \u8c03\u7528\u8fd0\u7b97\u7b26<br \/>\n    Ret operator()(Args&#8230; args) const {<br \/>\n        if (!_ptr) {<br \/>\n            throw runtime_error(&#034;MyFunction is empty!&#034;);<br \/>\n        }<br \/>\n        return (*_ptr)(std::forward&lt;Args&gt;(args)&#8230;);<br \/>\n    }<\/p>\n<p>    \/\/ \u7a7a\u5224\u65ad<br \/>\n    explicit operator bool() const {<br \/>\n        return _ptr !&#061; nullptr;<br \/>\n    }<br \/>\n};<\/p>\n<p>\/\/ \u6d4b\u8bd5<br \/>\nint main() {<br \/>\n    \/\/ \u5305\u88c5\u666e\u901a\u51fd\u6570<br \/>\n    auto add &#061; [](int a, int b) { return a &#043; b; };<br \/>\n    MyFunction&lt;int(int, int)&gt; f(add);<\/p>\n<p>    if (f) {<br \/>\n        cout &lt;&lt; &#034;f(3,4) &#061; &#034; &lt;&lt; f(3,4) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;7<br \/>\n    }<\/p>\n<p>    \/\/ \u5305\u88c5\u6709\u6355\u6349\u7684 Lambda<br \/>\n    int base &#061; 100;<br \/>\n    auto addBase &#061; [base](int a) { return a &#043; base; };<br \/>\n    MyFunction&lt;int(int)&gt; f2(addBase);<br \/>\n    cout &lt;&lt; &#034;f2(50) &#061; &#034; &lt;&lt; f2(50) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;150<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h3>\u4e09\u3001std::bind \u9002\u914d\u5668&#xff1a;\u51fd\u6570\u53c2\u6570\u7684\u7075\u6d3b\u8c03\u6574\u5de5\u5177<\/h3>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u5e38\u5e38\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u573a\u666f&#xff1a;\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u4e2a\u6570\u8fc7\u591a&#xff0c;\u800c\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528\u5176\u4e2d\u7684\u4e00\u90e8\u5206&#xff1b;\u6216\u8005\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u987a\u5e8f\u4e0d\u7b26\u5408\u6211\u4eec\u7684\u9700\u6c42&#xff0c;\u9700\u8981\u8c03\u6574\u53c2\u6570\u7684\u4f20\u9012\u987a\u5e8f\u3002\u5728 C&#043;&#043;11 \u4e4b\u524d&#xff0c;\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u552f\u4e00\u65b9\u6cd5\u662f\u91cd\u5199\u51fd\u6570\u7684\u91cd\u8f7d\u7248\u672c&#xff0c;\u8fd9\u4e0d\u4ec5\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u5197\u4f59\u5ea6&#xff0c;\u8fd8\u964d\u4f4e\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>C&#043;&#043;11 \u5f15\u5165\u7684 std::bind \u9002\u914d\u5668&#xff0c;\u4e3a\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u63d0\u4f9b\u4e86\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002std::bind \u662f\u4e00\u79cd\u51fd\u6570\u9002\u914d\u5668&#xff0c;\u5176\u672c\u8d28\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f&#xff0c;\u80fd\u591f\u5bf9\u73b0\u6709\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u8fdb\u884c\u53c2\u6570\u5c42\u9762\u7684\u6539\u9020&#xff0c;\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002\u65b0\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u5728\u8c03\u7528\u65f6&#xff0c;\u4f1a\u81ea\u52a8\u8c03\u7528\u539f\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u5e76\u6309\u7167\u9884\u8bbe\u7684\u89c4\u5219\u4f20\u9012\u53c2\u6570\u3002<\/p>\n<h4>3.1 std::bind \u7684\u6838\u5fc3\u6982\u5ff5\u4e0e\u8c03\u7528\u683c\u5f0f<\/h4>\n<p>std::bind \u7684\u6838\u5fc3\u4f5c\u7528\u662f\u8c03\u6574\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570&#xff0c;\u5176\u8c03\u7528\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>auto newCallable &#061; bind(callable, arg_list);<\/p>\n<p>\u5176\u4e2d&#xff0c;\u5404\u4e2a\u53c2\u6570\u7684\u542b\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>callable&#xff1a;\u5f85\u9002\u914d\u7684\u539f\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u53ef\u4ee5\u662f\u666e\u901a\u51fd\u6570\u3001\u4eff\u51fd\u6570\u3001Lambda \u8868\u8fbe\u5f0f\u3001std::function \u5bf9\u8c61\u3001\u7c7b\u6210\u5458\u51fd\u6570\u7b49\u3002<\/li>\n<li>arg_list&#xff1a;\u9017\u53f7\u5206\u9694\u7684\u53c2\u6570\u5217\u8868&#xff0c;\u7528\u4e8e\u6307\u5b9a\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u6765\u6e90\u3002\u8be5\u53c2\u6570\u5217\u8868\u5305\u542b\u4e24\u79cd\u7c7b\u578b\u7684\u5143\u7d20&#xff1a;\u666e\u901a\u503c\u548c\u5360\u4f4d\u7b26\u3002<\/li>\n<li>newCallable&#xff1a;std::bind \u8fd4\u56de\u7684\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u5176\u7c7b\u578b\u662f\u7f16\u8bd1\u5668\u751f\u6210\u7684\u533f\u540d\u7c7b\u578b&#xff0c;\u901a\u5e38\u9700\u8981\u4f7f\u7528 auto \u5173\u952e\u5b57\u63a5\u6536\u3002\u8c03\u7528 newCallable \u65f6&#xff0c;\u672c\u8d28\u4e0a\u662f\u8c03\u7528\u539f\u53ef\u8c03\u7528\u5bf9\u8c61 callable&#xff0c;\u5e76\u6309\u7167 arg_list \u7684\u89c4\u5219\u4f20\u9012\u53c2\u6570\u3002<\/li>\n<\/ul>\n<p>std::bind \u7684\u6838\u5fc3\u5728\u4e8e arg_list \u7684\u8bbe\u8ba1&#xff0c;\u5176\u4e2d\u666e\u901a\u503c\u4e0e\u5360\u4f4d\u7b26\u7684\u7ec4\u5408\u4f7f\u7528&#xff0c;\u51b3\u5b9a\u4e86\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u4e2a\u6570\u4e0e\u4f20\u9012\u89c4\u5219\u3002<\/p>\n<h4>3.2 \u5360\u4f4d\u7b26&#xff1a;std::bind \u7684\u6838\u5fc3\u5173\u952e<\/h4>\n<p>\u5360\u4f4d\u7b26\u662f std::bind \u5b9e\u73b0\u53c2\u6570\u8c03\u6574\u7684\u6838\u5fc3\u5de5\u5177&#xff0c;\u5b9a\u4e49\u5728 std::placeholders \u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u5360\u4f4d\u7b26\u7684\u5f62\u5f0f\u4e3a _n&#xff08;n \u662f\u6b63\u6574\u6570&#xff09;&#xff0c;\u5176\u6838\u5fc3\u542b\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<ul>\n<li>_1 \u8868\u793a\u8c03\u7528\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6\u4f20\u5165\u7684\u7b2c 1 \u4e2a\u53c2\u6570\u3002<\/li>\n<li>_2 \u8868\u793a\u8c03\u7528\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6\u4f20\u5165\u7684\u7b2c 2 \u4e2a\u53c2\u6570\u3002<\/li>\n<li>\u4ee5\u6b64\u7c7b\u63a8&#xff0c;_n \u8868\u793a\u8c03\u7528\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6\u4f20\u5165\u7684\u7b2c n \u4e2a\u53c2\u6570\u3002<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u5360\u4f4d\u7b26\u65f6&#xff0c;\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e24\u4e2a\u89c4\u5219&#xff1a;<\/p>\n<li>\u547d\u540d\u7a7a\u95f4\u5f15\u7528&#xff1a;\u5360\u4f4d\u7b26\u4f4d\u4e8e std::placeholders \u547d\u540d\u7a7a\u95f4\u4e2d&#xff0c;\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u9700\u8981\u663e\u5f0f\u5f15\u7528&#xff0c;\u4f8b\u5982 std::placeholders::_1&#xff0c;\u6216\u8005\u901a\u8fc7 using namespace std::placeholders; \u58f0\u660e\u547d\u540d\u7a7a\u95f4&#xff0c;\u7b80\u5316\u4ee3\u7801\u4e66\u5199\u3002<\/li>\n<li>\u5360\u4f4d\u7b26\u4e0d\u80fd\u8d85\u754c&#xff1a;\u5360\u4f4d\u7b26\u7684\u6570\u503c\u4e0d\u80fd\u8d85\u8fc7\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u4e2a\u6570\u3002\u4f8b\u5982&#xff0c;\u5982\u679c\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u8c03\u7528\u65f6\u9700\u8981\u4f20\u5165 2 \u4e2a\u53c2\u6570&#xff0c;\u90a3\u4e48\u4f7f\u7528 _3 \u4f1a\u89e6\u53d1\u7f16\u8bd1\u9519\u8bef&#xff0c;\u56e0\u4e3a\u4e0d\u5b58\u5728\u7b2c 3 \u4e2a\u53c2\u6570\u3002<\/li>\n<p>\u5360\u4f4d\u7b26\u7684\u672c\u8d28\u662f\u53c2\u6570\u4f4d\u7f6e\u7684\u6807\u8bb0&#xff0c;\u5b83\u6807\u8bb0\u4e86\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u9700\u8981\u4f20\u9012\u7ed9\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u54ea\u4e2a\u4f4d\u7f6e\u3002\u901a\u8fc7\u8c03\u6574\u5360\u4f4d\u7b26\u7684\u987a\u5e8f&#xff0c;\u53ef\u4ee5\u7075\u6d3b\u5730\u6539\u53d8\u53c2\u6570\u7684\u4f20\u9012\u987a\u5e8f&#xff1b;\u901a\u8fc7\u51cf\u5c11\u5360\u4f4d\u7b26\u7684\u6570\u91cf&#xff0c;\u53ef\u4ee5\u56fa\u5b9a\u90e8\u5206\u53c2\u6570&#xff0c;\u51cf\u5c11\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u4e2a\u6570\u3002<\/p>\n<h4>3.3 std::bind \u7684\u4e09\u5927\u6838\u5fc3\u529f\u80fd<\/h4>\n<p>std::bind \u7684\u6838\u5fc3\u529f\u80fd\u662f\u8c03\u6574\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570&#xff0c;\u5177\u4f53\u53ef\u4ee5\u5206\u4e3a\u56fa\u5b9a\u53c2\u6570\u3001\u8c03\u6574\u53c2\u6570\u987a\u5e8f\u3001\u6df7\u5408\u8c03\u6574\u4e09\u79cd\u573a\u666f&#xff0c;\u8986\u76d6\u4e86\u7edd\u5927\u591a\u6570\u53c2\u6570\u8c03\u6574\u7684\u9700\u6c42\u3002<\/p>\n<h5>3.3.1 \u529f\u80fd\u4e00&#xff1a;\u56fa\u5b9a\u53c2\u6570 \u2014\u2014 \u51cf\u5c11\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u4e2a\u6570<\/h5>\n<p>\u8fd9\u662f std::bind \u6700\u5e38\u7528\u7684\u529f\u80fd\u3002\u5f53\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u4e2a\u6570\u8fc7\u591a&#xff0c;\u800c\u6211\u4eec\u53ea\u9700\u8981\u4f7f\u7528\u5176\u4e2d\u7684\u4e00\u90e8\u5206\u65f6&#xff0c;\u53ef\u4ee5\u901a\u8fc7 std::bind \u5c06\u4e0d\u9700\u8981\u7684\u53c2\u6570\u56fa\u5b9a\u4e3a\u5177\u4f53\u503c&#xff0c;\u4ece\u800c\u51cf\u5c11\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u4e2a\u6570\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u662f&#xff1a;\u5728 arg_list \u4e2d&#xff0c;\u5c06\u9700\u8981\u56fa\u5b9a\u7684\u53c2\u6570\u66ff\u6362\u4e3a\u5177\u4f53\u7684\u666e\u901a\u503c&#xff0c;\u9700\u8981\u52a8\u6001\u4f20\u5165\u7684\u53c2\u6570\u66ff\u6362\u4e3a\u5360\u4f4d\u7b26\u3002\u4f8b\u5982&#xff0c;\u539f\u51fd\u6570 Sub \u7684\u7b7e\u540d\u4e3a int (int, int)&#xff0c;\u8868\u793a\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u5dee\u503c\u4e58\u4ee5 10\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u56fa\u5b9a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a 100&#xff0c;\u53ea\u4fdd\u7559\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f5c\u4e3a\u52a8\u6001\u53c2\u6570&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528 std::bind&#xff1a;auto sub3 &#061; bind (Sub, 100, _1);\u3002<\/p>\n<p>\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61 sub3 \u7684\u53c2\u6570\u4e2a\u6570\u4e3a 1&#xff0c;\u8c03\u7528 sub3 (5) \u65f6&#xff0c;\u672c\u8d28\u4e0a\u662f\u8c03\u7528\u539f\u51fd\u6570 Sub (100, 5)&#xff0c;\u8ba1\u7b97\u7ed3\u679c\u4e3a (100 &#8211; 5) * 10 &#061; 950\u3002\u540c\u7406&#xff0c;\u5982\u679c\u56fa\u5b9a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a 100&#xff0c;\u53ea\u4fdd\u7559\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f5c\u4e3a\u52a8\u6001\u53c2\u6570&#xff0c;\u53ef\u4ee5\u5199\u6210 auto sub4 &#061; bind (Sub, _1, 100);&#xff0c;\u8c03\u7528 sub4 (5) \u65f6&#xff0c;\u672c\u8d28\u4e0a\u662f\u8c03\u7528 Sub (5, 100)&#xff0c;\u8ba1\u7b97\u7ed3\u679c\u4e3a (5 &#8211; 100) * 10 &#061; -950\u3002<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u52bf\u5728\u4e8e&#xff0c;\u65e0\u9700\u91cd\u5199\u539f\u51fd\u6570\u7684\u91cd\u8f7d\u7248\u672c&#xff0c;\u5c31\u53ef\u4ee5\u5b9e\u73b0\u53c2\u6570\u4e2a\u6570\u7684\u51cf\u5c11&#xff0c;\u6781\u5927\u5730\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u56fa\u5b9a\u53c2\u6570<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<br \/>\nusing namespace placeholders; \/\/ \u7b80\u5316\u5360\u4f4d\u7b26\u4f7f\u7528<\/p>\n<p>\/\/ \u539f\u51fd\u6570&#xff1a;\u8ba1\u7b97 (a &#8211; b) * 10<br \/>\nint Sub(int a, int b) {<br \/>\n    return (a &#8211; b) * 10;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u56fa\u5b9a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a 100&#xff0c;\u53ea\u4fdd\u7559\u7b2c\u4e8c\u4e2a\u53c2\u6570<br \/>\n    auto sub3 &#061; bind(Sub, 100, _1);<br \/>\n    cout &lt;&lt; &#034;sub3(5) &#061; &#034; &lt;&lt; sub3(5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;950 (100-5)*10<\/p>\n<p>    \/\/ \u56fa\u5b9a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a 100&#xff0c;\u53ea\u4fdd\u7559\u7b2c\u4e00\u4e2a\u53c2\u6570<br \/>\n    auto sub4 &#061; bind(Sub, _1, 100);<br \/>\n    cout &lt;&lt; &#034;sub4(5) &#061; &#034; &lt;&lt; sub4(5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;-950 (5-100)*10<\/p>\n<p>    \/\/ \u56fa\u5b9a\u6240\u6709\u53c2\u6570&#xff1a;\u65b0\u51fd\u6570\u65e0\u53c2\u6570<br \/>\n    auto sub5 &#061; bind(Sub, 20, 10);<br \/>\n    cout &lt;&lt; &#034;sub5() &#061; &#034; &lt;&lt; sub5() &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;100 (20-10)*10<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>3.3.2 \u529f\u80fd\u4e8c&#xff1a;\u8c03\u6574\u53c2\u6570\u987a\u5e8f \u2014\u2014 \u6539\u53d8\u53c2\u6570\u7684\u4f20\u9012\u987a\u5e8f<\/h5>\n<p>\u5728\u67d0\u4e9b\u573a\u666f\u4e0b&#xff0c;\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u987a\u5e8f\u4e0d\u7b26\u5408\u6211\u4eec\u7684\u9700\u6c42&#xff0c;\u9700\u8981\u8c03\u6574\u53c2\u6570\u7684\u4f20\u9012\u987a\u5e8f\u3002\u4f7f\u7528 std::bind \u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u5360\u4f4d\u7b26\u7684\u987a\u5e8f&#xff0c;\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd&#xff0c;\u65e0\u9700\u91cd\u5199\u51fd\u6570\u3002<\/p>\n<p>\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u662f&#xff1a;\u5728 arg_list \u4e2d&#xff0c;\u8c03\u6574\u5360\u4f4d\u7b26\u7684\u987a\u5e8f&#xff0c;\u4f7f\u5176\u4e0e\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u987a\u5e8f\u4e0d\u4e00\u81f4\u3002\u4f8b\u5982&#xff0c;\u539f\u51fd\u6570 Sub \u7684\u7b7e\u540d\u4e3a int (int, int)&#xff0c;\u8ba1\u7b97\u903b\u8f91\u4e3a (a &#8211; b) * 10\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u8c03\u6574\u53c2\u6570\u987a\u5e8f&#xff0c;\u8ba1\u7b97 (b &#8211; a) * 10&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528 std::bind&#xff1a;auto sub2 &#061; bind (Sub, _2, _1);\u3002<\/p>\n<p>\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61 sub2 \u7684\u53c2\u6570\u4e2a\u6570\u4ecd\u4e3a 2&#xff0c;\u4f46\u53c2\u6570\u987a\u5e8f\u53d1\u751f\u4e86\u53d8\u5316\u3002\u8c03\u7528 sub2 (10, 5) \u65f6&#xff0c;\u5360\u4f4d\u7b26 _2 \u5bf9\u5e94\u4f20\u5165\u7684\u7b2c 2 \u4e2a\u53c2\u6570 5&#xff0c;\u5360\u4f4d\u7b26 _1 \u5bf9\u5e94\u4f20\u5165\u7684\u7b2c 1 \u4e2a\u53c2\u6570 10&#xff0c;\u56e0\u6b64\u672c\u8d28\u4e0a\u662f\u8c03\u7528\u539f\u51fd\u6570 Sub (5, 10)&#xff0c;\u8ba1\u7b97\u7ed3\u679c\u4e3a (5 &#8211; 10) * 10 &#061; -50\u3002<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u901a\u8fc7\u8c03\u6574\u5360\u4f4d\u7b26\u7684\u987a\u5e8f&#xff0c;\u5b9e\u73b0\u4e86\u53c2\u6570\u4f20\u9012\u987a\u5e8f\u7684\u6539\u53d8&#xff0c;\u6781\u5927\u5730\u7b80\u5316\u4e86\u4ee3\u7801\u7684\u4fee\u6539\u6210\u672c\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u8c03\u6574\u53c2\u6570\u987a\u5e8f<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<br \/>\nusing namespace placeholders;<\/p>\n<p>\/\/ \u539f\u51fd\u6570&#xff1a;\u6bd4\u8f83 a \u662f\u5426\u5927\u4e8e b<br \/>\nbool isGreater(int a, int b) {<br \/>\n    cout &lt;&lt; &#034;\u6bd4\u8f83 &#034; &lt;&lt; a &lt;&lt; &#034; &gt; &#034; &lt;&lt; b &lt;&lt; &#034; ? &#034;;<br \/>\n    return a &gt; b;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ \u539f\u51fd\u6570\u8c03\u7528<br \/>\n    cout &lt;&lt; boolalpha &lt;&lt; isGreater(10, 5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;true<\/p>\n<p>    \/\/ \u8c03\u6574\u53c2\u6570\u987a\u5e8f&#xff1a;\u6bd4\u8f83 b \u662f\u5426\u5927\u4e8e a<br \/>\n    auto isLess &#061; bind(isGreater, _2, _1);<br \/>\n    cout &lt;&lt; boolalpha &lt;&lt; isLess(10, 5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;false<\/p>\n<p>    \/\/ \u4e09\u5143\u53c2\u6570\u8c03\u6574\u987a\u5e8f\u793a\u4f8b<br \/>\n    auto func &#061; [](int a, int b, int c) {<br \/>\n        return a * 100 &#043; b * 10 &#043; c;<br \/>\n    };<\/p>\n<p>    \/\/ \u8c03\u6574\u4e3a c, a, b \u7684\u987a\u5e8f<br \/>\n    auto func2 &#061; bind(func, _2, _3, _1);<br \/>\n    cout &lt;&lt; &#034;func2(1,2,3) &#061; &#034; &lt;&lt; func2(1,2,3) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;231 (2*100 &#043;3*10 &#043;1)<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h5>3.3.3 \u529f\u80fd\u4e09&#xff1a;\u6df7\u5408\u8c03\u6574 \u2014\u2014 \u56fa\u5b9a\u53c2\u6570\u4e0e\u8c03\u6574\u987a\u5e8f\u7684\u7ed3\u5408<\/h5>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u5e38\u5e38\u9700\u8981\u540c\u65f6\u5b9e\u73b0\u56fa\u5b9a\u53c2\u6570\u4e0e\u8c03\u6574\u53c2\u6570\u987a\u5e8f&#xff0c;\u8fd9\u5c31\u662f std::bind \u7684\u6df7\u5408\u8c03\u6574\u529f\u80fd\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u662f&#xff1a;\u5728 arg_list \u4e2d&#xff0c;\u540c\u65f6\u4f7f\u7528\u666e\u901a\u503c&#xff08;\u56fa\u5b9a\u53c2\u6570&#xff09;\u548c\u5360\u4f4d\u7b26&#xff08;\u8c03\u6574\u987a\u5e8f&#xff09;&#xff0c;\u4ece\u800c\u5b9e\u73b0\u590d\u6742\u7684\u53c2\u6570\u8c03\u6574\u9700\u6c42\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u539f\u51fd\u6570 SubX \u7684\u7b7e\u540d\u4e3a int (int, int, int)&#xff0c;\u8ba1\u7b97\u903b\u8f91\u4e3a (a &#8211; b &#8211; c) * 10\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u56fa\u5b9a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a 100&#xff0c;\u540c\u65f6\u8c03\u6574\u540e\u4e24\u4e2a\u53c2\u6570\u7684\u987a\u5e8f&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528 std::bind&#xff1a;auto sub5 &#061; bind (SubX, 100, _2, _1);\u3002<\/p>\n<p>\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61 sub5 \u7684\u53c2\u6570\u4e2a\u6570\u4e3a 2&#xff0c;\u8c03\u7528 sub5 (5, 1) \u65f6&#xff0c;\u7b2c\u4e00\u4e2a\u53c2\u6570 100 \u662f\u56fa\u5b9a\u503c&#xff0c;\u5360\u4f4d\u7b26 _2 \u5bf9\u5e94\u4f20\u5165\u7684\u7b2c 2 \u4e2a\u53c2\u6570 1&#xff0c;\u5360\u4f4d\u7b26 _1 \u5bf9\u5e94\u4f20\u5165\u7684\u7b2c 1 \u4e2a\u53c2\u6570 5&#xff0c;\u56e0\u6b64\u672c\u8d28\u4e0a\u662f\u8c03\u7528\u539f\u51fd\u6570 SubX (100, 1, 5)&#xff0c;\u8ba1\u7b97\u7ed3\u679c\u4e3a (100 &#8211; 1 &#8211; 5) * 10 &#061; 940\u3002<\/p>\n<p>\u6df7\u5408\u8c03\u6574\u529f\u80fd\u7ed3\u5408\u4e86\u56fa\u5b9a\u53c2\u6570\u4e0e\u8c03\u6574\u987a\u5e8f\u7684\u4f18\u52bf&#xff0c;\u80fd\u591f\u6ee1\u8db3\u7edd\u5927\u591a\u6570\u590d\u6742\u7684\u53c2\u6570\u8c03\u6574\u9700\u6c42&#xff0c;\u662f std::bind \u7075\u6d3b\u6027\u7684\u96c6\u4e2d\u4f53\u73b0\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u6df7\u5408\u8c03\u6574\u53c2\u6570<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<br \/>\nusing namespace placeholders;<\/p>\n<p>\/\/ \u539f\u51fd\u6570&#xff1a;\u8ba1\u7b97 (a &#8211; b &#8211; c) * 10<br \/>\nint SubX(int a, int b, int c) {<br \/>\n    return (a &#8211; b &#8211; c) * 10;<br \/>\n}<\/p>\n<p>\/\/ \u8ba1\u7b97\u590d\u5229&#xff1a;\u672c\u91d1 * (1 &#043; \u5229\u7387)^\u5e74\u6570 &#8211; \u672c\u91d1<br \/>\ndouble compoundInterest(double rate, double principal, int years) {<br \/>\n    double result &#061; principal;<br \/>\n    for (int i &#061; 0; i &lt; years; &#043;&#043;i) {<br \/>\n        result *&#061; (1 &#043; rate);<br \/>\n    }<br \/>\n    return result &#8211; principal;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u56fa\u5b9a\u7b2c\u4e00\u4e2a\u53c2\u6570&#xff0c;\u8c03\u6574\u540e\u4e24\u4e2a\u53c2\u6570\u987a\u5e8f<br \/>\n    auto sub5 &#061; bind(SubX, 100, _2, _1);<br \/>\n    cout &lt;&lt; &#034;sub5(5,1) &#061; &#034; &lt;&lt; sub5(5,1) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;940 (100-1-5)*10<\/p>\n<p>    \/\/ 2. \u56fa\u5b9a\u5229\u7387\u548c\u5e74\u6570&#xff0c;\u53ea\u4fdd\u7559\u672c\u91d1\u4f5c\u4e3a\u53c2\u6570<br \/>\n    auto calcInterest &#061; bind(compoundInterest, 0.03, _1, 5);<br \/>\n    cout &lt;&lt; &#034;10000 \u5143 5 \u5e74\u590d\u5229&#xff08;\u5229\u7387 3%&#xff09;&#xff1a;&#034; &lt;&lt; calcInterest(10000) &lt;&lt; endl;<\/p>\n<p>    \/\/ 3. \u56fa\u5b9a\u5e74\u6570&#xff0c;\u8c03\u6574\u5229\u7387\u548c\u672c\u91d1\u987a\u5e8f<br \/>\n    auto calcInterest2 &#061; bind(compoundInterest, _2, _1, 10);<br \/>\n    cout &lt;&lt; &#034;20000 \u5143 10 \u5e74\u590d\u5229&#xff08;\u5229\u7387 4%&#xff09;&#xff1a;&#034; &lt;&lt; calcInterest2(20000, 0.04) &lt;&lt; endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>3.4 std::bind \u4e0e std::function \u7684\u7ed3\u5408\u4f7f\u7528<\/h4>\n<p>std::bind \u8fd4\u56de\u7684\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u662f\u7f16\u8bd1\u5668\u751f\u6210\u7684\u533f\u540d\u7c7b\u578b&#xff0c;\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528\u7c7b\u578b\u540d\u58f0\u660e&#xff0c;\u53ea\u80fd\u901a\u8fc7 auto \u5173\u952e\u5b57\u63a5\u6536\u3002\u8fd9\u79cd\u533f\u540d\u7c7b\u578b\u5728\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u6216\u5bb9\u5668\u5143\u7d20\u65f6\u4f1a\u53d7\u5230\u9650\u5236&#xff0c;\u800c std::function \u53ef\u4ee5\u5b8c\u7f8e\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<p>std::function \u53ef\u4ee5\u5305\u88c5 std::bind \u8fd4\u56de\u7684\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u5b9e\u73b0\u7c7b\u578b\u7684\u7edf\u4e00\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u662f&#xff1a;\u6839\u636e\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u51fd\u6570\u7b7e\u540d&#xff0c;\u5b9a\u4e49\u5bf9\u5e94\u7684 std::function \u7c7b\u578b&#xff0c;\u7136\u540e\u5c06 std::bind \u8fd4\u56de\u7684\u5bf9\u8c61\u8d4b\u503c\u7ed9 std::function \u5bf9\u8c61\u3002<\/p>\n<p>\u4f8b\u5982&#xff0c;\u539f Lambda \u8868\u8fbe\u5f0f func1 \u7684\u7b7e\u540d\u4e3a double (double, double, int)&#xff0c;\u8868\u793a\u8ba1\u7b97\u590d\u5229\u7684\u5229\u606f\u3002\u901a\u8fc7 std::bind \u56fa\u5b9a\u5229\u7387\u4e3a 0.015\u3001\u5e74\u6570\u4e3a 3&#xff0c;\u5f97\u5230\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61 func3_1_5&#xff0c;\u5176\u7b7e\u540d\u4e3a double (double)&#xff08;\u4ec5\u63a5\u6536\u672c\u91d1\u4f5c\u4e3a\u53c2\u6570&#xff09;\u3002\u4f7f\u7528 std::function \u5305\u88c5\u8be5\u5bf9\u8c61\u7684\u65b9\u5f0f\u4e3a&#xff1a;function&lt;double (double)&gt; f &#061; bind (func1, 0.015, _1, 3);\u3002<\/p>\n<p>\u8fd9\u79cd\u7ed3\u5408\u4f7f\u7528\u7684\u65b9\u5f0f&#xff0c;\u65e2\u53d1\u6325\u4e86 std::bind \u7075\u6d3b\u8c03\u6574\u53c2\u6570\u7684\u4f18\u52bf&#xff0c;\u53c8\u5229\u7528\u4e86 std::function \u7edf\u4e00\u7c7b\u578b\u7684\u7279\u6027&#xff0c;\u6781\u5927\u5730\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u4e0e\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;bind \u4e0e function \u7ed3\u5408\u4f7f\u7528<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;vector&gt;<br \/>\nusing namespace std;<br \/>\nusing namespace placeholders;<\/p>\n<p>\/\/ \u8ba1\u7b97\u5546\u54c1\u6298\u6263\u4ef7&#xff1a;\u539f\u4ef7 * (1 &#8211; \u6298\u6263\u7387)<br \/>\ndouble discountPrice(double price, double discount) {<br \/>\n    return price * (1 &#8211; discount);<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u7ed1\u5b9a\u56fa\u5b9a\u6298\u6263\u7387&#xff0c;\u751f\u6210\u65b0\u7684\u53ef\u8c03\u7528\u5bf9\u8c61<br \/>\n    auto discount8 &#061; bind(discountPrice, _1, 0.2); \/\/ 8 \u6298<br \/>\n    auto discount9 &#061; bind(discountPrice, _1, 0.1); \/\/ 9 \u6298<\/p>\n<p>    \/\/ 2. \u7528 std::function \u7edf\u4e00\u7c7b\u578b<br \/>\n    using DiscountFunc &#061; function&lt;double(double)&gt;;<br \/>\n    vector&lt;DiscountFunc&gt; discountFuncs;<br \/>\n    discountFuncs.push_back(discount8);<br \/>\n    discountFuncs.push_back(discount9);<br \/>\n    discountFuncs.push_back(bind(discountPrice, _1, 0.05)); \/\/ 95 \u6298<\/p>\n<p>    \/\/ 3. \u7edf\u4e00\u8c03\u7528<br \/>\n    double originalPrice &#061; 100.0;<br \/>\n    for (size_t i &#061; 0; i &lt; discountFuncs.size(); &#043;&#043;i) {<br \/>\n        cout &lt;&lt; &#034;\u6298\u6263 &#034; &lt;&lt; (i&#061;&#061;0?&#034;8\u6298&#034;:(i&#061;&#061;1?&#034;9\u6298&#034;:&#034;95\u6298&#034;))<br \/>\n             &lt;&lt; &#034; \u4ef7\u683c&#xff1a;&#034; &lt;&lt; discountFuncs[i](originalPrice) &lt;&lt; endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>3.5 std::bind \u7684\u7279\u6b8a\u7528\u6cd5&#xff1a;\u5305\u88c5\u7c7b\u6210\u5458\u51fd\u6570<\/h4>\n<p>std::bind \u5305\u88c5\u7c7b\u6210\u5458\u51fd\u6570\u7684\u65b9\u5f0f\u4e0e std::function \u7c7b\u4f3c&#xff0c;\u9700\u8981\u6ce8\u610f\u4e24\u4e2a\u7279\u6b8a\u89c4\u5219&#xff1a;<\/p>\n<li>\u5fc5\u987b\u6307\u660e\u7c7b\u57df\u4e0e\u53d6\u5730\u5740\u7b26\u53f7&#xff1a;\u7c7b\u6210\u5458\u51fd\u6570\u7684\u5730\u5740\u9700\u8981\u901a\u8fc7 &amp; \u7c7b\u540d&#xff1a;&#xff1a;\u6210\u5458\u51fd\u6570\u540d \u7684\u65b9\u5f0f\u83b7\u53d6\u3002<\/li>\n<li>\u666e\u901a\u6210\u5458\u51fd\u6570\u9700\u8981\u7ed1\u5b9a\u5bf9\u8c61\u5b9e\u4f8b&#xff1a;\u7c7b\u7684\u666e\u901a\u6210\u5458\u51fd\u6570\u5305\u542b\u9690\u85cf\u7684 this \u6307\u9488&#xff0c;\u56e0\u6b64\u5728\u4f7f\u7528 std::bind \u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570\u65f6&#xff0c;\u9700\u8981\u5c06\u5bf9\u8c61\u5b9e\u4f8b\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165 arg_list&#xff0c;\u7528\u4e8e\u521d\u59cb\u5316 this \u6307\u9488\u3002<\/li>\n<p>\u4f8b\u5982&#xff0c;\u5305\u88c5 Plus \u7c7b\u7684\u666e\u901a\u6210\u5458\u51fd\u6570 plusd&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528 std::bind&#xff1a;function&lt;double (double, double)&gt; f &#061; bind (&amp;Plus::plusd, Plus (), _1, _2);\u3002\u5176\u4e2d&#xff0c;Plus () \u662f\u4e00\u4e2a\u4e34\u65f6\u5bf9\u8c61&#xff0c;\u7528\u4e8e\u521d\u59cb\u5316 this \u6307\u9488\u3002\u8c03\u7528 f (1.1, 2.2) \u65f6&#xff0c;\u672c\u8d28\u4e0a\u662f\u8c03\u7528\u4e34\u65f6\u5bf9\u8c61\u7684 plusd \u51fd\u6570&#xff0c;\u8ba1\u7b97\u7ed3\u679c\u4e3a 1.1 &#043; 2.2 &#061; 3.3\u3002<\/p>\n<p>\u9664\u4e86\u4f20\u5165\u4e34\u65f6\u5bf9\u8c61&#xff0c;\u8fd8\u53ef\u4ee5\u4f20\u5165\u5bf9\u8c61\u7684\u6307\u9488\u6216\u5f15\u7528&#xff0c;\u4f8b\u5982 bind (&amp;Plus::plusd, &amp;p, _1, _2)&#xff08;p \u662f Plus \u7c7b\u7684\u5bf9\u8c61&#xff09;&#xff0c;\u6b64\u65f6 this \u6307\u9488\u6307\u5411\u5bf9\u8c61 p\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;\u5305\u88c5\u7c7b\u6210\u5458\u51fd\u6570<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<br \/>\nusing namespace placeholders;<\/p>\n<p>class MathUtil {<br \/>\npublic:<br \/>\n    \/\/ \u666e\u901a\u6210\u5458\u51fd\u6570&#xff1a;\u4e58\u6cd5<br \/>\n    double multiply(double a, double b) {<br \/>\n        return a * b * _factor;<br \/>\n    }<\/p>\n<p>    \/\/ \u9759\u6001\u6210\u5458\u51fd\u6570&#xff1a;\u9664\u6cd5<br \/>\n    static double divide(double a, double b) {<br \/>\n        if (b &#061;&#061; 0) return 0;<br \/>\n        return a \/ b;<br \/>\n    }<\/p>\n<p>    MathUtil(double f) : _factor(f) {}<br \/>\nprivate:<br \/>\n    double _factor;<br \/>\n};<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570&#xff1a;\u7ed1\u5b9a\u5bf9\u8c61\u5b9e\u4f8b<br \/>\n    MathUtil util(2.0);<br \/>\n    auto mul &#061; bind(&amp;MathUtil::multiply, &amp;util, _1, _2);<br \/>\n    cout &lt;&lt; &#034;mul(3,4) &#061; &#034; &lt;&lt; mul(3,4) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;24 (3*4*2)<\/p>\n<p>    \/\/ 2. \u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570&#xff1a;\u56fa\u5b9a\u4e00\u4e2a\u53c2\u6570<br \/>\n    auto mulBy10 &#061; bind(&amp;MathUtil::multiply, util, _1, 10);<br \/>\n    cout &lt;&lt; &#034;mulBy10(5) &#061; &#034; &lt;&lt; mulBy10(5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;100 (5*10*2)<\/p>\n<p>    \/\/ 3. \u5305\u88c5\u9759\u6001\u6210\u5458\u51fd\u6570&#xff1a;\u65e0\u9700\u7ed1\u5b9a\u5bf9\u8c61<br \/>\n    auto div &#061; bind(&amp;MathUtil::divide, _1, _2);<br \/>\n    cout &lt;&lt; &#034;div(10,2) &#061; &#034; &lt;&lt; div(10,2) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;5<\/p>\n<p>    \/\/ 4. \u7ed3\u5408 std::function \u4f7f\u7528<br \/>\n    function&lt;double(double)&gt; divBy2 &#061; bind(&amp;MathUtil::divide, _1, 2);<br \/>\n    cout &lt;&lt; &#034;divBy2(20) &#061; &#034; &lt;&lt; divBy2(20) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;10<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>3.6 std::bind \u7684\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/h4>\n<p>std::bind \u867d\u7136\u7075\u6d3b&#xff0c;\u4f46\u5728\u4f7f\u7528\u65f6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898&#xff0c;\u907f\u514d\u51fa\u73b0\u4e0d\u5fc5\u8981\u7684\u9519\u8bef&#xff1a;<\/p>\n<li>\u5f15\u7528\u53c2\u6570\u7684\u4f20\u9012&#xff1a;\u5982\u679c\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u662f\u5f15\u7528\u7c7b\u578b&#xff0c;\u76f4\u63a5\u4f7f\u7528 std::bind \u4f20\u9012\u53d8\u91cf\u65f6&#xff0c;\u4f1a\u9ed8\u8ba4\u8fdb\u884c\u62f7\u8d1d&#xff0c;\u65e0\u6cd5\u4f20\u9012\u5f15\u7528\u3002\u6b64\u65f6\u9700\u8981\u4f7f\u7528 std::ref () \u6216 std::cref () \u5305\u88c5\u53d8\u91cf&#xff0c;std::ref () \u7528\u4e8e\u4f20\u9012\u975e const \u5f15\u7528&#xff0c;std::cref () \u7528\u4e8e\u4f20\u9012 const \u5f15\u7528\u3002<\/li>\n<li>\u907f\u514d\u7a7a\u7ed1\u5b9a&#xff1a;\u5982\u679c\u7ed1\u5b9a\u7684\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u5931\u6548&#xff08;\u4f8b\u5982\u7ed1\u5b9a\u4e86\u5c40\u90e8\u51fd\u6570\u7684\u6307\u9488\u3001\u5df2\u9500\u6bc1\u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570&#xff09;&#xff0c;\u8c03\u7528\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u56e0\u6b64&#xff0c;\u5728\u4f7f\u7528 std::bind \u65f6&#xff0c;\u9700\u8981\u786e\u4fdd\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u957f\u4e8e\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<li>\u4e0e Lambda \u8868\u8fbe\u5f0f\u7684\u5bf9\u6bd4&#xff1a;\u5bf9\u4e8e\u7b80\u5355\u7684\u53c2\u6570\u8c03\u6574\u573a\u666f&#xff08;\u5982\u56fa\u5b9a 1-2 \u4e2a\u53c2\u6570&#xff09;&#xff0c;\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u4f1a\u66f4\u52a0\u7b80\u6d01\u76f4\u89c2&#xff1b;\u5bf9\u4e8e\u590d\u6742\u7684\u53c2\u6570\u8c03\u6574\u573a\u666f&#xff08;\u5982\u52a8\u6001\u8c03\u6574\u53c2\u6570\u987a\u5e8f\u3001\u6279\u91cf\u56fa\u5b9a\u53c2\u6570&#xff09;&#xff0c;\u4f7f\u7528 std::bind \u4f1a\u66f4\u52a0\u7075\u6d3b\u3002\u5f00\u53d1\u8005\u5e94\u6839\u636e\u5b9e\u9645\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<\/li>\n<p>\u4ee3\u7801\u793a\u4f8b&#xff1a;bind \u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\nusing namespace std;<br \/>\nusing namespace placeholders;<\/p>\n<p>\/\/ \u5f15\u7528\u53c2\u6570\u7684\u51fd\u6570<br \/>\nvoid modifyValue(int&amp; val, int add) {<br \/>\n    val &#043;&#061; add;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    \/\/ 1. \u5f15\u7528\u53c2\u6570\u4f20\u9012&#xff1a;\u5fc5\u987b\u4f7f\u7528 std::ref<br \/>\n    int x &#061; 10;<br \/>\n    \/\/ \u9519\u8bef\u65b9\u5f0f&#xff1a;\u7ed1\u5b9a\u7684\u662f\u62f7\u8d1d&#xff0c;\u4e0d\u4f1a\u4fee\u6539\u539f\u53d8\u91cf<br \/>\n    auto badBind &#061; bind(modifyValue, x, _1);<br \/>\n    badBind(5);<br \/>\n    cout &lt;&lt; &#034;badBind \u540e x &#061; &#034; &lt;&lt; x &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;10<\/p>\n<p>    \/\/ \u6b63\u786e\u65b9\u5f0f&#xff1a;\u4f7f\u7528 std::ref \u4f20\u9012\u5f15\u7528<br \/>\n    auto goodBind &#061; bind(modifyValue, ref(x), _1);<br \/>\n    goodBind(5);<br \/>\n    cout &lt;&lt; &#034;goodBind \u540e x &#061; &#034; &lt;&lt; x &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;15<\/p>\n<p>    \/\/ 2. const \u5f15\u7528&#xff1a;\u4f7f\u7528 std::cref<br \/>\n    const int y &#061; 100;<br \/>\n    auto print &#061; [](const int&amp; v) { cout &lt;&lt; &#034;\u503c&#xff1a;&#034; &lt;&lt; v &lt;&lt; endl; };<br \/>\n    auto printY &#061; bind(print, cref(y));<br \/>\n    printY(); \/\/ \u8f93\u51fa&#xff1a;100<\/p>\n<p>    \/\/ 3. Lambda \u66ff\u4ee3\u7b80\u5355 bind \u573a\u666f<br \/>\n    int base &#061; 10;<br \/>\n    \/\/ bind \u65b9\u5f0f<br \/>\n    auto addBind &#061; bind([](int a, int b) { return a &#043; b; }, _1, base);<br \/>\n    \/\/ Lambda \u65b9\u5f0f&#xff08;\u66f4\u7b80\u6d01&#xff09;<br \/>\n    auto addLambda &#061; [base](int a) { return a &#043; base; };<\/p>\n<p>    cout &lt;&lt; &#034;addBind(5) &#061; &#034; &lt;&lt; addBind(5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;15<br \/>\n    cout &lt;&lt; &#034;addLambda(5) &#061; &#034; &lt;&lt; addLambda(5) &lt;&lt; endl; \/\/ \u8f93\u51fa&#xff1a;15<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>\u6700\u4f73\u5b9e\u8df5&#xff1a;\u5de5\u5177\u9009\u62e9\u7684\u4f18\u5148\u7ea7<\/h4>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u9009\u62e9 Lambda \u8868\u8fbe\u5f0f\u3001std::function \u6216 std::bind \u65f6&#xff0c;\u5e94\u9075\u5faa\u4ee5\u4e0b\u4f18\u5148\u7ea7\u539f\u5219&#xff1a;<\/p>\n<li>\u4f18\u5148\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f&#xff1a;\u5bf9\u4e8e\u8f7b\u91cf\u7ea7\u7684\u51fd\u6570\u903b\u8f91&#xff0c;\u5c24\u5176\u662f\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u7684\u4e34\u65f6\u903b\u8f91&#xff0c;\u4f18\u5148\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u3002Lambda \u8868\u8fbe\u5f0f\u8bed\u6cd5\u7b80\u6d01&#xff0c;\u65e0\u9700\u989d\u5916\u5b9a\u4e49\u7c7b\u6216\u51fd\u6570&#xff0c;\u4e14\u652f\u6301\u6355\u6349\u5916\u5c42\u53d8\u91cf&#xff0c;\u662f\u5904\u7406\u8f7b\u91cf\u7ea7\u903b\u8f91\u7684\u6700\u4f73\u9009\u62e9\u3002<\/li>\n<li>\u9700\u8981\u7c7b\u578b\u7edf\u4e00\u65f6\u4f7f\u7528 std::function&#xff1a;\u5f53\u9700\u8981\u5b58\u50a8\u3001\u4f20\u9012\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6&#xff0c;\u4f7f\u7528 std::function \u8fdb\u884c\u5305\u88c5&#xff0c;\u5b9e\u73b0\u7c7b\u578b\u7edf\u4e00\u3002\u4f8b\u5982&#xff0c;\u5c06\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u5bb9\u5668\u4e2d&#xff0c;\u6216\u5c06\u53ef\u8c03\u7528\u5bf9\u8c61\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u9012\u3002<\/li>\n<li>\u590d\u6742\u53c2\u6570\u8c03\u6574\u65f6\u4f7f\u7528 std::bind&#xff1a;\u5f53\u9700\u8981\u56fa\u5b9a\u53c2\u6570\u3001\u8c03\u6574\u53c2\u6570\u987a\u5e8f\u65f6&#xff0c;\u4f7f\u7528 std::bind \u5bf9\u53ef\u8c03\u7528\u5bf9\u8c61\u8fdb\u884c\u53c2\u6570\u8c03\u6574\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u53c2\u6570\u8c03\u6574&#xff0c;\u4e5f\u53ef\u4ee5\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u66ff\u4ee3&#xff0c;\u4ee5\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n<li>\u907f\u514d\u8fc7\u5ea6\u5c01\u88c5&#xff1a;\u5982\u679c\u903b\u8f91\u7b80\u5355\u4e14\u65e0\u9700\u590d\u7528&#xff0c;\u76f4\u63a5\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f\u5373\u53ef&#xff0c;\u4e0d\u5fc5\u5f3a\u884c\u7528 std::function \u5305\u88c5&#xff1b;\u5982\u679c\u53ea\u662f\u56fa\u5b9a 1~2 \u4e2a\u53c2\u6570&#xff0c;Lambda \u8868\u8fbe\u5f0f\u6bd4 std::bind \u66f4\u76f4\u89c2\u3002<\/li>\n<h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<p>Lambda \u8868\u8fbe\u5f0f\u3001std::function \u5305\u88c5\u5668\u4e0e std::bind \u9002\u914d\u5668\u662f C&#043;&#043;11 \u5f15\u5165\u7684\u4e09\u5927\u6838\u5fc3\u5de5\u5177&#xff0c;\u5b83\u4eec\u5206\u522b\u4ece\u8f7b\u91cf\u7ea7\u51fd\u6570\u5b9a\u4e49\u3001\u53ef\u8c03\u7528\u5bf9\u8c61\u7c7b\u578b\u7edf\u4e00\u3001\u51fd\u6570\u53c2\u6570\u9002\u914d\u4e09\u4e2a\u7ef4\u5ea6&#xff0c;\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u7b80\u6d01\u3001\u7075\u6d3b\u3001\u9ad8\u6548\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u5904\u7406\u65b9\u6848\u3002<\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u4f5c\u4e3a\u533f\u540d\u51fd\u6570\u5bf9\u8c61\u7684\u8bed\u6cd5\u7cd6&#xff0c;\u652f\u6301\u6355\u6349\u5916\u5c42\u53d8\u91cf&#xff0c;\u6781\u5927\u5730\u7b80\u5316\u4e86\u8f7b\u91cf\u7ea7\u51fd\u6570\u903b\u8f91\u7684\u5b9e\u73b0&#xff1b;std::function \u4f5c\u4e3a\u901a\u7528\u53ef\u8c03\u7528\u5bf9\u8c61\u5305\u88c5\u5668&#xff0c;\u6253\u7834\u4e86\u4e0d\u540c\u7c7b\u578b\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u58c1\u5792&#xff0c;\u5b9e\u73b0\u4e86\u7c7b\u578b\u7684\u7edf\u4e00\u5b58\u50a8\u4e0e\u4f20\u9012&#xff1b;std::bind \u4f5c\u4e3a\u51fd\u6570\u9002\u914d\u5668&#xff0c;\u80fd\u591f\u7075\u6d3b\u5730\u8c03\u6574\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570&#xff0c;\u5b9e\u73b0\u53c2\u6570\u7684\u56fa\u5b9a\u4e0e\u987a\u5e8f\u8c03\u6574\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u5408\u7406\u5730\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e09\u5927\u5de5\u5177&#xff0c;\u9075\u5faa\u00a0\u201c\u4f18\u5148\u4f7f\u7528 Lambda \u8868\u8fbe\u5f0f&#xff0c;\u7c7b\u578b\u7edf\u4e00\u4f7f\u7528 std::function&#xff0c;\u590d\u6742\u53c2\u6570\u8c03\u6574\u4f7f\u7528 std::bind\u201d\u00a0\u7684\u539f\u5219&#xff0c;\u53ef\u4ee5\u663e\u8457\u63d0\u5347\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u3001\u53ef\u8bfb\u6027\u4e0e\u53ef\u7ef4\u62a4\u6027&#xff0c;\u6784\u5efa\u66f4\u52a0\u4f18\u96c5\u7684\u73b0\u4ee3 C&#043;&#043; \u7a0b\u5e8f\u3002<\/p>\n<h2>\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801:<\/h2>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;functional&gt;<br \/>\n#include &lt;algorithm&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/ OK&#xff0c;\u90a3\u4e48\u5728\u672c\u6587\u4ef6\u4e2d&#xff0c;\u6211\u4eec\u6765\u5b66\u4e60\u4e00\u4e9b\u65b0\u4e1c\u897f&#xff1a;lambda\u8868\u8fbe\u5f0f&#xff0c;\u5305\u88c5\u5668function&#xff0c;\u5305\u88c5\u5668bind<br \/>\n\/\/ \u90a3\u4e48\u5176\u4e2d\u6700\u6838\u5fc3\u7684\u5c31\u662flambda\u8868\u8fbe\u5f0f&#xff0c;\u8fd9\u4e2a\u662f\u5f88\u8f7b\u91cf\u5316\u7684\u4e00\u4e2a\u51fd\u6570\u8868\u8fbe\u5f62\u5f0f<br \/>\n\/\/ \u5176\u5b9e\u6211\u4eec\u4e4b\u524d\u5728Linux\u7ebf\u7a0b\u90e8\u5206&#xff0c;\u4e5f\u6709\u7528\u5230\u8fc7&#xff0c;\u90a3\u4e48\u5728\u8fd9\u91cc&#xff0c;\u6211\u4eec\u5c31\u6765\u8be6\u7ec6\u7684\u89e3\u6790\u4e00\u4e0b<br \/>\nnamespace lambda_funtion_bind<br \/>\n{<br \/>\n    \/\/ \u90a3\u4e48lambda \u8868\u8fbe\u5f0f\u672c\u8d28\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u5bf9\u8c61&#xff0c;\u8ddf\u666e\u901a\u51fd\u6570\u4e0d\u540c\u7684\u662f\u4ed6\u53ef\u4ee5\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u3002<br \/>\n    \/\/  lambda \u8868\u8fbe\u5f0f\u8bed\u6cd5\u4f7f\u7528\u5c42\u800c\u8a00\u6ca1\u6709\u7c7b\u578b&#xff0c;<br \/>\n    \/\/  \u6240\u4ee5\u6211\u4eec\u4e00\u822c\u662f\u7528auto\u6216\u8005\u6a21\u677f\u53c2\u6570\u5b9a\u4e49\u7684\u5bf9\u8c61\u53bb\u63a5\u6536lambda \u5bf9\u8c61\u3002<\/p>\n<p>    \/\/ \u6240\u4ee5\u6211\u4eec\u5c31\u77e5\u9053\u4e86&#xff0c;\u54e6&#xff0c;lambda\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u5bf9\u8c61\u554a<br \/>\n    \/\/ \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528auto\u53bb\u63a5\u6536\u8be5\u5bf9\u8c61&#xff0c;\u90a3\u4e48\u6b64\u65f6\u8be5\u53d8\u91cf\u5c31\u662f\u76f8\u5f53\u4e8e\u4e3a\u4e00\u4e2a\u51fd\u6570<br \/>\n    \/\/ \u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf(\u53c2\u6570)\u800c\u8c03\u7528lambda\u6240\u5f62\u6210\u7684\u51fd\u6570<br \/>\n    \/\/ \u8fd9\u4e00\u70b9\u5927\u5bb6\u8981\u662f\u8981\u77e5\u9053\u7684<\/p>\n<p>    \/\/ \u63a5\u4e0b\u6765\u6211\u4eec\u6765\u4e86\u89e3\u4e00\u4e0blambda\u7684\u683c\u5f0f&#xff1a;<br \/>\n    \/\/                                [] () -&gt; \u8fd4\u56de\u503c\u7684\u7c7b\u578b {}<br \/>\n    \/\/ \u5c31\u662f\u957f\u8fd9\u4e2a\u6837\u5b50&#xff0c;\u800c\u8981\u662f\u6211\u4eec\u4f7f\u7528\u53d8\u91cf\u63a5\u6536\u7684\u8bdd&#xff1a;<br \/>\n    \/\/ auto func&#061;[] () -&gt; \u8fd4\u56de\u503c\u7684\u7c7b\u578b {}<br \/>\n    \/\/ \u8c03\u7528\u8be5\u51fd\u6570\u5c31\u662f func()<\/p>\n<p>    \/\/ lambda\u8868\u8fbe\u5f0f\u7684\u683c\u5f0f&#xff1a; [capture-list] (parameters)-&gt; return type {function boby }<\/p>\n<p>    \/\/ \u90a3\u4e48\u683c\u5f0f\u5c31\u662f\u90a3\u6837\u5b50\u4e86&#xff0c;\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u8bf4&#061;\u8be6\u7ec6\u89e3\u6790\u5404\u4e2a\u62ec\u53f7\u662f\u4ec0\u4e48\u4ee5\u53ca\u8981\u653e\u4ec0\u4e48<br \/>\n    \/\/ \u6211\u4eec\u5148\u8bf4\u548c\u6211\u4eec\u5e73\u65f6\u51fd\u6570\u6709\u5173\u8054\u7684<br \/>\n    \/\/ \u6211\u4eec\u5148\u5199\u4e2a\u666e\u901a\u51fd\u6570<br \/>\n    int add(int a, int b)<br \/>\n    {<br \/>\n        return a &#043; b;<br \/>\n    }<\/p>\n<p>    \/\/ \u7b2c\u4e00\u4e2a\u662f () ,\u6211\u4eec\u5c31\u662f\u8981\u5f80\u8fd9\u91cc\u9762\u653e\u5f62\u53c2&#xff0c;\u5c31\u662f\u7c7b\u4f3c\u6b63\u5e38\u51fd\u6570\u7684()&#xff0c;\u91cc\u9762\u8981\u5199\u5f62\u53c2\u7c7b\u578b&#043;\u5f62\u53c2\u540d\u5b57<br \/>\n    \/\/ \u5c31\u662f\u76f8\u5f53\u4e8e\u4e0a\u9762\u666e\u901a\u51fd\u6570\u7684 (int a,int b) \u90e8\u5206&#xff0c;\u800c\u8981\u662f\u8be5\u51fd\u6570\u7528\u4e0d\u5230\u53c2\u6570\u7684\u8bdd<br \/>\n    \/\/ \u4e5f\u53ef\u4ee5\u628a()\u7701\u7565\u6389<br \/>\n    \/\/ \u8fd9\u4e2a\u8fd8\u662f\u5f88\u7b80\u5355\u7684&#xff0c;\u5927\u5bb6\u80af\u5b9a\u968f\u624b\u62ff\u634f<\/p>\n<p>    \/\/ \u7b2c\u4e8c\u4e2a\u5c31\u662f -&gt; \u8fd4\u56de\u503c\u7684\u7c7b\u578b \u90a3\u4e48\u8fd9\u4e2a\u5c31\u66f4\u7b80\u5355\u4e86&#xff0c;\u8fd9\u4e2a\u5c31\u662f\u8981\u5728-&gt;\u540e\u9762\u52a0\u4e0a\u4f60\u6240\u5199\u7684\u8fd9\u4e2a\u51fd\u6570\u7684<br \/>\n    \/\/ \u8fd4\u56de\u503c\u7684\u7c7b\u578b&#xff0c;\u5c31\u662f\u76f8\u5f53\u4e8e\u6b63\u5e38\u51fd\u6570\u4e2d\u7684\u6700\u524d\u9762\u7684\u8fd4\u56de\u503c\u7c7b\u578b<br \/>\n    \/\/ \u5c31\u662f\u76f8\u5f53\u4e8e\u4e0a\u9762\u666e\u901a\u51fd\u6570\u7684 int \u90e8\u5206<br \/>\n    \/\/ \u4e5f\u662f\u5f88\u7b80\u5355&#xff0c;\u90a3\u4e48\u6709\u6240\u4e0d\u540c\u7684\u5c31\u662f\u8bf4&#xff0c;\u5728lambda\u8868\u8fbe\u5f0f\u4e2d&#xff0c;\u6211\u4eec\u662f\u53ef\u4ee5\u7701\u7565 -&gt; \u8fd4\u56de\u503c\u7c7b\u578b \u7684<br \/>\n    \/\/ \u5373lambda\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7b80\u5316\u4e3a [] () {}  &#xff0c;\u662f\u7684&#xff0c;\u5c31\u662f\u8fd9\u4e48\u725b\u6ce2\u4e00<br \/>\n    \/\/ \u4e4b\u6240\u4ee5\u80fd\u8fd9\u6837\u7684\u539f\u56e0\u662f\u5728\u4e8e\u7f16\u8bd1\u5668\u53ef\u4ee5\u81ea\u52a8\u63a8\u5bfc\u51fa\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u7684&#xff0c;\u6240\u4ee5\u4e5f\u5c31\u4e0d\u9700\u8981\u6211\u4eec\u663e\u5f0f\u6307\u5b9a<br \/>\n    \/\/ \u4f46\u662f\u6211\u4eec\u8981\u6ce8\u610f&#xff0c;\u5c31\u662f\u8bf4\u8981\u662f\u51fd\u6570\u4f53\u5185\u90e8\u8fd4\u56de\u662f\u4f7f\u7528if else\u8bed\u53e5\u8fd4\u56de\u4e0d\u540c\u7c7b\u578b\u7684\u8bdd<br \/>\n    \/\/ \u90a3\u4e48\u6211\u4eec\u5c31\u5f97\u663e\u5f0f\u6307\u5b9a\u8fd4\u56de\u503c\u7c7b\u578b&#xff01;&#xff01;&#xff01;<\/p>\n<p>    \/\/ \u7b2c\u4e09\u4e2a\u5c31\u662f {} ,\u8fd9\u4e2a\u4e5f\u662f\u5f88\u7b80\u5355&#xff0c;\u6211\u4eec\u5c31\u662f\u8981\u5f80\u8fd9\u91cc\u9762\u653e\u51fd\u6570\u4e3b\u4f53&#xff0c;\u7b80\u79f0\u51fd\u6570\u4f53<br \/>\n    \/\/ \u90a3\u4e48\u5c31\u662f\u6211\u4eec\u5e73\u65f6\u6b63\u5e38\u51fd\u6570\u4e2d\u5f80{}\u91cc\u9762\u653e\u4ec0\u4e48&#xff0c;\u6211\u4eec\u5c31\u600e\u4e48\u5f80lambda\u4e2d\u7684{}\u600e\u4e48\u653e<br \/>\n    \/\/ \u4ed6\u5c31\u662f\u76f8\u5f53\u4e8e\u4e3a\u51fd\u6570\u7684\u4e3b\u4f53&#xff0c;\u4e5f\u5c31\u662f\u51fd\u6570\u7684\u4e3b\u8981\u8fd0\u884c\u903b\u8f91&#xff01;&#xff01;&#xff01;<br \/>\n    \/\/ \u5c31\u662f\u76f8\u5f53\u4e8e\u4e0a\u9762\u666e\u901a\u51fd\u6570\u7684<br \/>\n    \/\/  {<br \/>\n    \/\/      return a&#043;b;<br \/>\n    \/\/  }<br \/>\n    \/\/ \u90e8\u5206<\/p>\n<p>    \/\/ \u90a3\u4e48\u524d\u9762\u8bf4\u4e86lambda\u8868\u8fbe\u5f0f\u7684\u672c\u8d28\u662f\u533f\u540d\u51fd\u6570\u5bf9\u8c61&#xff0c;\u6240\u4ee5\u5728\u6b63\u5e38\u7684lambda\u8868\u8fbe\u5f0f\u4e2d\u81ea\u7136\u6ca1\u6709\u653e\u51fd\u6570\u540d\u5b57\u7684\u5730\u65b9<br \/>\n    \/\/ \u5f53\u7136&#xff0c;\u4f60\u8981\u662f\u4f7f\u7528\u76f8\u5bf9\u5e94\u7684\u53d8\u91cf\u63a5\u6536\u8be5lambda\u8868\u8fbe\u5f0f\u7684\u8bdd<br \/>\n    \/\/ \u90a3\u4e48\u8be5\u53d8\u91cf\u540d\u5b57\u5c31\u76f8\u5f53\u4e8e\u662f\u51fd\u6570\u540d\u5b57&#xff0c;\u5373\u76f8\u5f53\u4e8e\u4e0a\u9762\u666e\u901a\u51fd\u6570\u7684add<\/p>\n<p>    \/\/ \u2022 (parameters) &#xff1a;\u53c2\u6570\u5217\u8868&#xff0c;\u4e0e\u666e\u901a\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u529f\u80fd\u7c7b\u4f3c&#xff0c;<br \/>\n    \/\/ \u5982\u679c\u4e0d\u9700\u8981\u53c2\u6570\u4f20\u9012&#xff0c;\u5219\u53ef\u4ee5\u8fde\u540c()\u4e00\u8d77\u7701\u7565<\/p>\n<p>    \/\/ \u2022 -&gt;return type &#xff1a;\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u7528\u8ffd\u8e2a\u8fd4\u56de\u7c7b\u578b\u5f62\u5f0f\u58f0\u660e\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u6ca1\u6709\u8fd4\u56de\u503c\u65f6\u6b64\u90e8\u5206\u53ef\u7701\u7565\u3002<br \/>\n    \/\/ \u4e00\u822c\u8fd4\u56de\u503c\u7c7b\u578b\u660e\u786e\u60c5\u51b5\u4e0b&#xff0c;\u4e5f\u53ef\u7701\u7565&#xff0c;\u7531\u7f16\u8bd1\u5668\u5bf9\u8fd4\u56de\u7c7b\u578b\u8fdb\u884c\u63a8\u5bfc\u3002<\/p>\n<p>    \/\/ \u2022 {function boby} &#xff1a;\u51fd\u6570\u4f53&#xff0c;\u51fd\u6570\u4f53\u5185\u7684\u5b9e\u73b0\u8ddf\u666e\u901a\u51fd\u6570\u5b8c\u5168\u7c7b\u4f3c&#xff0c;<br \/>\n    \/\/ \u5728\u8be5\u51fd\u6570\u4f53\u5185&#xff0c;\u9664\u4e86\u53ef\u4ee5\u4f7f\u7528\u5176\u53c2\u6570\u5916&#xff0c;\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6240\u6709\u6355\u83b7\u5230\u7684\u53d8\u91cf&#xff0c;\u51fd\u6570\u4f53\u4e3a\u7a7a\u4e5f\u4e0d\u80fd\u7701\u7565\u3002<\/p>\n<p>    \/\/ \u63a5\u4e0b\u6765\u7684\u5c31\u662f[]\u90e8\u5206&#xff0c;\u8fd9\u4e2a\u7684\u540d\u5b57\u53eb\u505a\u6355\u6349\u5217\u8868<br \/>\n    \/\/ \u8fd9\u4e2a\u7b97\u662flambda\u8868\u8fbe\u5f0f\u7684\u6838\u5fc3\u90e8\u5206\u4e86<br \/>\n    \/\/ \u90a3\u4e48\u6211\u4eec\u60f3\u60f3\u770b&#xff0c;\u51fd\u6570\u4f53\u5185\u90e8\u662f\u4e0d\u662f\u53ef\u80fd\u4f1a\u7528\u5230\u51fd\u6570\u524d\u9762\u7684\u53d8\u91cf\u6216\u8005\u6570\u636e\u5462&#xff1f;&#xff1f;&#xff1f;<br \/>\n    \/\/ \u90a3\u4e48\u5982\u679c\u6211\u4eec\u53ea\u6709\u5f62\u53c2&#xff08;\u5373\u4f20\u5165\u7684\u53c2\u6570&#xff09;\u7684\u8bdd&#xff0c;\u51fd\u6570\u4f53\u5185\u90e8\u662f\u80af\u5b9a\u65e0\u6cd5\u7528\u5230\u524d\u9762\u7684\u6570\u636e\u7684<br \/>\n    \/\/ \u867d\u7136\u8bf4&#xff0c;\u6211\u4eec\u7684\u666e\u901a\u51fd\u6570\u662f\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u5230\u975e\u4f20\u53c2\u7684\u975e\u5168\u5c40or\u9759\u6001\u7684\u53d8\u91cf&#xff0c;\u4f46\u662f\u90fd\u662f\u53eb\u505a\u666e\u901a\u51fd\u6570\u4e86&#xff0c;\u6240\u4ee5\u80af\u5b9alow\u4e00\u4e0b<br \/>\n    \/\/ \u90a3\u4e48lambda\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u8f7b\u91cf\u7ea7\u53ef\u76f4\u63a5\u5728\u5176\u4ed6\u51fd\u6570\u5185\u90e8\u5199\u7684\u533f\u540d\u51fd\u6570\u5bf9\u8c61&#xff0c;\u5b83\u80af\u5b9a\u8981\u9ad8\u7ea7\u4e00\u70b9<br \/>\n    \/\/ \u6240\u4ee5\u5b83\u5c31\u5f97\u53ef\u4ee5\u83b7\u53d6\u5230\u5728\u5b83\u524d\u9762\u7684\u975e\u4f20\u53c2\u7684\u975e\u5168\u5c40or\u9759\u6001\u7684\u53d8\u91cf&#xff0c;\u90a3\u4e48\u5728\u4ed6\u540e\u9762\u80af\u5b9a\u65e0\u6cd5\u83b7\u53d6<br \/>\n    \/\/ \u6bd5\u7adf\u7f16\u8bd1\u5668\u7f16\u8bd1\u662f\u4ece\u4e0a\u5230\u4e0b\u7684&#xff01;&#xff01;&#xff01;<br \/>\n    \/\/ \u90a3\u4e48lambda\u8868\u8fbe\u5f0f\u662f\u4f9d\u9760\u4ec0\u4e48\u53bb\u83b7\u53d6\u5230\u5b83\u524d\u9762\u7684\u975e\u4f20\u53c2\u7684\u975e\u5168\u5c40or\u9759\u6001\u7684\u53d8\u91cf\u5462&#xff1f;&#xff1f;<br \/>\n    \/\/ \u7b54\u6848\u5c31\u662f[]\u2014\u2014\u6355\u6349\u5217\u8868&#xff01;&#xff01;&#xff01;<br \/>\n    \/\/ \u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u5b83\u53bb\u83b7\u53d6\u5230lambda\u8868\u8fbe\u5f0f\u524d\u9762\u7684\u975e\u4f20\u53c2\u7684\u975e\u5168\u5c40or\u9759\u6001\u7684\u53d8\u91cf<\/p>\n<p>    \/\/ \u90a3\u4e48\u5177\u4f53\u662f\u600e\u4e48\u4f7f\u7528\u6355\u6349\u5217\u8868\u7684\u5462&#xff1f;&#xff1f;&#xff1f;<br \/>\n    \/\/ \u6709\u633a\u591a\u79cd\u65b9\u5f0f&#xff0c;\u6211\u4eec\u4e00\u79cd\u4e00\u79cd\u6765\u8bf4<br \/>\n    \/\/ \u7b2c\u4e00\u79cd\u5c31\u662f\u76f4\u63a5\u628alambda\u8868\u8fbe\u5f0f\u524d\u9762\u7684\u975e\u4f20\u53c2\u7684\u975e\u5168\u5c40or\u9759\u6001\u7684\u53d8\u91cf\u540d\u5b57\u5199\u5165<br \/>\n    \/\/ \u6bd4\u5982\u8fd9\u6837\u5b50&#xff1a;<br \/>\n    int main()<br \/>\n    {<br \/>\n        int a &#061; 10;<br \/>\n        double d &#061; 1.1;<br \/>\n        char c &#061; &#039;a&#039;;<br \/>\n        int t &#061; 0;<\/p>\n<p>        \/\/ lambda\u8868\u8fbe\u5f0f<br \/>\n        auto func &#061; [a, d, c](int num) -&gt; int<br \/>\n        {int sum&#061;a&#043;d&#043;c&#043;num; return sum; };<br \/>\n        int ret &#061; func(t);<\/p>\n<p>        \/\/ \u53ef\u4ee5\u770b\u5230&#xff0c;\u8fd9\u91cc\u6211\u4eec\u5c31\u662f\u5728[]\u6355\u6349\u5217\u8868\u4e2d\u5c06lambda\u8868\u8fbe\u5f0f\u524d\u9762\u7684\u975e\u4f20\u53c2\u7684\u975e\u5168\u5c40or\u9759\u6001\u7684\u53d8\u91cf\u540d\u5b57\u5199\u5165<br \/>\n        \/\/ \u90a3\u4e48\u5728lambda\u51fd\u6570\u4f53\u4e2d&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528\u5230\u8fd9\u4e9b\u53d8\u91cf&#xff01;&#xff01;&#xff01;<br \/>\n        \/\/ \u4f46\u662f\u5462&#xff0c;\u8981\u6ce8\u610f&#xff0c;\u8fd9\u91cc\u7684\u6355\u6349&#xff0c;\u53ea\u662f\u4f20\u503c\u6355\u6349<br \/>\n        \/\/ \u610f\u601d\u5c31\u662f\u8bf4&#xff0c;\u5bf9\u4e8e\u76f4\u63a5\u5199\u53d8\u91cf\u540d\u5b57\u7684\u6355\u6349\u800c\u8a00<br \/>\n        \/\/ \u5b83\u4eec\u7684\u524d\u9762\u90fd\u6709\u4e2aconst\u4fee\u9970&#xff0c;\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\u8bf4&#xff0c;\u5bf9\u4e8e\u8fd9\u4e9b\u53d8\u91cf<br \/>\n        \/\/ \u6211\u4eec\u53ea\u80fd\u5728\u51fd\u6570\u4f53\u5185\u4f7f\u7528\u5b83\u4eec\u7684\u503c&#xff0c;\u4e0d\u80fd\u53bb\u4fee\u6539\u5b83\u4eec\u7684\u503c&#xff0c;\u5373\u4e0d\u80fd\u4fee\u6539\u8fd9\u4e9b\u53d8\u91cf&#xff01;&#xff01;&#xff01;<\/p>\n<p>        \/\/ lambda\u8868\u8fbe\u5f0f<br \/>\n        auto func1 &#061; [a, d, c](int num) -&gt; int<br \/>\n        {<br \/>\n            \/\/&#043;&#043;a;\/\/\u62a5\u9519&#xff0c;\u8868\u8fbe\u5f0f\u5fc5\u987b\u662f\u53ef\u4fee\u6539\u7684\u5de6\u503c<br \/>\n            int sum &#061; a &#043; d &#043; c &#043; num;<br \/>\n            return sum;<br \/>\n        };<br \/>\n        int ret1 &#061; func1(t);<\/p>\n<p>        \/\/ \u90a3\u4e48\u4f60\u8981\u662f\u8bf4\u6211\u5c31\u662f\u60f3\u4fee\u6539\u6355\u6349\u5230\u7684\u53d8\u91cf\u503c&#xff0c;\u8bf6&#xff0c;\u90a3\u4e48\u6709\u4e24\u79cd\u65b9\u5f0f\u4f9b\u4f60\u9009\u62e9<br \/>\n        \/\/ \u7b2c\u4e00\u79cd\u5c31\u662f\u5728)\u540e\u9762\u52a0mutable&#xff0c;\u8868\u793a\u4e3a\u53ef\u53d8\u7684,\u76f8\u5f53\u4e8e\u662f\u5728\u51fd\u6570\u58f0\u660e\u540e\u9762\u6dfb\u52a0mutable<br \/>\n        \/\/ \u90a3\u4e48\u6b64\u65f6\u4f60\u5c31\u80fd\u5bf9\u6355\u6349\u5230\u7684\u53d8\u91cf\u7684\u503c\u8fdb\u884c\u4fee\u6539&#xff0c;\u4f46\u662f\u6ce8\u610f&#xff0c;\u8fd9\u91cc\u53ea\u662f\u4fee\u6539\u4e86\u51fd\u6570\u4f53\u5185\u53ef\u4f7f\u7528\u7684\u503c<br \/>\n        \/\/ \u800c\u5bf9\u4e8e\u5916\u9762\u7684\u88ab\u6355\u6349\u5230\u7684\u53d8\u91cf\u800c\u8a00&#xff0c;\u5b83\u4eec\u7684\u503c\u662f\u6ca1\u6709\u88ab\u4fee\u6539\u7684<br \/>\n        \/\/ \u603b\u7ed3\u4e00\u4e0b\u8fd9\u4e2a\u5c31\u662f\u76f8\u5f53\u4e8e\u662f\u4f20\u503c\u4f20\u53c2&#xff01;&#xff01;&#xff01;<\/p>\n<p>        \/\/ lambda\u8868\u8fbe\u5f0f<br \/>\n        auto func2 &#061; [a, d, c](int num) mutable -&gt; int<br \/>\n        {<br \/>\n            &#043;&#043;a; \/\/ \u4e0d\u62a5\u9519&#xff0c;\u4e0d\u4f1a\u4fee\u6539\u6389\u5916\u9762\u7684a\u7684\u503c<br \/>\n            int sum &#061; a &#043; d &#043; c &#043; num;<br \/>\n            return sum;<br \/>\n        };<br \/>\n        int ret2 &#061; func2(t);<\/p>\n<p>        \/\/ \u90a3\u4e48\u4e0a\u9762\u8bf4\u7684\u6dfb\u52a0mutable\u7684\u65b9\u5f0f\u662f\u53ef\u4ee5\u4fee\u6539\u6355\u6349\u5230\u7684\u53d8\u91cf\u503c<br \/>\n        \/\/ \u4f46\u662f\u4e0d\u4f1a\u5f71\u54cd\u5230\u5916\u9762\u7684\u88ab\u6355\u6349\u5230\u7684\u53d8\u91cf\u7684\u503c<br \/>\n        \/\/ \u90a3\u4e48\u53e6\u4e00\u79cd\u65b9\u5f0f\u5c31\u662f\u53ef\u4ee5\u5f71\u54cd\u5230\u5916\u9762\u7684\u88ab\u6355\u6349\u5230\u7684\u53d8\u91cf\u7684\u503c<br \/>\n        \/\/ \u4e5f\u5c31\u662f\u5728\u8981\u6355\u6349\u7684\u53d8\u91cf\u540d\u5b57\u524d\u9762\u52a0&amp;<br \/>\n        \/\/ \u8fd9\u4e2a\u5c31\u662f\u76f8\u5f53\u4e8e\u5f15\u7528\u4f20\u53c2&#xff01;&#xff01;&#xff01;<br \/>\n        \/\/ \u5c31\u662f\u76f8\u5f53\u4e8e\u6211\u4eec\u5e73\u65f6\u51fd\u6570\u5f62\u53c2\u7c7b\u578b\u4e3a  \u7c7b\u578b&amp; \u5f62\u53c2\u540d\u5b57<br \/>\n        \/\/ \u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u4f53\u5185\u90e8\u4fee\u6539\u5f62\u53c2\u800c\u5f71\u54cd\u5230\u88ab\u4f20\u9012\u7684\u5b9e\u53c2<br \/>\n        \/\/ \u5728lambda\u4e2d&#xff0c;\u5f53\u6211\u4eec\u5728[]\u6355\u6349\u5217\u8868\u4e2d\u8981\u6355\u6349\u7684\u53d8\u91cf\u540d\u5b57\u524d\u9762\u52a0&amp;\u7684\u8bdd<br \/>\n        \/\/ \u5c31\u662f\u76f8\u5f53\u4e8e\u8fd9\u6837\u5b50\u7684\u529f\u6548<\/p>\n<p>        \/\/ lambda\u8868\u8fbe\u5f0f<br \/>\n        auto func3 &#061; [&amp;a, &amp;d, &amp;c](int num) -&gt; int<br \/>\n        {<br \/>\n            &#043;&#043;a; \/\/ \u4f1a\u4fee\u6539\u6389\u5916\u9762\u7684a\u7684\u503c<br \/>\n            int sum &#061; a &#043; d &#043; c &#043; num;<br \/>\n            return sum;<br \/>\n        };<br \/>\n        int ret3 &#061; func3(t);<br \/>\n        cout &lt;&lt; a &lt;&lt; endl;<\/p>\n<p>        \/\/ \u90a3\u4e48&#xff0c;\u8981\u662f\u6211\u4eec\u6709\u4e9b\u53d8\u91cf\u60f3\u4fee\u6539\u503c&#xff0c;\u6709\u4e9b\u53d8\u91cf\u4e0d\u60f3\u7684\u8bdd&#xff0c;\u5e94\u8be5\u600e\u4e48\u529e\u5462&#xff1f;&#xff1f;<br \/>\n        \/\/ \u5176\u5b9e\u5f88\u7b80\u5355&#xff0c;\u5c31\u662f\u65e2\u7528\u76f4\u63a5\u53d8\u91cf\u540d\u5b57&#xff0c;\u53c8\u7528&amp;\u53d8\u91cf\u540d\u5b57\u5373\u53ef&#xff0c;\u8fd9\u4e2a\u8fd8\u662f\u5f88\u7b80\u5355\u7684<br \/>\n        \/\/ lambda\u8868\u8fbe\u5f0f<br \/>\n        auto func4 &#061; [&amp;a, d, c](int num) -&gt; int<br \/>\n        {<br \/>\n            &#043;&#043;a; \/\/ \u4f1a\u4fee\u6539\u6389\u5916\u9762\u7684a\u7684\u503c<br \/>\n            int sum &#061; a &#043; d &#043; c &#043; num;<br \/>\n            return sum;<br \/>\n        };<br \/>\n        int ret4 &#061; func4(t);<br \/>\n        cout &lt;&lt; a &lt;&lt; endl;<\/p>\n<p>        \/\/ \u90a3\u4e48\u8fd8\u6709\u5c31\u662f&#xff0c;\u8981\u662f\u6211\u4eec\u4e0d\u60f3\u81ea\u5df1\u6355\u6349&#xff0c;\u6211\u5c31\u662f\u61d2&#xff0c;\u90a3\u4e48\u8981\u600e\u4e48\u529e\u5462&#xff1f;&#xff1f;&#xff1f;<br \/>\n        \/\/  \u90a3\u4e48\u5c31\u662f\u6355\u6349\u5217\u8868\u4e2d\u9690\u5f0f\u6355\u6349&#xff0c;<br \/>\n        \/\/  \u4eec\u5728\u6355\u6349\u5217\u8868\u5199\u4e00\u4e2a&#061;\u8868\u793a\u9690\u5f0f\u503c\u6355\u6349&#xff0c;<br \/>\n        \/\/  \u5728\u6355\u6349\u5217\u8868\u5199\u4e00\u4e2a&amp;\u8868\u793a\u9690\u5f0f\u5f15\u7528\u6355\u6349&#xff0c;<br \/>\n        \/\/  \u8fd9\u6837\u6211\u4eeclambda \u8868\u8fbe\u5f0f\u4e2d\u7528\u4e86\u90a3\u4e9b\u53d8\u91cf&#xff0c;\u7f16\u8bd1\u5668\u5c31\u4f1a\u81ea\u52a8\u6355\u6349\u90a3\u4e9b\u53d8\u91cf\u3002<br \/>\n        \/\/  \u7f16\u8bd1\u5668\u53ef\u4e0d\u4f1a\u8822\u5f97\u628a\u524d\u9762\u7684\u6240\u6709\u53d8\u91cf\u5168\u90e8\u6355\u6349&#xff01;&#xff01;&#xff01;<br \/>\n        \/\/  \u4f46\u662f\u8981\u6ce8\u610f\u7684\u662f&#xff1a;\u53ef\u4e0d\u80fd\u540c\u65f6\u5199&#061;\u548c&amp;&#xff0c;\u8fd9\u662f\u4e0d\u88ab\u5141\u8bb8\u7684&#xff0c;\u6bd5\u7adf\u8fd9\u4e5f\u592a\u4e3a\u96be\u7f16\u8bd1\u5668\u4e86<br \/>\n        \/\/  \u90a3\u4e48\u540c\u6837\u7684\u9053\u7406&#xff0c;\u6211\u4eec\u4e5f\u53ef\u4ee5\u5199\u4e00\u4e2a&amp; \u7136\u540e\u5bf9\u4e8e\u4e0d\u60f3\u6539\u53d8\u503c\u7684\u53d8\u91cf&#xff0c;\u5c31\u518d\u5355\u72ec\u5199\u51fa\u6765<br \/>\n        \/\/  &#061;\u4e5f\u662f&#xff0c;\u5bf9\u4e8e\u60f3\u6539\u53d8\u503c\u7684\u53d8\u91cf&#xff0c;\u6211\u4eec\u518d\u5355\u72ec\u5199\u51fa\u6765\u5373\u53ef&#xff0c;\u4e0b\u9762\u662f\u793a\u4f8b<\/p>\n<p>        auto func5 &#061; [&amp;](int num) -&gt; int<br \/>\n        {<br \/>\n            &#043;&#043;a; \/\/ \u4f1a\u4fee\u6539\u6389\u5916\u9762\u7684a\u7684\u503c<br \/>\n            int sum &#061; a &#043; d &#043; c &#043; num;<br \/>\n            return sum;<br \/>\n        };<br \/>\n        int ret5 &#061; func5(t);<\/p>\n<p>        auto func6 &#061; [&#061;](int num) -&gt; int<br \/>\n        {<br \/>\n            \/\/&#043;&#043;a;\/\/\u62a5\u9519<br \/>\n            int sum &#061; a &#043; d &#043; c &#043; num;<br \/>\n            return sum;<br \/>\n        };<br \/>\n        int ret6 &#061; func6(t);<\/p>\n<p>        auto func7 &#061; [&amp;, d](int num) -&gt; int<br \/>\n        {<br \/>\n            &#043;&#043;a; \/\/ \u4f1a\u4fee\u6539\u6389\u5916\u9762\u7684a\u7684\u503c<br \/>\n            \/\/&#043;&#043;d;\/\/\u62a5\u9519<br \/>\n            int sum &#061; a &#043; d &#043; c &#043; num;<br \/>\n            return sum;<br \/>\n        };<br \/>\n        int ret7 &#061; func7(t);<\/p>\n<p>        auto func8 &#061; [&#061;, &amp;d](int num) -&gt; int<br \/>\n        {<br \/>\n            \/\/&#043;&#043;a;\/\/\u62a5\u9519<br \/>\n            &#043;&#043;d; \/\/ \u4e0d\u62a5\u9519<br \/>\n            int sum &#061; a &#043; d &#043; c &#043; num;<br \/>\n            return sum;<br \/>\n        };<br \/>\n        int ret8 &#061; func8(t);<\/p>\n<p>        return 0;<br \/>\n    }<\/p>\n<p>    \/\/ \u90a3\u4e48\u53ef\u80fd\u5c31\u6709\u4eba\u597d\u5947\u4e86&#xff0c;<br \/>\n    \/\/ lambda\u6709\u4ec0\u4e48\u7528\u5462&#xff1f;&#xff1f;&#xff1f;\u4f5c\u7528\u5728\u54ea\u91cc\u5462&#xff1f;&#xff1f;&#xff1f;<br \/>\n    \/\/ \u90a3\u4e48\u80af\u5b9a\u662f\u5f88\u591a\u7684<br \/>\n    \/\/ \u9664\u4e86\u7ebf\u7a0b\u4e4b\u5916<br \/>\n    \/\/ \u6211\u4eec\u8fd8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u53bb\u66ff\u4ee3\u4eff\u51fd\u6570\u4f20\u5165\u7c7b\u6a21\u7248\u4e2d\u8fdb\u884c\u6bd4\u8f83\u5927\u5c0f<br \/>\n    \/\/ \u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u793a\u4f8b&#xff1a;<br \/>\n    struct Goods<br \/>\n    {<br \/>\n        string _name;  \/\/ \u540d\u5b57<br \/>\n        double _price; \/\/ \u4ef7\u683c<br \/>\n        int _evaluate; \/\/ \u8bc4\u4ef7<br \/>\n        \/\/ &#8230;<br \/>\n        Goods(const char *str, double price, int evaluate)<br \/>\n            : _name(str), _price(price), _evaluate(evaluate)<br \/>\n        {<br \/>\n        }<br \/>\n    };<br \/>\n    struct ComparePriceLess<br \/>\n    {<br \/>\n        bool operator()(const Goods &amp;gl, const Goods &amp;gr)<br \/>\n        {<br \/>\n            return gl._price &lt; gr._price;<br \/>\n        }<br \/>\n    };<br \/>\n    struct ComparePriceGreater<br \/>\n    {<br \/>\n        bool operator()(const Goods &amp;gl, const Goods &amp;gr)<br \/>\n        {<br \/>\n            return gl._price &gt; gr._price;<br \/>\n        }<br \/>\n    };<br \/>\n    int main()<br \/>\n    {<br \/>\n        vector&lt;Goods&gt; v &#061; {{&#034;\u82f9\u679c&#034;, 2.1, 5}, {&#034;\u9999\u8549&#034;, 3, 4}, {&#034;\u6a59\u5b50&#034;, 2.2, 3}, {&#034;\u83e0\u841d&#034;, 1.5, 4}};<br \/>\n        \/\/ \u7c7b\u4f3c\u8fd9\u6837\u7684\u573a\u666f&#xff0c;\u6211\u4eec\u5b9e\u73b0\u4eff\u51fd\u6570\u5bf9\u8c61\u6216\u8005\u51fd\u6570\u6307\u9488\u652f\u6301\u5546\u54c1\u4e2d<br \/>\n        \/\/ \u4e0d\u540c\u9879\u7684\u6bd4\u8f83&#xff0c;\u76f8\u5bf9\u8fd8\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684&#xff0c;\u90a3\u4e48\u8fd9\u91cclambda\u5c31\u5f88\u597d\u7528\u4e86<br \/>\n        sort(v.begin(), v.end(), ComparePriceLess());<br \/>\n        sort(v.begin(), v.end(), ComparePriceGreater());<\/p>\n<p>        sort(v.begin(), v.end(), [](const Goods &amp;g1, const Goods &amp;g2)<br \/>\n             { return g1._price &lt; g2._price; });<br \/>\n        sort(v.begin(), v.end(), [](const Goods &amp;g1, const Goods &amp;g2)<br \/>\n             { return g1._price &gt; g2._price; });<br \/>\n        sort(v.begin(), v.end(), [](const Goods &amp;g1, const Goods &amp;g2)<br \/>\n             { return g1._evaluate &lt; g2._evaluate; });<br \/>\n        sort(v.begin(), v.end(), [](const Goods &amp;g1, const Goods &amp;g2)<br \/>\n             { return g1._evaluate &gt; g2._evaluate; });<br \/>\n        return 0;<br \/>\n    }<br \/>\n    \/\/ \u53ef\u4ee5\u770b\u5230&#xff0c;\u6211\u4eec\u4f7f\u7528lambda\u7684\u8bdd&#xff0c;\u5c31\u4f1a\u65b9\u4fbf\u5f88\u591a&#xff0c;\u4e5f\u76f4\u89c2\u5f88\u591a&#xff01;&#xff01;&#xff01;<\/p>\n<p>    \/\/ \u90a3\u4e48\u8fd9\u91cc\u518d\u7b80\u5355\u63d0\u4e00\u5634&#xff0c;lambda\u7684\u539f\u7406\u5c31\u662f\u5f62\u6210\u4eff\u51fd\u6570\u54e6<br \/>\n    \/*<br \/>\n    \u4e00\u3001Lambda \u6838\u5fc3\u672c\u8d28<br \/>\n    Lambda \u8868\u8fbe\u5f0f\u662f C&#043;&#043;11 \u5f15\u5165\u7684\u533f\u540d\u51fd\u6570\u5bf9\u8c61&#xff0c;\u6838\u5fc3\u7279\u70b9\u7684\u662f\u7a81\u7834\u666e\u901a\u51fd\u6570\u7684\u9650\u5236 \u2014\u2014<br \/>\n    \u53ef\u76f4\u63a5\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8&#xff0c;\u65e0\u9700\u50cf\u666e\u901a\u51fd\u6570\u90a3\u6837\u53ea\u80fd\u5728\u5168\u5c40\u6216\u547d\u540d\u7a7a\u95f4\u4e2d\u58f0\u660e\u5b9a\u4e49&#xff1b;<br \/>\n    \u8bed\u6cd5\u5c42\u9762\u6ca1\u6709\u663e\u5f0f\u7c7b\u578b&#xff0c;\u5fc5\u987b\u901a\u8fc7auto\u3001\u51fd\u6570\u6a21\u677f\u6216std::function\u63a5\u6536&#xff1b;<br \/>\n    \u5e95\u5c42\u672c\u8d28\u662f\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7684\u4eff\u51fd\u6570\u7c7b&#xff08;\u540e\u7eed\u539f\u7406\u90e8\u5206\u4f1a\u8be6\u7ec6\u62c6\u89e3&#xff09;&#xff0c;<br \/>\n    \u6700\u5927\u4f18\u52bf\u662f\u652f\u6301\u6355\u6349\u5916\u5c42\u4f5c\u7528\u57df\u7684\u53d8\u91cf&#xff0c;\u8fd9\u662f\u666e\u901a\u51fd\u6570\u65e0\u6cd5\u76f4\u63a5\u5b9e\u73b0\u7684\u529f\u80fd\u3002<\/p>\n<p>    \u4e8c\u3001Lambda \u5b8c\u6574\u8bed\u6cd5\u683c\u5f0f&#xff08;\u9010\u90e8\u5206\u62c6\u89e3&#xff09;<br \/>\n    Lambda \u7684\u6807\u51c6\u8bed\u6cd5\u683c\u5f0f\u4e3a&#xff1a;[capture-list] (parameters) -&gt; return-type { function-body }&#xff0c;<br \/>\n    \u6bcf\u4e2a\u90e8\u5206\u7684\u4f5c\u7528\u3001\u89c4\u5219\u548c\u7701\u7565\u6761\u4ef6\u90fd\u6709\u660e\u786e\u8981\u6c42&#xff0c;\u7f3a\u4e00\u4e0d\u53ef&#xff08;\u7b26\u5408\u7701\u7565\u89c4\u5219\u7684\u9664\u5916&#xff09;\u3002<\/p>\n<p>    &#xff08;\u4e00&#xff09;[capture-list] \u6355\u6349\u5217\u8868&#xff08;\u6838\u5fc3\u91cd\u70b9&#xff0c;\u6700\u6613\u51fa\u9519&#xff09;<br \/>\n    \u6355\u6349\u5217\u8868\u662f Lambda \u7684\u8d77\u59cb\u6807\u8bc6&#xff0c;\u4e5f\u662f\u6700\u6838\u5fc3\u7684\u90e8\u5206&#xff0c;<br \/>\n    \u4f5c\u7528\u662f\u6355\u6349 Lambda \u5b9a\u4e49\u4f4d\u7f6e\u7684\u5916\u5c42\u4f5c\u7528\u57df\u53d8\u91cf&#xff0c;\u4f9b\u51fd\u6570\u4f53\u5185\u90e8\u4f7f\u7528&#xff0c;\u6838\u5fc3\u89c4\u5219\u548c\u6355\u6349\u65b9\u5f0f\u5982\u4e0b&#xff1a;<br \/>\n    1. \u57fa\u7840\u89c4\u5219<br \/>\n    \u5fc5\u987b\u4f5c\u4e3a Lambda \u7684\u5f00\u5934&#xff0c;\u54ea\u6015\u4e0d\u9700\u8981\u6355\u6349\u4efb\u4f55\u53d8\u91cf&#xff0c;[]\u4e5f\u4e0d\u80fd\u7701\u7565<br \/>\n    &#xff08;\u8fd9\u662f\u7f16\u8bd1\u5668\u8bc6\u522b Lambda \u7684\u5173\u952e\u6807\u8bc6&#xff09;&#xff1b;<br \/>\n    \u53ea\u80fd\u6355\u6349 \u201cLambda \u5b9a\u4e49\u4f4d\u7f6e\u4e4b\u524d\u201d \u7684\u5c40\u90e8\u53d8\u91cf&#xff0c;\u65e0\u6cd5\u6355\u6349\u5b9a\u4e49\u4e4b\u540e\u58f0\u660e\u7684\u5c40\u90e8\u53d8\u91cf&#xff1b;<br \/>\n    \u9759\u6001\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u4e0d\u9700\u8981\u6355\u6349&#xff0c;\u53ef\u76f4\u63a5\u5728 Lambda \u51fd\u6570\u4f53\u4e2d\u4f7f\u7528<br \/>\n    &#xff08;\u56e0\u4e3a\u5b83\u4eec\u7684\u4f5c\u7528\u57df\u662f\u5168\u5c40\u53ef\u89c1\u7684&#xff0c;\u4e0d\u53d7\u5c40\u90e8\u4f5c\u7528\u57df\u9650\u5236&#xff09;&#xff1b;<br \/>\n    \u5982\u679c Lambda \u5b9a\u4e49\u5728\u5168\u5c40\u4f4d\u7f6e&#xff08;\u800c\u975e\u51fd\u6570\u5185\u90e8&#xff09;&#xff0c;\u6355\u6349\u5217\u8868\u5fc5\u987b\u4e3a\u7a7a \u2014\u2014<br \/>\n     \u56e0\u4e3a\u5168\u5c40\u4f5c\u7528\u57df\u6ca1\u6709 \u201c\u5916\u5c42\u5c40\u90e8\u53d8\u91cf\u201d \u53ef\u6355\u6349\u3002<\/p>\n<p>    2. \u4e09\u79cd\u6355\u6349\u65b9\u5f0f&#xff08;\u8986\u76d6\u6240\u6709\u4f7f\u7528\u573a\u666f&#xff09;<br \/>\n    &#xff08;1&#xff09;\u663e\u5f0f\u6355\u6349&#xff1a;\u7cbe\u51c6\u63a7\u5236\u5355\u4e2a\u53d8\u91cf\u7684\u6355\u6349\u65b9\u5f0f<br \/>\n    \u663e\u5f0f\u6355\u6349\u662f\u6307\u5728\u6355\u6349\u5217\u8868\u4e2d\u660e\u786e\u6307\u5b9a\u6bcf\u4e2a\u53d8\u91cf\u7684\u6355\u6349\u7c7b\u578b&#xff0c;\u591a\u4e2a\u53d8\u91cf\u7528\u9017\u53f7\u5206\u9694&#xff0c;<br \/>\n    \u9002\u7528\u4e8e\u9700\u8981\u7cbe\u51c6\u63a7\u5236\u53d8\u91cf\u8bbf\u95ee\u65b9\u5f0f\u7684\u573a\u666f\u3002<br \/>\n    \u8bed\u6cd5\u683c\u5f0f&#xff1a;[\u53d8\u91cf1, &amp;\u53d8\u91cf2, \u53d8\u91cf3]&#xff0c;\u5176\u4e2d\u4e0d\u52a0&amp;\u8868\u793a \u201c\u503c\u6355\u6349\u201d&#xff08;\u62f7\u8d1d\u5916\u5c42\u53d8\u91cf\u7684\u526f\u672c\u5230 Lambda \u5185\u90e8&#xff09;&#xff0c;\u52a0&amp;\u8868\u793a \u201c\u5f15\u7528\u6355\u6349\u201d&#xff08;\u76f8\u5f53\u4e8e\u7ed9\u5916\u5c42\u53d8\u91cf\u8d77\u522b\u540d&#xff0c;\u76f4\u63a5\u64cd\u4f5c\u539f\u53d8\u91cf&#xff09;&#xff1b;<br \/>\n    \u793a\u4f8b&#xff1a;\u5916\u5c42\u6709\u53d8\u91cfa&#061;10, b&#061;20, c&#061;30&#xff0c;\u5b9a\u4e49auto func &#061; [a, &amp;b]()&#xff0c;<br \/>\n    \u5219a\u662f\u503c\u6355\u6349&#xff08;Lambda \u5185\u90e8\u7528\u7684\u662fa\u7684\u526f\u672c&#xff09;&#xff0c;b\u662f\u5f15\u7528\u6355\u6349&#xff08;Lambda \u5185\u90e8\u64cd\u4f5c\u7684\u662f\u539f\u53d8\u91cfb&#xff09;&#xff0c;<br \/>\n    c\u672a\u88ab\u6355\u6349&#xff0c;\u82e5\u5728\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528c\u4f1a\u76f4\u63a5\u7f16\u8bd1\u62a5\u9519&#xff1b;<br \/>\n    \u6ce8\u610f&#xff1a;\u503c\u6355\u6349\u7684\u53d8\u91cf\u9ed8\u8ba4\u662f\u53ea\u8bfb\u7684&#xff0c;\u65e0\u6cd5\u5728 Lambda \u5185\u90e8\u4fee\u6539&#xff08;\u9664\u975e\u52a0mutable\u4fee\u9970\u7b26&#xff09;\u3002<\/p>\n<p>    &#xff08;2&#xff09;\u9690\u5f0f\u6355\u6349&#xff1a;\u7f16\u8bd1\u5668\u81ea\u52a8\u6355\u6349\u6240\u9700\u53d8\u91cf<br \/>\n    \u9690\u5f0f\u6355\u6349\u4e0d\u9700\u8981\u624b\u52a8\u6307\u5b9a\u53d8\u91cf\u540d&#xff0c;\u53ea\u9700\u8981\u7528&#061;\u6216&amp;\u6307\u5b9a\u9ed8\u8ba4\u6355\u6349\u89c4\u5219&#xff0c;<br \/>\n    \u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u6355\u6349\u51fd\u6570\u4f53\u4e2d\u7528\u5230\u7684\u6240\u6709\u5916\u5c42\u5c40\u90e8\u53d8\u91cf&#xff0c;\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528\u591a\u4e2a\u53d8\u91cf\u3001\u4e14\u6355\u6349\u65b9\u5f0f\u4e00\u81f4\u7684\u573a\u666f\u3002<\/p>\n<p>    \u8bed\u6cd5\u683c\u5f0f 1&#xff1a;[&#061;]\u2014\u2014 \u9690\u5f0f\u503c\u6355\u6349&#xff0c;\u51fd\u6570\u4f53\u4e2d\u7528\u5230\u7684\u6240\u6709\u5916\u5c42\u5c40\u90e8\u53d8\u91cf&#xff0c;\u90fd\u4ee5\u503c\u6355\u6349\u7684\u65b9\u5f0f\u83b7\u53d6\u526f\u672c&#xff1b;<br \/>\n    \u793a\u4f8b&#xff1a;\u5916\u5c42\u6709x&#061;100, y&#061;200, z&#061;300&#xff0c;auto func &#061; [&#061;]()&#xff0c;\u82e5\u51fd\u6570\u4f53\u4e2d\u4f7f\u7528\u4e86x\u548cy&#xff0c;<br \/>\n    \u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u4ee5\u503c\u6355\u6349\u7684\u65b9\u5f0f\u62f7\u8d1dx\u548cy\u7684\u526f\u672c&#xff0c;z\u672a\u88ab\u4f7f\u7528\u5219\u4e0d\u6355\u6349&#xff1b;<\/p>\n<p>    \u8bed\u6cd5\u683c\u5f0f 2&#xff1a;[&amp;]\u2014\u2014 \u9690\u5f0f\u5f15\u7528\u6355\u6349&#xff0c;\u51fd\u6570\u4f53\u4e2d\u7528\u5230\u7684\u6240\u6709\u5916\u5c42\u5c40\u90e8\u53d8\u91cf&#xff0c;\u90fd\u4ee5\u5f15\u7528\u6355\u6349\u7684\u65b9\u5f0f\u5173\u8054\u539f\u53d8\u91cf&#xff1b;<br \/>\n    \u793a\u4f8b&#xff1a;auto func &#061; [&amp;]()&#xff0c;\u51fd\u6570\u4f53\u4e2d\u4fee\u6539x\u7684\u503c&#xff0c;\u4f1a\u76f4\u63a5\u5f71\u54cd\u5916\u5c42\u539f\u53d8\u91cfx&#xff1b;<br \/>\n    \u6ce8\u610f&#xff1a;\u9690\u5f0f\u503c\u6355\u6349\u7684\u53d8\u91cf\u540c\u6837\u9ed8\u8ba4\u53ea\u8bfb&#xff0c;\u9690\u5f0f\u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u53ef\u76f4\u63a5\u4fee\u6539\u539f\u53d8\u91cf\u3002<\/p>\n<p>    &#xff08;3&#xff09;\u6df7\u5408\u6355\u6349&#xff1a;\u9690\u5f0f &#043; \u663e\u5f0f\u7ed3\u5408&#xff08;\u6709\u4e25\u683c\u89c4\u5219&#xff09;<br \/>\n    \u6df7\u5408\u6355\u6349\u662f\u9690\u5f0f\u6355\u6349\u548c\u663e\u5f0f\u6355\u6349\u7684\u7ed3\u5408&#xff0c;\u9002\u7528\u4e8e\u5927\u90e8\u5206\u53d8\u91cf\u91c7\u7528\u540c\u4e00\u6355\u6349\u65b9\u5f0f&#xff0c;<br \/>\n    \u4e2a\u522b\u53d8\u91cf\u9700\u8981\u7279\u6b8a\u5904\u7406\u7684\u573a\u666f&#xff0c;\u6838\u5fc3\u89c4\u5219\u5982\u4e0b&#xff1a;<br \/>\n    \u89c4\u5219 1&#xff1a;\u6355\u6349\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5fc5\u987b\u662f&#061;\u6216&amp;&#xff08;\u5148\u6307\u5b9a\u9ed8\u8ba4\u6355\u6349\u65b9\u5f0f&#xff09;&#xff1b;<\/p>\n<p>    \u89c4\u5219 2&#xff1a;\u82e5\u9ed8\u8ba4\u6355\u6349\u65b9\u5f0f\u662f&#061;&#xff08;\u9690\u5f0f\u503c\u6355\u6349&#xff09;&#xff0c;\u540e\u7eed\u663e\u5f0f\u6307\u5b9a\u7684\u53d8\u91cf\u5fc5\u987b\u662f\u5f15\u7528\u6355\u6349&#xff08;\u5373\u53d8\u91cf\u524d\u52a0&amp;&#xff09;&#xff0c;<br \/>\n    \u4e0d\u80fd\u518d\u662f\u503c\u6355\u6349&#xff08;\u5426\u5219\u7f16\u8bd1\u62a5\u9519&#xff09;&#xff1b;<\/p>\n<p>    \u89c4\u5219 3&#xff1a;\u82e5\u9ed8\u8ba4\u6355\u6349\u65b9\u5f0f\u662f&amp;&#xff08;\u9690\u5f0f\u5f15\u7528\u6355\u6349&#xff09;&#xff0c;\u540e\u7eed\u663e\u5f0f\u6307\u5b9a\u7684\u53d8\u91cf\u5fc5\u987b\u662f\u503c\u6355\u6349&#xff08;\u5373\u53d8\u91cf\u524d\u4e0d\u52a0&amp;&#xff09;&#xff0c;<br \/>\n    \u4e0d\u80fd\u518d\u662f\u5f15\u7528\u6355\u6349&#xff1b;<\/p>\n<p>    \u793a\u4f8b 1&#xff1a;[&#061;, &amp;x]\u2014\u2014 \u9ed8\u8ba4\u503c\u6355\u6349\u6240\u6709\u7528\u5230\u7684\u53d8\u91cf&#xff0c;\u552f\u72ecx\u91c7\u7528\u5f15\u7528\u6355\u6349&#xff1b;<\/p>\n<p>    \u793a\u4f8b 2&#xff1a;[&amp;, y, z]\u2014\u2014 \u9ed8\u8ba4\u5f15\u7528\u6355\u6349\u6240\u6709\u7528\u5230\u7684\u53d8\u91cf&#xff0c;\u552f\u72ecy\u548cz\u91c7\u7528\u503c\u6355\u6349\u3002<\/p>\n<p>    &#xff08;\u4e8c&#xff09;(parameters) \u53c2\u6570\u5217\u8868&#xff08;\u548c\u666e\u901a\u51fd\u6570\u4e00\u81f4&#xff09;<br \/>\n    \u53c2\u6570\u5217\u8868\u7684\u4f5c\u7528\u662f\u63a5\u6536\u5916\u90e8\u4f20\u5165\u7684\u53c2\u6570&#xff0c;\u529f\u80fd\u548c\u666e\u901a\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u5b8c\u5168\u76f8\u540c&#xff0c;\u6838\u5fc3\u89c4\u5219\u548c\u7701\u7565\u6761\u4ef6\u5982\u4e0b&#xff1a;<br \/>\n    \u8bed\u6cd5\u683c\u5f0f\u548c\u666e\u901a\u51fd\u6570\u4e00\u81f4&#xff1a;(\u53c2\u6570\u7c7b\u578b1 \u53c2\u6570\u540d1, \u53c2\u6570\u7c7b\u578b2 \u53c2\u6570\u540d2, &#8230;)&#xff0c;<br \/>\n    \u652f\u6301\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u7b49\u7279\u6027&#xff1b;<br \/>\n    \u7701\u7565\u89c4\u5219&#xff1a;\u5982\u679c Lambda \u4e0d\u9700\u8981\u63a5\u6536\u5916\u90e8\u53c2\u6570&#xff0c;\u53c2\u6570\u5217\u8868\u53ef\u4ee5\u8fde\u540c()\u4e00\u8d77\u7701\u7565&#xff1b;<br \/>\n    \u4f46\u5982\u679c\u4f7f\u7528\u4e86mutable\u4fee\u9970\u7b26&#xff0c;\u5373\u4f7f\u6ca1\u6709\u53c2\u6570&#xff0c;()\u4e5f\u4e0d\u80fd\u7701\u7565&#xff08;\u8fd9\u662fmutable\u7684\u5f3a\u5236\u8981\u6c42&#xff09;&#xff1b;<\/p>\n<p>    \u793a\u4f8b 1&#xff1a;\u6709\u53c2\u6570 \u2014\u2014auto add &#061; [](int x, int y) { return x &#043; y; }&#xff0c;<br \/>\n    \u63a5\u6536\u4e24\u4e2aint\u7c7b\u578b\u53c2\u6570&#xff0c;\u8fd4\u56de\u5b83\u4eec\u7684\u548c&#xff1b;<\/p>\n<p>    \u793a\u4f8b 2&#xff1a;\u65e0\u53c2\u6570&#xff08;\u53ef\u7701\u7565()&#xff09;\u2014\u2014auto printHello &#061; [] { cout &lt;&lt; &#034;Hello Lambda&#034; &lt;&lt; endl; }\u3002<\/p>\n<p>    &#xff08;\u4e09&#xff09;-&gt; return-type \u8fd4\u56de\u503c\u7c7b\u578b&#xff08;\u53ef\u63a8\u5bfc&#xff0c;\u5e38\u7701\u7565&#xff09;<br \/>\n    \u8fd4\u56de\u503c\u7c7b\u578b\u7528\u4e8e\u6307\u5b9a Lambda \u7684\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;\u91c7\u7528 \u201c\u8ffd\u8e2a\u8fd4\u56de\u7c7b\u578b\u201d \u8bed\u6cd5&#xff0c;\u6838\u5fc3\u89c4\u5219\u548c\u7701\u7565\u6761\u4ef6\u5982\u4e0b&#xff1a;<br \/>\n    \u7701\u7565\u89c4\u5219 1&#xff1a;<br \/>\n    \u5982\u679c Lambda \u6ca1\u6709\u8fd4\u56de\u503c&#xff08;\u51fd\u6570\u4f53\u4e2d\u65e0return\u8bed\u53e5&#xff0c;\u6216return\u8bed\u53e5\u65e0\u8fd4\u56de\u503c&#xff09;&#xff0c;<br \/>\n    -&gt; return-type\u53ef\u76f4\u63a5\u7701\u7565&#xff08;\u9ed8\u8ba4\u8fd4\u56devoid&#xff09;&#xff1b;<\/p>\n<p>    \u7701\u7565\u89c4\u5219 2&#xff1a;<br \/>\n    \u5982\u679c Lambda \u6709\u8fd4\u56de\u503c&#xff0c;\u4e14\u6240\u6709return\u8bed\u53e5\u7684\u8fd4\u56de\u7c7b\u578b\u4e00\u81f4&#xff0c;<br \/>\n    \u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u63a8\u5bfc\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;-&gt; return-type\u4e5f\u53ef\u7701\u7565&#xff1b;<br \/>\n    \u5fc5\u987b\u663e\u5f0f\u6307\u5b9a\u7684\u573a\u666f&#xff1a;<br \/>\n    \u5982\u679c Lambda \u6709\u591a\u4e2areturn\u8bed\u53e5&#xff0c;<br \/>\n    \u4e14\u8fd4\u56de\u7c7b\u578b\u4e0d\u540c&#xff08;\u5982\u4e00\u4e2a\u8fd4\u56deint&#xff0c;\u4e00\u4e2a\u8fd4\u56dedouble&#xff09;&#xff0c;\u5fc5\u987b\u663e\u5f0f\u6307\u5b9a\u8fd4\u56de\u503c\u7c7b\u578b&#xff0c;<br \/>\n    \u5426\u5219\u7f16\u8bd1\u5668\u65e0\u6cd5\u63a8\u5bfc&#xff0c;\u4f1a\u62a5\u9519&#xff1b;<\/p>\n<p>    \u793a\u4f8b&#xff1a;auto calc &#061; [](int x, bool flag) -&gt; double<br \/>\n    { if (flag) return x * 2; else return x &#043; 0.5; }&#xff0c;<br \/>\n     \u56e0\u8fd4\u56de\u503c\u53ef\u80fd\u662fint\u6216double&#xff0c;\u9700\u663e\u5f0f\u6307\u5b9a\u8fd4\u56dedouble\u3002<\/p>\n<p>    &#xff08;\u56db&#xff09;{function-body} \u51fd\u6570\u4f53&#xff08;\u6838\u5fc3\u903b\u8f91\u5b9e\u73b0&#xff09;<br \/>\n    \u51fd\u6570\u4f53\u662f Lambda \u7684\u6838\u5fc3\u903b\u8f91\u6240\u5728&#xff0c;\u548c\u666e\u901a\u51fd\u6570\u7684\u51fd\u6570\u4f53\u5b8c\u5168\u4e00\u81f4&#xff0c;\u6838\u5fc3\u89c4\u5219\u5982\u4e0b&#xff1a;<br \/>\n    \u53ef\u4f7f\u7528\u7684\u53d8\u91cf&#xff1a;\u53c2\u6570\u5217\u8868\u4e2d\u7684\u53c2\u6570 &#043; \u6355\u6349\u5217\u8868\u4e2d\u6355\u6349\u7684\u53d8\u91cf &#043; \u5168\u5c40 \/ \u9759\u6001\u53d8\u91cf&#xff1b;<br \/>\n    \u8bed\u6cd5\u8981\u6c42&#xff1a;\u5373\u4f7f\u51fd\u6570\u4f53\u4e3a\u7a7a&#xff0c;{}\u4e5f\u4e0d\u80fd\u7701\u7565&#xff1b;<br \/>\n    \u793a\u4f8b&#xff1a;auto func &#061; [a, &amp;b]() { cout &lt;&lt; &#034;a\u7684\u526f\u672c&#xff1a;&#034; &lt;&lt; a &lt;&lt; endl; b &#061; 200; }&#xff0c;<br \/>\n    \u51fd\u6570\u4f53\u4e2d\u6253\u5370\u503c\u6355\u6349\u7684a&#xff0c;\u5e76\u4fee\u6539\u5f15\u7528\u6355\u6349\u7684b\u3002<\/p>\n<p>    \u4e09\u3001\u5173\u952e\u4fee\u9970\u7b26&#xff1a;mutable&#xff08;\u89e3\u9664\u503c\u6355\u6349\u7684 const \u9650\u5236&#xff09;<br \/>\n    mutable\u662f Lambda \u7684\u53ef\u9009\u4fee\u9970\u7b26&#xff0c;\u6838\u5fc3\u4f5c\u7528\u662f\u53d6\u6d88\u503c\u6355\u6349\u53d8\u91cf\u7684const\u5c5e\u6027&#xff0c;<br \/>\n    \u5141\u8bb8\u5728\u51fd\u6570\u4f53\u4e2d\u4fee\u6539\u503c\u6355\u6349\u7684\u53d8\u91cf&#xff0c;\u6838\u5fc3\u89c4\u5219\u5982\u4e0b&#xff1a;<br \/>\n    \u4f5c\u7528\u8303\u56f4&#xff1a;\u4ec5\u5bf9 \u201c\u503c\u6355\u6349\u7684\u53d8\u91cf\u201d \u6709\u6548&#xff0c;\u5bf9\u5f15\u7528\u6355\u6349\u7684\u53d8\u91cf\u65e0\u5f71\u54cd&#xff08;\u5f15\u7528\u6355\u6349\u672c\u8eab\u5c31\u53ef\u4fee\u6539\u539f\u53d8\u91cf&#xff09;&#xff1b;<br \/>\n    \u5173\u952e\u6ce8\u610f&#xff1a;\u4fee\u6539\u7684\u662f \u201c\u503c\u6355\u6349\u7684\u526f\u672c\u201d&#xff0c;\u4e0d\u4f1a\u5f71\u54cd\u5916\u5c42\u539f\u53d8\u91cf \u2014\u2014 \u56e0\u4e3a\u503c\u6355\u6349\u672c\u8d28\u662f\u62f7\u8d1d&#xff0c;<br \/>\n    mutable\u53ea\u662f\u5141\u8bb8\u4fee\u6539\u526f\u672c&#xff0c;\u539f\u53d8\u91cf\u4f9d\u7136\u4fdd\u6301\u4e0d\u53d8&#xff1b;<br \/>\n    \u5f3a\u5236\u8981\u6c42&#xff1a;\u4f7f\u7528mutable\u540e&#xff0c;\u53c2\u6570\u5217\u8868()\u4e0d\u53ef\u7701\u7565&#xff0c;\u5373\u4f7f Lambda \u6ca1\u6709\u53c2\u6570&#xff1b;<br \/>\n    \u793a\u4f8b&#xff1a;\u5916\u5c42int num &#061; 5&#xff0c;auto func &#061; [num]() mutable { num &#061; 50; cout &lt;&lt; &#034;\u526f\u672cnum&#xff1a;&#034; &lt;&lt; num &lt;&lt; endl; }&#xff0c;\u8c03\u7528func()\u540e&#xff0c;\u526f\u672cnum\u53d8\u4e3a 50&#xff0c;\u4f46\u5916\u5c42\u539f\u53d8\u91cfnum\u4ecd\u4e3a 5\u3002<br \/>\n    \u56db\u3001Lambda \u7684\u6838\u5fc3\u5e94\u7528\u573a\u666f&#xff08;\u5bf9\u6bd4\u4f20\u7edf\u53ef\u8c03\u7528\u5bf9\u8c61&#xff09;<br \/>\n    \u5728 Lambda \u51fa\u73b0\u4e4b\u524d&#xff0c;C&#043;&#043; \u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u4e3b\u8981\u662f \u201c\u51fd\u6570\u6307\u9488\u201d \u548c \u201c\u4eff\u51fd\u6570\u201d&#xff0c;<br \/>\n    \u4f46\u4e24\u8005\u90fd\u6709\u660e\u663e\u75db\u70b9&#xff1a;\u51fd\u6570\u6307\u9488\u7c7b\u578b\u5b9a\u4e49\u590d\u6742&#xff0c;\u4e14\u65e0\u6cd5\u6355\u6349\u5916\u5c42\u53d8\u91cf&#xff1b;<br \/>\n    \u4eff\u51fd\u6570\u9700\u8981\u81ea\u5b9a\u4e49\u7c7b&#xff0c;\u4ee3\u7801\u5197\u4f59\u3002<br \/>\n    Lambda \u5b8c\u7f8e\u89e3\u51b3\u4e86\u8fd9\u4e9b\u95ee\u9898&#xff0c;\u6210\u4e3a\u8f7b\u91cf\u7ea7\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u9996\u9009&#xff0c;\u5178\u578b\u5e94\u7528\u573a\u666f\u5982\u4e0b&#xff1a;<\/p>\n<p>    &#xff08;\u4e00&#xff09;STL \u7b97\u6cd5\u81ea\u5b9a\u4e49\u903b\u8f91&#xff08;\u5982 sort \u6392\u5e8f&#xff09;<br \/>\n    STL \u7b97\u6cd5&#xff08;\u5982sort\u3001find_if&#xff09;\u5e38\u9700\u8981\u4f20\u5165\u81ea\u5b9a\u4e49\u903b\u8f91&#xff08;\u5982\u6392\u5e8f\u89c4\u5219\u3001\u7b5b\u9009\u6761\u4ef6&#xff09;&#xff0c;Lambda \u76f8\u6bd4\u4eff\u51fd\u6570\u66f4\u7b80\u6d01&#xff0c;\u65e0\u9700\u5b9a\u4e49\u989d\u5916\u7c7b\u3002<br \/>\n    \u793a\u4f8b&#xff1a;\u5bf9vector&lt;int&gt; vec &#061; {3,1,4,2,5}\u6392\u5e8f&#xff0c;\u7528\u4eff\u51fd\u6570\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u5e76\u91cd\u8f7doperator()&#xff0c;\u800c\u7528 Lambda \u53ef\u76f4\u63a5\u5728sort\u53c2\u6570\u4e2d\u5199\u903b\u8f91&#xff1a;sort(vec.begin(), vec.end(), [](int a, int b) { return a &lt; b; })&#xff0c;\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u5347\u5e8f\u6392\u5e8f\u3002<\/p>\n<p>    &#xff08;\u4e8c&#xff09;\u7ebf\u7a0b\u51fd\u6570&#xff08;\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6267\u884c\u903b\u8f91&#xff09;<br \/>\n    \u521b\u5efa\u7ebf\u7a0b\u65f6&#xff0c;Lambda \u53ef\u76f4\u63a5\u6355\u6349\u5916\u5c42\u53d8\u91cf&#xff0c;\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u7684\u6267\u884c\u903b\u8f91&#xff0c;\u65e0\u9700\u5b9a\u4e49\u5168\u5c40\u51fd\u6570\u6216\u7c7b\u6210\u5458\u51fd\u6570\u3002<br \/>\n    \u793a\u4f8b&#xff1a;int threadId &#061; 1; int count &#061; 3;&#xff0c;\u521b\u5efa\u7ebf\u7a0bthread t([threadId, count]() { for (int i&#061;0; i&lt;count; &#043;&#043;i) { cout &lt;&lt; &#034;\u7ebf\u7a0b&#034; &lt;&lt; threadId &lt;&lt; &#034;\u6267\u884c\u7b2c&#034; &lt;&lt; i&#043;1 &lt;&lt; &#034;\u6b21&#034; &lt;&lt; endl; } });&#xff0c;\u7ebf\u7a0b\u51fd\u6570\u76f4\u63a5\u4f7f\u7528\u6355\u6349\u7684threadId\u548ccount&#xff0c;\u903b\u8f91\u6e05\u6670\u4e14\u65e0\u9700\u989d\u5916\u5b9a\u4e49\u3002<\/p>\n<p>    &#xff08;\u4e09&#xff09;\u667a\u80fd\u6307\u9488\u81ea\u5b9a\u4e49\u5220\u9664\u5668<br \/>\n    \u667a\u80fd\u6307\u9488&#xff08;\u5982unique_ptr\u3001shared_ptr&#xff09;\u9ed8\u8ba4\u4f7f\u7528delete\u91ca\u653e\u8d44\u6e90&#xff0c;<br \/>\n    \u4f46\u5bf9\u4e8e\u52a8\u6001\u6570\u7ec4\u3001\u6587\u4ef6\u6307\u9488\u7b49\u7279\u6b8a\u8d44\u6e90&#xff0c;\u9700\u8981\u81ea\u5b9a\u4e49\u5220\u9664\u5668&#xff0c;Lambda \u53ef\u7b80\u6d01\u5b9e\u73b0\u8fd9\u4e00\u903b\u8f91\u3002<br \/>\n    \u793a\u4f8b&#xff1a;\u521b\u5efa\u52a8\u6001\u6570\u7ec4\u7684\u667a\u80fd\u6307\u9488&#xff0c;\u81ea\u5b9a\u4e49\u5220\u9664\u5668\u91ca\u653e\u6570\u7ec4&#xff1a;<br \/>\n    int* arr &#061; new int[5]{1,2,3,4,5};<br \/>\n    unique_ptr&lt;int, function&lt;void(int*)&gt;&gt; up(arr, [](int* p)<br \/>\n    { delete[] p; cout &lt;&lt; &#034;\u52a8\u6001\u6570\u7ec4\u5df2\u91ca\u653e&#034; &lt;&lt; endl; });&#xff1b;<\/p>\n<p>    \u53e6\u4e00\u793a\u4f8b&#xff1a;\u6587\u4ef6\u6307\u9488\u7684\u5220\u9664\u5668&#xff1a;FILE* fp &#061; fopen(&#034;test.txt&#034;, &#034;w&#034;);<br \/>\n    unique_ptr&lt;FILE, function&lt;void(FILE*)&gt;&gt; fpUp(fp, [](FILE* p)<br \/>\n    { fclose(p); cout &lt;&lt; &#034;\u6587\u4ef6\u5df2\u5173\u95ed&#034; &lt;&lt; endl; });\u3002<\/p>\n<p>    \u4e94\u3001Lambda \u7684\u5e95\u5c42\u539f\u7406&#xff08;\u7f16\u8bd1\u5668\u89c6\u89d2&#xff09;<br \/>\n    Lambda \u7684\u5e95\u5c42\u672c\u8d28\u662f\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7684\u533f\u540d\u4eff\u51fd\u6570\u7c7b&#xff0c;\u6211\u4eec\u5199\u7684 Lambda \u4ee3\u7801&#xff0c;\u4f1a\u88ab\u7f16\u8bd1\u5668\u8f6c\u5316\u4e3a\u4e00\u4e2a\u5b8c\u6574\u7684\u4eff\u51fd\u6570\u7c7b&#xff0c;\u4e24\u8005\u5b8c\u5168\u7b49\u4ef7&#xff0c;\u5177\u4f53\u5bf9\u5e94\u5173\u7cfb\u5982\u4e0b&#xff1a;<br \/>\n    1. \u6838\u5fc3\u7b49\u4ef7\u5173\u7cfb<br \/>\n    Lambda \u4ee3\u7801\u7247\u6bb5\u7f16\u8bd1\u5668\u751f\u6210\u7684\u4eff\u51fd\u6570\u7c7b\u903b\u8f91<br \/>\n    auto add &#061; [a](int x) { return a &#043; x; }1. \u751f\u6210\u4e00\u4e2a\u540d\u79f0\u552f\u4e00\u7684\u4eff\u51fd\u6570\u7c7b&#xff08;\u5982__lambda_123&#xff09;&#xff1b;<br \/>\n    2. \u7c7b\u4e2d\u5b9a\u4e49\u79c1\u6709\u6210\u5458\u53d8\u91cfint a&#xff08;\u5bf9\u5e94 Lambda \u7684\u6355\u6349\u5217\u8868\u53d8\u91cf&#xff09;&#xff1b;<br \/>\n    3. \u7c7b\u7684\u6784\u9020\u51fd\u6570\u63a5\u6536int a_&#xff0c;\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cfa(a_)&#xff1b;<br \/>\n    4. \u91cd\u8f7doperator()&#xff1a;int operator()(int x) const { return a &#043; x; }&#xff08;\u5bf9\u5e94 Lambda \u7684\u53c2\u6570\u3001\u8fd4\u56de\u503c\u548c\u51fd\u6570\u4f53&#xff09;&#xff1b;<br \/>\n    5. \u5b9e\u4f8b\u5316\u4eff\u51fd\u6570\u5bf9\u8c61&#xff1a;__lambda_123 add(a);<br \/>\n    2. \u5173\u952e\u7ec6\u8282\u89e3\u8bfb<br \/>\n    \u4eff\u51fd\u6570\u7c7b\u540d\u7531\u7f16\u8bd1\u5668\u6309\u89c4\u5219\u751f\u6210&#xff08;\u5982\u5305\u542b\u884c\u53f7\u3001\u968f\u673a\u6807\u8bc6&#xff09;&#xff0c;\u786e\u4fdd\u4e0d\u540c Lambda \u751f\u6210\u7684\u7c7b\u540d\u4e0d\u51b2\u7a81&#xff1b;<br \/>\n    \u6355\u6349\u5217\u8868\u7684\u53d8\u91cf\u4f1a\u8f6c\u5316\u4e3a\u4eff\u51fd\u6570\u7c7b\u7684\u6210\u5458\u53d8\u91cf&#xff1a;\u503c\u6355\u6349\u5bf9\u5e94\u666e\u901a\u6210\u5458\u53d8\u91cf&#xff08;\u62f7\u8d1d\u521d\u59cb\u5316&#xff09;&#xff0c;<br \/>\n    \u5f15\u7528\u6355\u6349\u5bf9\u5e94\u5f15\u7528\u7c7b\u578b\u6210\u5458\u53d8\u91cf&#xff08;\u5173\u8054\u539f\u53d8\u91cf&#xff09;&#xff1b;<br \/>\n    Lambda \u7684\u53c2\u6570\u3001\u8fd4\u56de\u503c\u3001\u51fd\u6570\u4f53&#xff0c;\u5206\u522b\u5bf9\u5e94\u4eff\u51fd\u6570operator()\u7684\u53c2\u6570\u3001\u8fd4\u56de\u503c\u3001\u51fd\u6570\u4f53&#xff1b;<br \/>\n    Lambda \u503c\u6355\u6349\u9ed8\u8ba4\u53ea\u8bfb\u7684\u539f\u56e0&#xff1a;\u4eff\u51fd\u6570\u7684operator()\u9ed8\u8ba4\u662fconst\u6210\u5458\u51fd\u6570&#xff0c;<br \/>\n    const\u6210\u5458\u51fd\u6570\u65e0\u6cd5\u4fee\u6539\u7c7b\u7684\u666e\u901a\u6210\u5458\u53d8\u91cf&#xff1b;<br \/>\n    \u800cmutable\u4fee\u9970\u7b26\u4f1a\u53bb\u6389operator()\u7684const\u5c5e\u6027&#xff0c;\u5141\u8bb8\u4fee\u6539\u6210\u5458\u53d8\u91cf&#xff08;\u5373\u503c\u6355\u6349\u7684\u526f\u672c&#xff09;\u3002<\/p>\n<p>    \u516d\u3001\u6838\u5fc3\u603b\u7ed3<br \/>\n    \u672c\u8d28&#xff1a;Lambda \u662f\u533f\u540d\u51fd\u6570\u5bf9\u8c61&#xff0c;\u5e95\u5c42\u662f\u7f16\u8bd1\u5668\u751f\u6210\u7684\u4eff\u51fd\u6570\u7c7b&#xff0c;\u53ef\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49&#xff0c;\u652f\u6301\u6355\u6349\u5916\u5c42\u53d8\u91cf&#xff1b;<br \/>\n    \u8bed\u6cd5\u6838\u5fc3&#xff1a;\u6355\u6349\u5217\u8868[]\u662f\u6807\u8bc6&#xff0c;\u53c2\u6570\u5217\u8868\u53ef\u7701\u7565&#xff08;\u65e0\u53c2\u6570\u65f6&#xff09;&#xff0c;\u8fd4\u56de\u503c\u53ef\u63a8\u5bfc&#xff08;\u7c7b\u578b\u4e00\u81f4\u65f6&#xff09;&#xff0c;<br \/>\n    \u51fd\u6570\u4f53{}\u4e0d\u53ef\u7701\u7565&#xff1b;<br \/>\n    \u6355\u6349\u89c4\u5219&#xff1a;\u663e\u5f0f\u6355\u6349\u7cbe\u51c6&#xff0c;\u9690\u5f0f\u6355\u6349\u7b80\u6d01&#xff0c;\u6df7\u5408\u6355\u6349\u9700\u9075\u5faa \u201c\u9ed8\u8ba4\u5728\u524d\u3001\u7c7b\u578b\u5339\u914d\u201d \u89c4\u5219&#xff1b;<br \/>\n    \u503c\u6355\u6349\u53ea\u8bfb&#xff08;\u9700mutable\u89e3\u9664&#xff09;&#xff0c;\u5f15\u7528\u6355\u6349\u53ef\u4fee\u6539\u539f\u53d8\u91cf&#xff1b;<\/p>\n<p>    \u4f18\u52bf&#xff1a;\u76f8\u6bd4\u51fd\u6570\u6307\u9488\u66f4\u7075\u6d3b&#xff08;\u53ef\u6355\u6349\u53d8\u91cf&#xff09;&#xff0c;\u76f8\u6bd4\u4eff\u51fd\u6570\u66f4\u7b80\u6d01&#xff08;\u65e0\u9700\u5b9a\u4e49\u7c7b&#xff09;&#xff1b;<\/p>\n<p>    \u5e94\u7528&#xff1a;STL \u7b97\u6cd5\u3001\u7ebf\u7a0b\u51fd\u6570\u3001\u667a\u80fd\u6307\u9488\u5220\u9664\u5668\u7b49\u573a\u666f&#xff0c;\u662f C&#043;&#043;11 &#043; \u540e\u8f7b\u91cf\u7ea7\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u9996\u9009\u3002<br \/>\n    *\/<\/p>\n<p>    \/\/ OK&#xff0c;\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u5b66\u4e60\u4e00\u4e0bfunction\u5305\u88c5\u5668<br \/>\n    \/\/ \u5176\u5b9e\u5927\u5bb6\u5bf9\u5b83\u80af\u5b9a\u662f\u5b8c\u5168\u4e0d\u964c\u751f&#xff01;&#xff01;&#xff01;<br \/>\n    \/\/ \u56e0\u4e3a\u6211\u4eec\u4e4b\u524d\u5c31\u5728Linux\u90e8\u5206\u4e2d\u7ecf\u5e38\u7528\u5b83\u5305\u88c5\u51fd\u6570&#xff0c;\u7136\u540e\u5916\u754c\u518d\u4f20\u5165\u51fd\u6570<br \/>\n    \/\/ \u6bd4\u5982\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4efb\u52a1\u8c03\u5ea6&#xff0c;\u6211\u4eec\u5c31\u662f\u4f7f\u7528function\u53bb\u5305\u88c5\u51fd\u6570\u7c7b\u578b<br \/>\n    \/\/ \u7136\u540e\u5728\u5916\u754c\u5c31\u53ef\u4ee5\u76f4\u63a5\u628a\u5bf9\u5e94\u7684\u51fd\u6570\u4f20\u5165&#xff0c;\u7136\u540e\u7528function\u5305\u88c5\u540e\u7684\u53d8\u91cf\u53bb\u63a5\u6536<br \/>\n    \/\/ \u90a3\u4e48function\u7684\u4e00\u4e2a\u6700\u6838\u5fc3\u7684\u4f5c\u7528\u5c31\u662f\u5b83\u53ef\u4ee5\u628a\u4efb\u610f\u7684\u51fd\u6570\u554a&#xff0c;lambda\u7b49\u7b49\u90fd\u5305\u88c5\u6210\u4e00\u4e2a\u597d\u7528\u6d45\u663e\u7684\u7c7b\u578b<br \/>\n    \/\/ \u8fd9\u6837\u5728\u5f88\u591a\u5730\u65b9\u5c31\u65b9\u4fbf\u58f0\u660e\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b<br \/>\n    \/\/  template &lt;class T&gt;<br \/>\n    \/\/  class function; \/\/ undefined<br \/>\n    \/\/  template &lt;class Ret, class&#8230; Args&gt;<br \/>\n    \/\/  class function&lt;Ret(Args&#8230;)&gt;;<br \/>\n    \/\/  \u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u3001lambda \u7b49\u5747\u5c5e\u4e8e C&#043;&#043; \u4e2d\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u4f46\u5b83\u4eec\u7684\u5e95\u5c42\u7c7b\u578b\u5b58\u5728\u672c\u8d28\u5dee\u5f02&#xff1a;<br \/>\n    \/\/  \u51fd\u6570\u6307\u9488\u662f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\u7c7b\u578b&#xff08;\u5982int (*)(int, int)&#xff09;&#xff0c;<br \/>\n    \/\/  \u7c7b\u578b\u7531\u51fd\u6570\u7b7e\u540d&#xff08;\u8fd4\u56de\u503c &#043; \u53c2\u6570&#xff09;\u548c\u51fd\u6570\u5730\u5740\u552f\u4e00\u786e\u5b9a&#xff1b;<\/p>\n<p>    \/\/ \u4eff\u51fd\u6570\u662f\u81ea\u5b9a\u4e49\u7c7b\u91cd\u8f7doperator()\u540e\u7684\u7c7b\u7c7b\u578b<br \/>\n    \/\/ &#xff08;\u5982struct Add { int operator()(int, int) { &#8230; } };&#xff09;&#xff0c;\u4e0d\u540c\u4eff\u51fd\u6570\u5bf9\u5e94\u4e0d\u540c\u7684\u7c7b\u7c7b\u578b&#xff1b;<\/p>\n<p>    \/\/ lambda \u8868\u8fbe\u5f0f\u7684\u5e95\u5c42\u662f\u7f16\u8bd1\u5668\u751f\u6210\u7684\u533f\u540d\u95ed\u5305\u7c7b\u578b&#xff08;\u65e0\u663e\u5f0f\u7c7b\u578b\u540d&#xff09;&#xff0c;<br \/>\n    \/\/ \u5373\u4f7f\u7b7e\u540d\u76f8\u540c\u7684\u4e24\u4e2a lambda&#xff0c;\u5176\u7c7b\u578b\u4e5f\u88ab\u7f16\u8bd1\u5668\u89c6\u4e3a\u4e0d\u540c&#xff1b;<\/p>\n<p>    \/\/ \u8fd9\u79cd\u7c7b\u578b\u788e\u7247\u5316\u4f1a\u5bfc\u81f4\u4e25\u91cd\u7684\u5f00\u53d1\u75db\u70b9&#xff1a;<br \/>\n    \/\/ \u65e0\u6cd5\u7528\u7edf\u4e00\u7684\u7c7b\u578b\u58f0\u660e\u3001\u5b58\u50a8\u3001\u4f20\u9012\u8fd9\u4e9b\u53ef\u8c03\u7528\u5bf9\u8c61<br \/>\n    \/\/ &#xff08;\u6bd4\u5982\u5bb9\u5668\u65e0\u6cd5\u540c\u65f6\u5b58\u50a8\u51fd\u6570\u6307\u9488\u548c lambda&#xff0c;\u51fd\u6570\u53c2\u6570\u65e0\u6cd5\u517c\u5bb9\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff09;\u3002<\/p>\n<p>    \/\/ std::function \u662f C&#043;&#043;11 \u5f15\u5165\u7684\u7c7b\u578b\u5b89\u5168\u7684\u901a\u7528\u51fd\u6570\u5305\u88c5\u5668&#xff08;\u4f4d\u4e8e&lt;functional&gt;\u5934\u6587\u4ef6&#xff09;&#xff0c;<br \/>\n    \/\/ \u5176\u6838\u5fc3\u4f18\u52bf\u6b63\u662f\u6253\u7834\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u58c1\u5792&#xff0c;\u7edf\u4e00\u6240\u6709\u7b26\u5408\u76f8\u540c\u7b7e\u540d\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u7c7b\u578b&#xff1a;<\/p>\n<p>    \/\/ \u7c7b\u578b\u7edf\u4e00&#xff1a;\u53ea\u8981\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7b7e\u540d&#xff08;\u8fd4\u56de\u503c\u7c7b\u578b &#043; \u53c2\u6570\u5217\u8868\u7c7b\u578b&#xff09;\u4e00\u81f4&#xff0c;<br \/>\n    \/\/ \u65e0\u8bba\u5176\u5e95\u5c42\u662f\u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u8fd8\u662f lambda&#xff08;\u5305\u62ec\u6709\u6355\u6349\u7684 lambda&#xff09;&#xff0c;<br \/>\n    \/\/ \u90fd\u80fd\u88ab\u540c\u4e00\u7c7b\u578b\u7684std::function\u5305\u88c5&#xff1b;<\/p>\n<p>    \/\/ \u7c7b\u578b\u5b89\u5168&#xff1a;\u76f8\u6bd4\u51fd\u6570\u6307\u9488\u7684\u9690\u5f0f\u8f6c\u6362\u98ce\u9669&#xff0c;<br \/>\n    \/\/ std::function\u5728\u7f16\u8bd1\u671f\u68c0\u67e5\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7b7e\u540d\u662f\u5426\u5339\u914d&#xff0c;\u4e0d\u5339\u914d\u5219\u76f4\u63a5\u62a5\u9519&#xff1b;<\/p>\n<p>    \/\/ \u7075\u6d3b\u5b58\u50a8\u4e0e\u4f20\u9012&#xff1a;\u53ef\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u3001\u8fd4\u56de\u503c\u3001\u5bb9\u5668\u5143\u7d20<br \/>\n    \/\/ &#xff08;\u5982vector&lt;std::function&lt;int(int, int)&gt;&gt;&#xff09;&#xff0c;<br \/>\n    \/\/ \u89e3\u51b3\u4e86\u4e0d\u540c\u7c7b\u578b\u53ef\u8c03\u7528\u5bf9\u8c61\u65e0\u6cd5\u7edf\u4e00\u5b58\u50a8 \/ \u4f20\u9012\u7684\u95ee\u9898&#xff1b;<\/p>\n<p>    \/\/ \u652f\u6301\u7a7a\u72b6\u6001&#xff1a;\u53ef\u9ed8\u8ba4\u6784\u9020&#xff08;\u7a7a\u72b6\u6001&#xff09;&#xff0c;<br \/>\n    \/\/ \u4e5f\u53ef\u901a\u8fc7operator bool()\u5224\u65ad\u662f\u5426\u6301\u6709\u6709\u6548\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u907f\u514d\u7a7a\u6307\u9488\u8c03\u7528\u98ce\u9669&#xff1b;<\/p>\n<p>    \/\/ \u517c\u5bb9\u6240\u6709\u53ef\u8c03\u7528\u5bf9\u8c61&#xff1a;<br \/>\n    \/\/ \u5c24\u5176\u662f\u80fd\u5305\u88c5\u6709\u6355\u6349\u7684 lambda&#xff08;\u51fd\u6570\u6307\u9488\u65e0\u6cd5\u76f4\u63a5\u63a5\u6536\u6709\u6355\u6349\u7684 lambda&#xff09;&#xff0c;\u5f25\u8865\u4e86\u51fd\u6570\u6307\u9488\u7684\u5c40\u9650\u6027\u3002<\/p>\n<p>    \/\/ \u5927\u5bb6\u53ef\u4ee5\u53d1\u73b0&#xff0c;function\u7684\u4f7f\u7528&#xff0c;\u662f\u771f\u7684\u5f88\u723d&#xff01;&#xff01;<br \/>\n    \/\/ \u5b83\u53ef\u4ee5\u628a\u5404\u79cd\u4e0d\u540c\u7684&#xff0c;\u96f6\u788e\u7684\u7c7b\u578b\u90fd\u5305\u88c5\u8d77\u6765<\/p>\n<p>    \/\/ \u4e0b\u9762\u6211\u4eec\u5c31\u6765\u4e00\u4e9b\u7b80\u5355\u7684\u793a\u4f8b<\/p>\n<p>    \/\/ \u4eff\u51fd\u6570<br \/>\n    struct func<br \/>\n    {<br \/>\n        bool operator()(int a, int b)<br \/>\n        {<br \/>\n            return a &gt; b;<br \/>\n        }<br \/>\n    };<\/p>\n<p>    \/\/ \u666e\u901a\u51fd\u6570<br \/>\n    bool func1(int a, int b)<br \/>\n    {<br \/>\n        return a &gt; b;<br \/>\n    }<\/p>\n<p>    int main()<br \/>\n    {<br \/>\n        \/\/ lambda\u8868\u8fbe\u5f0f<br \/>\n        auto la &#061; [](int a, int b)<br \/>\n        { return a &gt; b; };<\/p>\n<p>        \/\/ \u6211\u4eec\u77e5\u9053&#xff0c;\u4e0a\u9762\u7684\u4e09\u4e2a\u7684\u672c\u8d28\u90fd\u662f\u51fd\u6570&#xff0c;\u6211\u4eec\u90fd\u60f3\u628a\u5b83\u4eec\u5f53\u4f5c\u51fd\u6570\u6765\u4f7f\u7528<br \/>\n        \/\/ \u4f46\u662f\u7531\u4e8e\u5b83\u4eec\u7684\u7c7b\u578b\u90fd\u4e0d\u540c&#xff0c;\u6240\u4ee5\u6211\u4eec\u4e5f\u4e0d\u80fd\u628a\u5b83\u4eec\u5b58\u50a8\u8fdb\u540c\u4e00\u4e2a\u5bb9\u5668\u53d8\u91cf\u4e2d<br \/>\n        \/\/ \u4f46\u662f\u6709\u4e86function\u4e4b\u540e&#xff0c;\u8fd9\u4e2a\u5c31\u662f\u5c0f\u95ee\u9898\u4e86<\/p>\n<p>        \/\/ \u4eff\u51fd\u6570<br \/>\n        func f;<br \/>\n        std::function&lt;bool(int, int)&gt; f1 &#061; f;<\/p>\n<p>        std::function&lt;bool(int, int)&gt; f2 &#061; func1;<\/p>\n<p>        std::function&lt;bool(int, int)&gt; f3 &#061; la;<\/p>\n<p>        std::function&lt;bool(int, int)&gt; f4 &#061; [](int a, int b)<br \/>\n        { return a &gt; b; };<\/p>\n<p>        \/\/ \u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5305\u88c5\u5668\u53d8\u91cf\u52a0()\u4ece\u800c\u5b9e\u73b0\u51fd\u6570\u7684\u76f8\u540c\u6548\u679c&#xff01;&#xff01;&#xff01;<br \/>\n        f1(4, 5);<br \/>\n        f2(4, 5);<br \/>\n        f3(4, 5);<br \/>\n        f4(4, 5);<\/p>\n<p>        \/\/ \u4f60\u8fd8\u53ef\u4ee5\u53d1\u73b0&#xff0c;f1\u5df4\u62c9\u5df4\u62c9\u7684\u7c7b\u578b\u90fd\u662f\u4e00\u6837\u7684&#xff01;&#xff01;&#xff01;<\/p>\n<p>        \/\/ \u6240\u4ee5&#xff1a;<br \/>\n        std::vector&lt;std::function&lt;bool(int, int)&gt;&gt; v{f1, f2, f3, f4};<br \/>\n        \/\/ \u79d2\u61c2&#xff01;&#xff01;&#xff01;<\/p>\n<p>        \/\/ \u90a3\u4e48\u9664\u4e86\u50cf\u4e0a\u9762\u8fd9\u4e48\u4f7f\u7528\u4e4b\u5916<br \/>\n        \/\/ \u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9function\u5df4\u62c9\u5df4\u62c9\u8fdb\u884c\u91cd\u547d\u540d\u4e3a\u4e00\u4e2a\u540d\u5b57<br \/>\n        \/\/ \u90a3\u4e48\u4e4b\u540e\u8be5\u540d\u5b57\u7684\u53d8\u91cf\u5c31\u53ef\u4ee5\u63a5\u6536\u4efb\u4f55\u548cfunction\u5df4\u62c9\u5df4\u62c9\u76f8\u540c\u8fd4\u56de\u503c&#xff0c;\u76f8\u540c\u5f62\u53c2\u7c7b\u578b\u53ca\u4e2a\u6570\u7684\u51fd\u6570\u4e86&#xff01;&#xff01;&#xff01;<br \/>\n        \/\/ \u8fd9\u4e2a\u624d\u662f\u6211\u4eec\u6700\u7ecf\u5e38\u4f7f\u7528\u7684&#xff01;&#xff01;&#xff01;<br \/>\n        using func_t &#061; std::function&lt;bool(int, int)&gt;;<\/p>\n<p>        func_t funcc;<br \/>\n        funcc &#061; [](int a, int b)<br \/>\n        { return b &gt; a; };<br \/>\n        \/\/ \u79d2\u61c2&#xff01;&#xff01;&#xff01;<br \/>\n    }<\/p>\n<p>    \/\/ \u90a3\u4e48\u8fd8\u6709\u4e00\u4e2a\u6ce8\u610f\u4e8b\u9879\u5c31\u662f&#xff0c;\u8981\u662f\u6211\u4eec\u8981\u7528function\u5305\u88c5\u5668\u53bb\u5305\u88c5\u7c7b\u5185\u7684\u9759\u6001\u6210\u5458\u51fd\u6570\u6216\u8005\u662f\u6210\u5458\u51fd\u6570\u7684\u65f6\u5019<br \/>\n    \/\/ \u6211\u4eec\u8981\u6307\u660e\u7c7b\u57df&#xff0c;\u5e76\u4e14\u8fd8\u8981\u52a0\u4e0a&amp;\u624d\u884c<br \/>\n    \/\/ \u7136\u540e\u5c31\u662f\u8981\u6ce8\u610f\u666e\u901a\u6210\u5458\u51fd\u6570\u8fd8\u6709\u4e2a\u9690\u85cf\u5f62\u53c2this\u54e6<br \/>\n    class Plus<br \/>\n    {<br \/>\n    public:<br \/>\n        Plus(int n &#061; 10)<br \/>\n            : _n(n)<br \/>\n        {<br \/>\n        }<br \/>\n        static int plusi(int a, int b)<br \/>\n        {<br \/>\n            return a &#043; b;<br \/>\n        }<br \/>\n        double plusd(double a, double b)<br \/>\n        {<br \/>\n            return (a &#043; b) * _n;<br \/>\n        }<\/p>\n<p>    private:<br \/>\n        int _n;<br \/>\n    };<br \/>\n    int main()<br \/>\n    {<\/p>\n<p>        \/\/ \u5305\u88c5\u9759\u6001\u6210\u5458\u51fd\u6570<br \/>\n        \/\/ \u6210\u5458\u51fd\u6570\u8981\u6307\u5b9a\u7c7b\u57df\u5e76\u4e14\u524d\u9762\u52a0&amp;\u624d\u80fd\u83b7\u53d6\u5730\u5740<br \/>\n        function&lt;int(int, int)&gt; f4 &#061; &amp;Plus::plusi;<br \/>\n        cout &lt;&lt; f4(1, 1) &lt;&lt; endl;<\/p>\n<p>        \/\/ \u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570<br \/>\n        \/\/ \u666e\u901a\u6210\u5458\u51fd\u6570\u8fd8\u6709\u4e00\u4e2a\u9690\u542b\u7684this\u6307\u9488\u53c2\u6570&#xff0c;\u6240\u4ee5\u7ed1\u5b9a\u65f6\u4f20\u5bf9\u8c61\u6216\u8005\u5bf9\u8c61\u7684\u6307\u9488\u8fc7\u53bb\u90fd\u53ef\u4ee5<br \/>\n        function&lt;double(Plus *, double, double)&gt; f5 &#061; &amp;Plus::plusd;<br \/>\n        Plus pd;<br \/>\n        cout &lt;&lt; f5(&amp;pd, 1.1, 1.1) &lt;&lt; endl;<\/p>\n<p>        \/\/ \u76f4\u63a5\u4f20\u7c7b\u540d\u5b57\u4e5f\u884c<br \/>\n        function&lt;double(Plus, double, double)&gt; f6 &#061; &amp;Plus::plusd;<br \/>\n        cout &lt;&lt; f6(pd, 1.1, 1.1) &lt;&lt; endl;<br \/>\n        cout &lt;&lt; f6(pd, 1.1, 1.1) &lt;&lt; endl;<\/p>\n<p>        \/\/ \u4f20\u7c7b\u7684\u53f3\u503c\u5f15\u7528\u4e5f\u884c<br \/>\n        function&lt;double(Plus &amp;&amp;, double, double)&gt; f7 &#061; &amp;Plus::plusd;<br \/>\n        cout &lt;&lt; f7(move(pd), 1.1, 1.1) &lt;&lt; endl;<br \/>\n        cout &lt;&lt; f7(Plus(), 1.1, 1.1) &lt;&lt; endl;<\/p>\n<p>        return 0;<br \/>\n    }<\/p>\n<p>    \/\/ OK&#xff0c;\u6700\u540e\u6211\u4eec\u6765\u8bb2\u8bb2bind\u7684\u4f7f\u7528&#xff01;&#xff01;&#xff01;<br \/>\n    \/\/  simple(1)<br \/>\n    \/\/  template &lt;class Fn, class&#8230; Args&gt;<br \/>\n    \/\/  \/* unspecified *\/ bind (Fn&amp;&amp; fn, Args&amp;&amp;&#8230; args);<br \/>\n    \/\/  with return type (2)<br \/>\n    \/\/  template &lt;class Ret, class Fn, class&#8230; Args&gt;<br \/>\n    \/\/  \/* unspecified *\/ bind (Fn&amp;&amp; fn, Args&amp;&amp;&#8230; args);<br \/>\n    \/\/ \u4f46\u662f\u8bf4\u5b9e\u8bdd&#xff0c;bind\u7684\u4f7f\u7528\u573a\u666f\u6bd4\u8f83\u5c11<br \/>\n    \/\/ \u5b83\u7684\u6700\u4e3b\u8981\u529f\u80fd\u5c31\u662f\u4f7f\u7528bind\u53bb\u7ed1\u5b9a\u4e00\u4e9b\u56fa\u5b9a\u53c2\u6570<br \/>\n    \/*<br \/>\n    \u4e00\u3001std::bind \u7684\u6838\u5fc3\u5b9a\u4f4d&#xff1a;\u201c\u51fd\u6570\u9002\u914d\u5668\u201d<br \/>\n    std::bind \u662f C&#043;&#043;11 \u5f15\u5165\u7684\u51fd\u6570\u6a21\u677f&#xff08;\u4f4d\u4e8e&lt;functional&gt;\u5934\u6587\u4ef6&#xff09;&#xff0c;<br \/>\n    \u672c\u8d28\u662f\u53ef\u8c03\u7528\u5bf9\u8c61\u5305\u88c5\u5668&#xff0c;\u66f4\u5f62\u8c61\u7684\u79f0\u547c\u662f \u201c\u51fd\u6570\u9002\u914d\u5668\u201d\u2014\u2014<br \/>\n    \u5c31\u50cf\u751f\u6d3b\u4e2d\u9002\u914d\u4e0d\u540c\u63d2\u5934\u7684\u7535\u6e90\u8f6c\u6362\u5668&#xff0c;<br \/>\n    \u5b83\u80fd\u5bf9\u73b0\u6709\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff08;\u666e\u901a\u51fd\u6570\u3001\u4eff\u51fd\u6570\u3001lambda\u3001\u51fd\u6570\u6307\u9488\u3001\u7c7b\u6210\u5458\u51fd\u6570\u7b49&#xff09;<br \/>\n    \u505a \u201c\u53c2\u6570\u5c42\u9762\u7684\u6539\u9020\u201d&#xff0c;\u65e0\u9700\u91cd\u5199\u539f\u5bf9\u8c61&#xff0c;\u5c31\u80fd\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff1b;<br \/>\n    \u8c03\u7528\u8fd9\u4e2a\u65b0\u5bf9\u8c61\u65f6&#xff0c;\u4f1a\u81ea\u52a8\u8c03\u7528\u539f\u5bf9\u8c61&#xff0c;\u5e76\u6309\u6211\u4eec\u9884\u8bbe\u7684\u89c4\u5219\u4f20\u9012\u53c2\u6570\u3002<\/p>\n<p>    \u4e8c\u3001std::bind \u7684\u8c03\u7528\u5f62\u5f0f&#xff08;\u9010\u5b57\u62c6\u89e3&#xff09;<br \/>\n    \u8c03\u7528 std::bind \u7684\u901a\u7528\u683c\u5f0f\u4e3a&#xff1a;auto newCallable &#061; bind(callable, arg_list);&#xff0c;<br \/>\n    \u5404\u90e8\u5206\u7684\u901a\u4fd7\u542b\u4e49\u5982\u4e0b&#xff1a;<br \/>\n    callable&#xff1a;\u5f85\u9002\u914d\u7684\u539f\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u6bd4\u5982\u4e00\u4e2a\u9700\u8981\u8c03\u6574\u53c2\u6570\u7684\u51fd\u6570\u3001lambda \u8868\u8fbe\u5f0f\u7b49&#xff1b;<br \/>\n    arg_list&#xff1a;\u9017\u53f7\u5206\u9694\u7684\u53c2\u6570\u5217\u8868&#xff0c;\u5bf9\u5e94\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570&#xff0c;\u5305\u542b\u4e24\u79cd\u7c7b\u578b&#xff1a;<\/p>\n<p>    \u666e\u901a\u503c&#xff08;\u5982\u6570\u5b57 10\u3001\u53d8\u91cf x\u3001\u5b57\u7b26\u4e32 &#034;test&#034;&#xff09;&#xff1a;<br \/>\n    \u8868\u793a \u201c\u56fa\u5b9a\u4f20\u7ed9\u539f\u5bf9\u8c61\u7684\u53c2\u6570\u201d&#xff0c;\u8c03\u7528\u65b0\u5bf9\u8c61\u65f6\u65e0\u9700\u518d\u4f20\u8fd9\u90e8\u5206\u53c2\u6570&#xff1b;<\/p>\n<p>    \u5360\u4f4d\u7b26&#xff08;\u5982_1\u3001_2\u3001_3&#xff09;&#xff1a;\u8868\u793a \u201c\u65b0\u5bf9\u8c61\u7684\u53c2\u6570\u4f4d\u7f6e\u6807\u8bb0\u201d&#xff0c;<br \/>\n    \u8c03\u7528\u65b0\u5bf9\u8c61\u65f6\u4f20\u5165\u7684\u53c2\u6570\u4f1a\u6309\u5360\u4f4d\u7b26\u7684\u6807\u8bb0&#xff0c;\u586b\u5145\u5230\u539f\u5bf9\u8c61\u7684\u5bf9\u5e94\u4f4d\u7f6e&#xff1b;<\/p>\n<p>    newCallable&#xff1a;bind \u8fd4\u56de\u7684\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;<br \/>\n    \u8c03\u7528\u5b83\u5c31\u7b49\u540c\u4e8e\u8c03\u7528\u539f\u5bf9\u8c61&#xff0c;\u4f46\u53c2\u6570\u5df2\u6309 arg_list \u7684\u89c4\u5219\u8c03\u6574\u5b8c\u6bd5\u3002<\/p>\n<p>    \u4e09\u3001\u5360\u4f4d\u7b26&#xff08;_n&#xff09;&#xff1a;bind \u7684\u6838\u5fc3\u5173\u952e<br \/>\n    \u5360\u4f4d\u7b26\u662f\u5b9e\u73b0\u53c2\u6570\u8c03\u6574\u7684\u6838\u5fc3&#xff0c;\u9700\u660e\u786e 3 \u4e2a\u57fa\u7840\u89c4\u5219&#xff1a;<\/p>\n<p>    \u5f62\u5f0f\u4e0e\u542b\u4e49&#xff1a;\u5360\u4f4d\u7b26\u4ee5_n\u8868\u793a&#xff08;n \u662f\u6b63\u6574\u6570&#xff09;&#xff0c;<br \/>\n    _1\u4ee3\u8868 \u201c\u8c03\u7528\u65b0\u5bf9\u8c61\u65f6\u4f20\u5165\u7684\u7b2c 1 \u4e2a\u53c2\u6570\u201d&#xff0c;<br \/>\n    _2\u4ee3\u8868 \u201c\u8c03\u7528\u65b0\u5bf9\u8c61\u65f6\u4f20\u5165\u7684\u7b2c 2 \u4e2a\u53c2\u6570\u201d&#xff0c;\u4ee5\u6b64\u7c7b\u63a8&#xff1b;<\/p>\n<p>    \u547d\u540d\u7a7a\u95f4&#xff1a;\u5360\u4f4d\u7b26\u5b9a\u4e49\u5728std::placeholders\u547d\u540d\u7a7a\u95f4\u4e2d&#xff0c;<br \/>\n    \u4f7f\u7528\u65f6\u9700\u663e\u5f0f\u5f15\u7528&#xff08;\u5982std::placeholders::_1&#xff09;<br \/>\n    \u6216\u63d0\u524d\u58f0\u660eusing namespace std::placeholders;&#xff1b;<\/p>\n<p>    \u4f5c\u7528&#xff1a;\u5360\u4f4d\u7b26\u662f \u201c\u53c2\u6570\u4f4d\u7f6e\u7684\u6807\u8bb0\u201d\u2014\u2014<br \/>\n    \u6807\u8bb0\u65b0\u5bf9\u8c61\u7684\u53c2\u6570\u8981\u4f20\u9012\u7ed9\u539f\u5bf9\u8c61\u7684\u54ea\u4e2a\u53c2\u6570\u4f4d\u7f6e&#xff0c;<br \/>\n    \u6bd4\u5982\u539f\u5bf9\u8c61\u9700\u8981 3 \u4e2a\u53c2\u6570&#xff0c;bind \u65f6\u5199bind(\u539f\u5bf9\u8c61, _2, 10, _1)&#xff0c;<br \/>\n    \u8868\u793a\u539f\u5bf9\u8c61\u7684\u7b2c 1 \u4e2a\u53c2\u6570\u63a5\u6536\u65b0\u5bf9\u8c61\u7684\u7b2c 2 \u4e2a\u53c2\u6570&#xff0c;<br \/>\n    \u7b2c 2 \u4e2a\u53c2\u6570\u56fa\u5b9a\u4e3a 10&#xff0c;\u7b2c 3 \u4e2a\u53c2\u6570\u63a5\u6536\u65b0\u5bf9\u8c61\u7684\u7b2c 1 \u4e2a\u53c2\u6570\u3002<\/p>\n<p>    \u56db\u3001std::bind \u7684\u6838\u5fc3\u529f\u80fd&#xff08;\u53c2\u6570\u8c03\u6574&#xff09;<br \/>\n    1. \u56fa\u5b9a\u53c2\u6570&#xff08;\u51cf\u5c11\u53c2\u6570\u4e2a\u6570&#xff09;<br \/>\n    \u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u9700\u8981\u591a\u4e2a\u53c2\u6570&#xff0c;<br \/>\n    \u901a\u8fc7 bind \u56fa\u5b9a\u5176\u4e2d\u90e8\u5206\u53c2\u6570\u4e3a\u5177\u4f53\u503c&#xff0c;\u65b0\u5bf9\u8c61\u53ea\u9700\u4f20\u9012\u5269\u4e0b\u7684\u53c2\u6570&#xff0c;\u5b9e\u73b0\u53c2\u6570\u4e2a\u6570\u7684\u51cf\u5c11\u3002<br \/>\n    \u4f8b\u5982&#xff1a;\u539f\u51fd\u6570\u662f \u201c\u8ba1\u7b97 a&#043;b&#043;c\u201d&#xff08;\u9700 3 \u4e2a\u53c2\u6570&#xff09;&#xff0c;<br \/>\n    \u901a\u8fc7 bind \u56fa\u5b9a a&#061;10&#xff0c;\u65b0\u5bf9\u8c61\u53ea\u9700\u4f20 b \u548c c \u4e24\u4e2a\u53c2\u6570 \u2014\u2014<br \/>\n    \u8c03\u7528\u65b0\u5bf9\u8c61\u65f6&#xff0c;\u4f1a\u81ea\u52a8\u628a\u56fa\u5b9a\u7684 10 \u548c\u4f20\u5165\u7684 b\u3001c \u4e00\u8d77\u4f20\u7ed9\u539f\u51fd\u6570&#xff0c;\u65e0\u9700\u624b\u52a8\u4f20 a\u3002<\/p>\n<p>    2. \u8c03\u6574\u53c2\u6570\u987a\u5e8f<br \/>\n    \u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570\u6709\u56fa\u5b9a\u987a\u5e8f&#xff0c;<br \/>\n    \u901a\u8fc7 bind \u8c03\u6574\u5360\u4f4d\u7b26\u7684\u4f4d\u7f6e&#xff0c;\u53ef\u8c03\u6362\u4f20\u9012\u7ed9\u539f\u5bf9\u8c61\u7684\u53c2\u6570\u987a\u5e8f&#xff0c;\u53c2\u6570\u4e2a\u6570\u4e0d\u53d8\u4f46\u987a\u5e8f\u6539\u53d8\u3002<br \/>\n    \u4f8b\u5982&#xff1a;\u539f\u51fd\u6570\u662f \u201c\u6253\u5370 a \u548c b&#xff08;a \u5728\u524d\u3001b \u5728\u540e&#xff09;\u201d<br \/>\n    &#xff0c;bind \u65f6\u5199bind(\u539f\u51fd\u6570, _2, _1)&#xff0c;<br \/>\n    \u8868\u793a\u539f\u51fd\u6570\u7684\u7b2c 1 \u4e2a\u53c2\u6570&#xff08;a&#xff09;\u63a5\u6536\u65b0\u5bf9\u8c61\u7684\u7b2c 2 \u4e2a\u53c2\u6570&#xff0c;<br \/>\n    \u539f\u51fd\u6570\u7684\u7b2c 2 \u4e2a\u53c2\u6570&#xff08;b&#xff09;\u63a5\u6536\u65b0\u5bf9\u8c61\u7684\u7b2c 1 \u4e2a\u53c2\u6570&#xff1b;<br \/>\n    \u8c03\u7528\u65b0\u5bf9\u8c61\u4f20\u5165 (10,20)&#xff0c;\u539f\u51fd\u6570\u5b9e\u9645\u62ff\u5230\u7684\u53c2\u6570\u662f (20,10)&#xff0c;\u5b9e\u73b0\u53c2\u6570\u987a\u5e8f\u8c03\u6362\u3002<\/p>\n<p>    3. \u6df7\u5408\u8c03\u6574&#xff08;\u56fa\u5b9a\u53c2\u6570 &#043; \u8c03\u6574\u987a\u5e8f&#xff09;<br \/>\n    \u540c\u65f6\u5b9e\u73b0\u53c2\u6570\u56fa\u5b9a\u548c\u987a\u5e8f\u8c03\u6574&#xff0c;\u662f\u6700\u5e38\u7528\u7684\u590d\u6742\u573a\u666f\u3002<br \/>\n    \u4f8b\u5982&#xff1a;\u539f\u51fd\u6570\u662f \u201c\u8ba1\u7b97 a-b&#043;c-d\u201d&#xff08;\u9700 4 \u4e2a\u53c2\u6570&#xff09;&#xff0c;<br \/>\n    bind \u65f6\u56fa\u5b9a a&#061;100\u3001d&#061;20&#xff0c;\u540c\u65f6\u8c03\u6574 b \u548c c \u7684\u987a\u5e8f<br \/>\n    &#xff08;\u539f b \u7528\u65b0\u5bf9\u8c61\u7684\u7b2c 2 \u4e2a\u53c2\u6570&#xff0c;\u539f c \u7528\u65b0\u5bf9\u8c61\u7684\u7b2c 1 \u4e2a\u53c2\u6570&#xff09;&#xff1b;<br \/>\n    \u8c03\u7528\u65b0\u5bf9\u8c61\u4f20\u5165 (30,40)&#xff0c;\u539f\u51fd\u6570\u5b9e\u9645\u62ff\u5230\u7684\u53c2\u6570\u662f (100,40,30,20)&#xff0c;<br \/>\n    \u65e2\u56fa\u5b9a\u4e86\u90e8\u5206\u53c2\u6570&#xff0c;\u53c8\u8c03\u6362\u4e86\u53c2\u6570\u987a\u5e8f\u3002<\/p>\n<p>    \u4e94\u3001std::bind \u4e0e std::function \u7684\u7ed3\u5408<br \/>\n    bind \u8fd4\u56de\u7684\u65b0\u53ef\u8c03\u7528\u5bf9\u8c61\u662f\u7f16\u8bd1\u5668\u751f\u6210\u7684\u533f\u540d\u7c7b\u578b&#xff0c;<br \/>\n    \u65e0\u6cd5\u76f4\u63a5\u58f0\u660e&#xff08;\u53ea\u80fd\u7528 auto \u63a5\u6536&#xff09;&#xff0c;<br \/>\n    \u4f46\u53ef\u901a\u8fc7 std::function \u5305\u88c5&#xff0c;\u5b9e\u73b0\u7c7b\u578b\u7edf\u4e00 \u2014\u2014<br \/>\n    \u65e0\u8bba\u539f\u5bf9\u8c61\u662f\u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u8fd8\u662f lambda&#xff0c;\u53ea\u8981\u7b7e\u540d\u4e00\u81f4&#xff08;\u8fd4\u56de\u503c&#xff0c;\u53c2\u6570\u7c7b\u578b\u53ca\u4e2a\u6570\u90fd\u76f8\u540c&#xff09;&#xff0c;<br \/>\n    bind \u540e\u7684\u65b0\u5bf9\u8c61\u90fd\u80fd\u88ab\u540c\u4e00\u7c7b\u578b\u7684 std::function \u5b58\u50a8\u3001\u4f20\u9012<br \/>\n    &#xff08;\u6bd4\u5982\u5b58\u5165\u540c\u4e00\u5bb9\u5668&#xff0c;\u6216\u4f5c\u4e3a\u7edf\u4e00\u7684\u51fd\u6570\u53c2\u6570&#xff09;&#xff0c;\u5f25\u8865\u4e86 bind \u8fd4\u56de\u7c7b\u578b\u533f\u540d\u7684\u4e0d\u8db3\u3002<\/p>\n<p>    \u516d\u3001\u4f7f\u7528 std::bind \u7684\u6ce8\u610f\u4e8b\u9879&#xff08;\u907f\u5751\u70b9&#xff09;<br \/>\n    \u5360\u4f4d\u7b26\u4e0d\u80fd\u8d85\u754c&#xff1a;\u5982\u679c\u65b0\u5bf9\u8c61\u53ea\u63a5\u6536 2 \u4e2a\u53c2\u6570&#xff0c;<br \/>\n    \u4f7f\u7528_3\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u62a5\u9519&#xff0c;\u5360\u4f4d\u7b26\u7684\u6570\u503c\u4e0d\u80fd\u8d85\u8fc7\u65b0\u5bf9\u8c61\u7684\u53c2\u6570\u4e2a\u6570&#xff1b;<\/p>\n<p>    \u7ed1\u5b9a\u7c7b\u6210\u5458\u51fd\u6570&#xff1a;\u9700\u989d\u5916\u4f20\u9012\u7c7b\u7684\u5b9e\u4f8b&#xff08;\u6216\u5b9e\u4f8b\u6307\u9488&#xff09;&#xff0c;<br \/>\n    \u6bd4\u5982\u7ed1\u5b9a\u7c7b\u7684\u6210\u5458\u51fd\u6570\u65f6&#xff0c;\u8981\u628a\u5bf9\u8c61\u6307\u9488\u4f5c\u4e3a arg_list \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570&#xff0c;\u5426\u5219\u65e0\u6cd5\u8c03\u7528&#xff1b;<\/p>\n<p>    \u7ed1\u5b9a\u5f15\u7528\u53c2\u6570&#xff1a;\u5982\u679c\u60f3\u7ed9\u539f\u5bf9\u8c61\u4f20\u9012\u5f15\u7528&#xff08;\u800c\u975e\u62f7\u8d1d&#xff09;&#xff0c;<br \/>\n    \u4e0d\u80fd\u76f4\u63a5\u4f20\u53d8\u91cf&#xff08;bind \u4f1a\u9ed8\u8ba4\u62f7\u8d1d\u53d8\u91cf&#xff09;&#xff0c;<br \/>\n    \u9700\u7528std::ref()&#xff08;\u975e const \u5f15\u7528&#xff09;\u6216std::cref()&#xff08;const \u5f15\u7528&#xff09;\u5305\u88c5\u53d8\u91cf&#xff1b;<\/p>\n<p>    \u907f\u514d\u7a7a\u7ed1\u5b9a&#xff1a;\u5982\u679c\u7ed1\u5b9a\u7684\u539f\u53ef\u8c03\u7528\u5bf9\u8c61\u5931\u6548&#xff08;\u6bd4\u5982\u7ed1\u5b9a\u4e86\u5c40\u90e8\u51fd\u6570\u7684\u6307\u9488\u3001\u5df2\u9500\u6bc1\u5bf9\u8c61\u7684\u6210\u5458\u51fd\u6570&#xff09;&#xff0c;<br \/>\n    \u8c03\u7528\u65b0\u5bf9\u8c61\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u51fa\u73b0\u672a\u5b9a\u4e49\u884c\u4e3a&#xff1b;<\/p>\n<p>    \u4e0e lambda \u7684\u5bf9\u6bd4&#xff1a;\u7b80\u5355\u7684\u53c2\u6570\u8c03\u6574&#xff08;\u5982\u56fa\u5b9a 1-2 \u4e2a\u53c2\u6570&#xff09;\u7528 lambda \u66f4\u7b80\u6d01\u76f4\u89c2&#xff1b;<br \/>\n    \u590d\u6742\u7684\u53c2\u6570\u9002\u914d&#xff08;\u5982\u52a8\u6001\u8c03\u6574\u53c2\u6570\u987a\u5e8f\u3001\u6279\u91cf\u56fa\u5b9a\u53c2\u6570&#xff09;\u7528 bind \u66f4\u7075\u6d3b\u3002<\/p>\n<p>    \u4e03\u3001\u6838\u5fc3\u603b\u7ed3<br \/>\n    std::bind \u7684\u672c\u8d28\u662f \u201c\u51fd\u6570\u9002\u914d\u5668\u201d&#xff0c;<br \/>\n    \u6838\u5fc3\u4f5c\u7528\u662f\u8c03\u6574\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u53c2\u6570&#xff08;\u56fa\u5b9a\u53c2\u6570\u51cf\u5c11\u4e2a\u6570\u3001\u8c03\u6574\u5360\u4f4d\u7b26\u6539\u53d8\u987a\u5e8f&#xff09;&#xff1b;<br \/>\n    \u5360\u4f4d\u7b26_n\u662f\u5173\u952e&#xff0c;\u6807\u8bb0\u65b0\u5bf9\u8c61\u7684\u53c2\u6570\u8981\u586b\u5145\u5230\u539f\u5bf9\u8c61\u7684\u54ea\u4e2a\u4f4d\u7f6e&#xff1b;<br \/>\n    bind \u8fd4\u56de\u7684\u533f\u540d\u53ef\u8c03\u7528\u5bf9\u8c61\u53ef\u901a\u8fc7 std::function \u5305\u88c5&#xff0c;\u5b9e\u73b0\u7c7b\u578b\u7edf\u4e00&#xff1b;<br \/>\n    \u7b80\u5355\u573a\u666f\u4f18\u5148\u7528 lambda&#xff0c;\u590d\u6742\u53c2\u6570\u9002\u914d\u573a\u666f\u7528 bind \u66f4\u9ad8\u6548\u3002<br \/>\n    *\/<\/p>\n<p>    using placeholders::_1;<br \/>\n    using placeholders::_2;<br \/>\n    using placeholders::_3;<\/p>\n<p>    int Sub(int a, int b)<br \/>\n    {<br \/>\n        return (a &#8211; b) * 10;<br \/>\n    }<\/p>\n<p>    int SubX(int a, int b, int c)<br \/>\n    {<br \/>\n        return (a &#8211; b &#8211; c) * 10;<br \/>\n    }<\/p>\n<p>    class Plus<br \/>\n    {<br \/>\n    public:<br \/>\n        static int plusi(int a, int b)<br \/>\n        {<br \/>\n            return a &#043; b;<br \/>\n        }<br \/>\n        double plusd(double a, double b)<br \/>\n        {<br \/>\n            return a &#043; b;<br \/>\n        }<br \/>\n    };<\/p>\n<p>    int main()<br \/>\n    {<br \/>\n        auto sub1 &#061; bind(Sub, _1, _2);<br \/>\n        cout &lt;&lt; sub1(10, 5) &lt;&lt; endl;<br \/>\n        \/\/ bind \u672c\u8d28\u8fd4\u56de\u7684\u4e00\u4e2a\u4eff\u51fd\u6570\u5bf9\u8c61<br \/>\n        \/\/ \u8c03\u6574\u53c2\u6570\u987a\u5e8f&#xff08;\u4e0d\u5e38\u7528&#xff09;<br \/>\n        \/\/ _1\u4ee3\u8868\u7b2c\u4e00\u4e2a\u5b9e\u53c2<br \/>\n        \/\/ _2\u4ee3\u8868\u7b2c\u4e8c\u4e2a\u5b9e\u53c2<br \/>\n        \/\/ &#8230;<br \/>\n        auto sub2 &#061; bind(Sub, _2, _1);<br \/>\n        cout &lt;&lt; sub2(10, 5) &lt;&lt; endl;\/\/\u628a\u65b0\u5bf9\u8c61\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u7ed9\u539f\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u2026\u2026<\/p>\n<p>        \/\/ \u8c03\u6574\u53c2\u6570\u4e2a\u6570 &#xff08;\u5e38\u7528&#xff09;<br \/>\n        auto sub3 &#061; bind(Sub, 100, _1);<br \/>\n        cout &lt;&lt; sub3(5) &lt;&lt; endl;<br \/>\n        auto sub4 &#061; bind(Sub, _1, 100);<br \/>\n        cout &lt;&lt; sub4(5) &lt;&lt; endl;<\/p>\n<p>        \/\/ \u5206\u522b\u7ed1\u6b7b\u7b2c123\u4e2a\u53c2\u6570<br \/>\n        auto sub5 &#061; bind(SubX, 100, _1, _2);<br \/>\n        cout &lt;&lt; sub5(5, 1) &lt;&lt; endl;<br \/>\n        auto sub6 &#061; bind(SubX, _1, 100, _2);<br \/>\n        cout &lt;&lt; sub6(5, 1) &lt;&lt; endl;<br \/>\n        auto sub7 &#061; bind(SubX, _1, _2, 100);<br \/>\n        cout &lt;&lt; sub7(5, 1) &lt;&lt; endl;<\/p>\n<p>        \/\/ \u6210\u5458\u51fd\u6570\u5bf9\u8c61\u8fdb\u884c\u7ed1\u6b7b&#xff0c;\u5c31\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u4f20\u9012\u4e86<br \/>\n        function&lt;double(Plus &amp;&amp;, double, double)&gt; f6 &#061; &amp;Plus::plusd;<br \/>\n        Plus pd;<br \/>\n        cout &lt;&lt; f6(move(pd), 1.1, 1.1) &lt;&lt; endl;<br \/>\n        cout &lt;&lt; f6(Plus(), 1.1, 1.1) &lt;&lt; endl;<\/p>\n<p>        \/\/ bind\u4e00\u822c\u7528\u4e8e&#xff0c;\u7ed1\u6b7b\u4e00\u4e9b\u56fa\u5b9a\u53c2\u6570<br \/>\n        function&lt;double(double, double)&gt; f7 &#061; bind(&amp;Plus::plusd, Plus(), _1, _2);<br \/>\n        cout &lt;&lt; f7(1.1, 1.1) &lt;&lt; endl;<\/p>\n<p>        \/\/ \u8ba1\u7b97\u590d\u5229\u7684lambda<br \/>\n        auto func1 &#061; [](double rate, double money, int year) -&gt; double<br \/>\n        {<br \/>\n            double ret &#061; money;<br \/>\n            for (int i &#061; 0; i &lt; year; i&#043;&#043;)<br \/>\n            {<br \/>\n                ret &#043;&#061; ret * rate;<br \/>\n            }<br \/>\n            return ret &#8211; money;<br \/>\n        };<\/p>\n<p>        \/\/ \u7ed1\u6b7b\u4e00\u4e9b\u53c2\u6570&#xff0c;\u5b9e\u73b0\u51fa\u652f\u6301\u4e0d\u540c\u5e74\u534e\u5229\u7387&#xff0c;\u4e0d\u540c\u91d1\u989d\u548c\u4e0d\u540c\u5e74\u4efd\u8ba1\u7b97\u51fa\u590d\u5229\u7684\u7ed3\u7b97\u5229\u606f<br \/>\n        function&lt;double(double)&gt; func3_1_5 &#061; bind(func1, 0.015, _1, 3);<\/p>\n<p>        function&lt;double(double)&gt; func5_1_5 &#061; bind(func1, 0.015, _1, 5);<\/p>\n<p>        function&lt;double(double)&gt; func10_2_5 &#061; bind(func1, 0.025, _1, 10);<\/p>\n<p>        function&lt;double(double)&gt; func20_3_5 &#061; bind(func1, 0.035, _1, 30);<\/p>\n<p>        cout &lt;&lt; func3_1_5(1000000) &lt;&lt; endl;<\/p>\n<p>        cout &lt;&lt; func5_1_5(1000000) &lt;&lt; endl;<\/p>\n<p>        cout &lt;&lt; func10_2_5(1000000) &lt;&lt; endl;<\/p>\n<p>        cout &lt;&lt; func20_3_5(1000000) &lt;&lt; endl;<br \/>\n        return 0;<br \/>\n    }<br \/>\n    \/\/bind\u7684\u4f7f\u7528\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\u7684&#xff0c;\u5927\u5bb6\u77e5\u9053\u5c31\u884c<br \/>\n}<\/p>\n<h2>\u7ed3\u8bed&#xff1a;\u4ee5\u4f18\u96c5\u5de5\u5177\u4e3a\u7ffc&#xff0c;\u89e3\u9501\u73b0\u4ee3 C&#043;&#043; \u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u4e4b\u7f8e<\/h2>\n<p>\u4eb2\u7231\u7684\u8bfb\u8005&#xff0c;\u5f53\u4f60\u8bfb\u5b8c\u8fd9\u7bc7\u535a\u5ba2\u65f6&#xff0c;\u60f3\u5fc5\u5df2\u7ecf\u5bf9 C&#043;&#043;11 \u4e2d Lambda \u8868\u8fbe\u5f0f\u3001std::function \u5305\u88c5\u5668\u4e0e std::bind \u9002\u914d\u5668\u8fd9\u4e09\u5927\u6838\u5fc3\u5de5\u5177&#xff0c;\u5efa\u7acb\u8d77\u4e86\u7cfb\u7edf\u800c\u6df1\u5165\u7684\u8ba4\u77e5\u3002\u8fd9\u8d9f\u5b66\u4e60\u4e4b\u65c5\u6216\u8bb8\u66fe\u8ba9\u4f60\u5728 Lambda \u7684\u6355\u6349\u5217\u8868\u4e2d\u5f98\u5f8a&#xff0c;\u5728 std::function \u7684\u7c7b\u578b\u5339\u914d\u4e2d\u56f0\u60d1&#xff0c;\u5728 std::bind \u7684\u5360\u4f4d\u7b26\u89c4\u5219\u4e2d\u7ea0\u7ed3&#xff0c;\u4f46\u8bf7\u76f8\u4fe1&#xff0c;\u6bcf\u4e00\u6b21\u5bf9\u7ec6\u8282\u7684\u6253\u78e8\u3001\u6bcf\u4e00\u6b21\u5bf9\u539f\u7406\u7684\u8ffd\u95ee&#xff0c;\u90fd\u5728\u4e3a\u4f60\u7684 C&#043;&#043; \u529f\u5e95\u6ce8\u5165\u65b0\u7684\u529b\u91cf \u2014\u2014 \u8fd9\u4e9b\u770b\u4f3c\u590d\u6742\u7684\u7279\u6027&#xff0c;\u672c\u8d28\u4e0a\u662f\u73b0\u4ee3 C&#043;&#043; \u4e3a\u5f00\u53d1\u8005\u91cf\u8eab\u6253\u9020\u7684 \u201c\u4f18\u96c5\u5229\u5668\u201d&#xff0c;\u65e8\u5728\u7528\u7b80\u6d01\u7684\u8bed\u6cd5\u89e3\u51b3\u4f20\u7edf\u7f16\u7a0b\u4e2d\u7684\u7e41\u7410\u96be\u9898\u3002<\/p>\n<p>\u56de\u987e C&#043;&#043;11 \u4e4b\u524d\u7684\u7f16\u7a0b\u573a\u666f&#xff0c;\u6211\u4eec\u4e3a\u4e86\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u89c4\u5219&#xff0c;\u4e0d\u5f97\u4e0d\u7f16\u5199\u5b8c\u6574\u7684\u4eff\u51fd\u6570\u7c7b&#xff1b;\u4e3a\u4e86\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u4e0d\u5f97\u4e0d\u8bbe\u8ba1\u590d\u6742\u7684\u6a21\u677f\u7279\u5316&#xff1b;\u4e3a\u4e86\u8c03\u6574\u51fd\u6570\u53c2\u6570\u987a\u5e8f&#xff0c;\u4e0d\u5f97\u4e0d\u91cd\u5199\u5927\u91cf\u91cd\u8f7d\u51fd\u6570\u3002\u8fd9\u4e9b\u91cd\u590d\u800c\u673a\u68b0\u7684\u5de5\u4f5c&#xff0c;\u4e0d\u4ec5\u6d88\u8017\u7740\u5f00\u53d1\u8005\u7684\u7cbe\u529b&#xff0c;\u66f4\u8ba9\u4ee3\u7801\u53d8\u5f97\u5197\u4f59\u3001\u6666\u6da9&#xff0c;\u96be\u4ee5\u7ef4\u62a4\u3002\u800c Lambda\u3001std::function \u4e0e std::bind \u7684\u51fa\u73b0&#xff0c;\u5f7b\u5e95\u98a0\u8986\u4e86\u8fd9\u4e00\u5c40\u9762 \u2014\u2014 \u5b83\u4eec\u4ece \u201c\u8f7b\u91cf\u7ea7\u51fd\u6570\u5b9e\u73b0\u201d\u201c\u53ef\u8c03\u7528\u5bf9\u8c61\u7c7b\u578b\u7edf\u4e00\u201d\u201c\u51fd\u6570\u53c2\u6570\u7075\u6d3b\u9002\u914d\u201d \u4e09\u4e2a\u7ef4\u5ea6&#xff0c;\u4e3a\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u5904\u7406\u63d0\u4f9b\u4e86\u4e00\u7ad9\u5f0f\u89e3\u51b3\u65b9\u6848&#xff0c;\u8ba9 C&#043;&#043; \u7f16\u7a0b\u53d8\u5f97\u66f4\u52a0\u7b80\u6d01\u3001\u7075\u6d3b\u3001\u9ad8\u6548\u3002<\/p>\n<p>Lambda \u8868\u8fbe\u5f0f\u4f5c\u4e3a\u8fd9\u4e09\u5927\u5de5\u5177\u7684 \u201c\u57fa\u77f3\u201d&#xff0c;\u5176\u6838\u5fc3\u9b45\u529b\u5728\u4e8e \u201c\u8f7b\u91cf\u4e0e\u7075\u6d3b\u201d\u3002\u5b83\u4ee5\u533f\u540d\u51fd\u6570\u5bf9\u8c61\u7684\u5f62\u5f0f&#xff0c;\u5141\u8bb8\u6211\u4eec\u5728\u51fd\u6570\u5185\u90e8\u76f4\u63a5\u5b9a\u4e49\u903b\u8f91&#xff0c;\u65e0\u9700\u989d\u5916\u58f0\u660e\u7c7b\u6216\u51fd\u6570&#xff1b;\u5176\u72ec\u7279\u7684\u6355\u6349\u5217\u8868&#xff0c;\u6253\u7834\u4e86\u666e\u901a\u51fd\u6570\u65e0\u6cd5\u76f4\u63a5\u4f7f\u7528\u5916\u5c42\u5c40\u90e8\u53d8\u91cf\u7684\u9650\u5236&#xff0c;\u8ba9\u4ee3\u7801\u903b\u8f91\u66f4\u52a0\u8fde\u8d2f\u3002\u4ece\u5e95\u5c42\u539f\u7406\u6765\u770b&#xff0c;Lambda \u5e76\u975e\u5168\u65b0\u7684\u8bed\u8a00\u7279\u6027&#xff0c;\u800c\u662f\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\u7684\u4eff\u51fd\u6570\u7c7b\u7684 \u201c\u8bed\u6cd5\u7cd6\u201d\u2014\u2014 \u4f46\u8fd9\u5c42\u8bed\u6cd5\u7cd6\u5374\u610f\u4e49\u975e\u51e1&#xff0c;\u5b83\u8ba9\u6211\u4eec\u6446\u8131\u4e86\u624b\u52a8\u7f16\u5199\u4eff\u51fd\u6570\u7684\u7e41\u7410\u6d41\u7a0b&#xff0c;\u7528\u6700\u7b80\u6d01\u7684\u8bed\u6cd5\u5b9e\u73b0\u6700\u6838\u5fc3\u7684\u903b\u8f91\u3002\u65e0\u8bba\u662f STL \u7b97\u6cd5\u4e2d\u7684\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219\u3001\u591a\u7ebf\u7a0b\u4e2d\u7684\u4efb\u52a1\u903b\u8f91&#xff0c;\u8fd8\u662f\u667a\u80fd\u6307\u9488\u7684\u81ea\u5b9a\u4e49\u5220\u9664\u5668&#xff0c;Lambda \u90fd\u80fd\u4ee5 \u201c\u4e00\u884c\u4ee3\u7801\u201d \u7684\u59ff\u6001\u4f18\u96c5\u80dc\u4efb&#xff0c;\u6210\u4e3a\u73b0\u4ee3 C&#043;&#043; \u4e2d\u5904\u7406\u8f7b\u91cf\u7ea7\u903b\u8f91\u7684\u9996\u9009\u3002<\/p>\n<p>\u5982\u679c\u8bf4 Lambda \u662f \u201c\u7075\u6d3b\u7684\u6267\u884c\u8005\u201d&#xff0c;\u90a3\u4e48 std::function \u5c31\u662f \u201c\u7edf\u4e00\u7684\u7ba1\u7406\u8005\u201d\u3002\u5728 C&#043;&#043; \u4e2d&#xff0c;\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u788e\u7247\u5316\u95ee\u9898\u66fe\u957f\u671f\u56f0\u6270\u7740\u5f00\u53d1\u8005&#xff1a;\u666e\u901a\u51fd\u6570\u7684\u7c7b\u578b\u7531\u7b7e\u540d\u51b3\u5b9a&#xff0c;\u4eff\u51fd\u6570\u7684\u7c7b\u578b\u662f\u81ea\u5b9a\u4e49\u7c7b\u7c7b\u578b&#xff0c;Lambda \u7684\u7c7b\u578b\u662f\u7f16\u8bd1\u5668\u751f\u6210\u7684\u533f\u540d\u7c7b\u578b\u3002\u8fd9\u79cd\u7c7b\u578b\u5dee\u5f02\u5bfc\u81f4\u6211\u4eec\u65e0\u6cd5\u7528\u7edf\u4e00\u7684\u65b9\u5f0f\u5b58\u50a8\u3001\u4f20\u9012\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61 \u2014\u2014 \u800c std::function \u7684\u51fa\u73b0&#xff0c;\u6070\u597d\u6253\u7834\u4e86\u8fd9\u4e00\u58c1\u5792\u3002\u4f5c\u4e3a\u4e00\u79cd\u7c7b\u578b\u5b89\u5168\u7684\u901a\u7528\u5305\u88c5\u5668&#xff0c;\u5b83\u80fd\u591f\u5305\u88c5\u4efb\u610f\u7b26\u5408\u7279\u5b9a\u7b7e\u540d\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u65e0\u8bba\u5176\u5e95\u5c42\u662f\u666e\u901a\u51fd\u6570\u3001\u4eff\u51fd\u6570\u3001Lambda&#xff0c;\u8fd8\u662f\u7c7b\u6210\u5458\u51fd\u6570&#xff0c;\u90fd\u80fd\u88ab\u7edf\u4e00\u4e3a\u540c\u4e00\u4e2a std::function \u7c7b\u578b\u3002\u8fd9\u79cd\u7c7b\u578b\u7edf\u4e00\u7684\u7279\u6027&#xff0c;\u8ba9\u6211\u4eec\u53ef\u4ee5\u5c06\u591a\u4e2a\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u5bb9\u5668\u4e2d&#xff0c;\u4e5f\u53ef\u4ee5\u5c06\u5176\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u6216\u8fd4\u56de\u503c\u7075\u6d3b\u4f20\u9012&#xff0c;\u6781\u5927\u5730\u63d0\u5347\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u4e0e\u6269\u5c55\u6027\u3002\u5c24\u5176\u662f\u5728\u5904\u7406\u56de\u8c03\u51fd\u6570\u3001\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u7b49\u573a\u666f\u65f6&#xff0c;std::function \u8ba9\u4ee3\u7801\u7ed3\u6784\u53d8\u5f97\u6e05\u6670\u800c\u89c4\u6574\u3002<\/p>\n<p>\u800c std::bind \u5219\u662f \u201c\u7075\u6d3b\u7684\u9002\u914d\u5668\u201d&#xff0c;\u5b83\u4e3a\u51fd\u6570\u53c2\u6570\u7684\u8c03\u6574\u63d0\u4f9b\u4e86\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u5e38\u5e38\u9047\u5230 \u201c\u51fd\u6570\u53c2\u6570\u8fc7\u591a\u4f46\u53ea\u9700\u4f7f\u7528\u90e8\u5206\u201d\u201c\u53c2\u6570\u987a\u5e8f\u4e0d\u7b26\u5408\u9700\u6c42\u201d \u7b49\u573a\u666f \u2014\u2014 \u5728 C&#043;&#043;11 \u4e4b\u524d&#xff0c;\u8fd9\u4e9b\u95ee\u9898\u53ea\u80fd\u901a\u8fc7\u91cd\u5199\u51fd\u6570\u91cd\u8f7d\u7248\u672c\u6765\u89e3\u51b3&#xff0c;\u4e0d\u4ec5\u5197\u4f59&#xff0c;\u8fd8\u5bb9\u6613\u51fa\u9519\u3002\u800c std::bind \u901a\u8fc7 \u201c\u56fa\u5b9a\u53c2\u6570\u201d\u201c\u8c03\u6574\u53c2\u6570\u987a\u5e8f\u201d\u201c\u6df7\u5408\u9002\u914d\u201d \u7b49\u529f\u80fd&#xff0c;\u65e0\u9700\u4fee\u6539\u539f\u51fd\u6570&#xff0c;\u5c31\u80fd\u751f\u6210\u65b0\u7684\u53ef\u8c03\u7528\u5bf9\u8c61&#xff0c;\u5b8c\u7f8e\u6ee1\u8db3\u5404\u79cd\u53c2\u6570\u8c03\u6574\u9700\u6c42\u3002\u5176\u6838\u5fc3\u7684\u5360\u4f4d\u7b26\u673a\u5236&#xff08;_1\u3001_2 \u7b49&#xff09;&#xff0c;\u8ba9\u53c2\u6570\u7684\u6620\u5c04\u5173\u7cfb\u53d8\u5f97\u76f4\u89c2\u800c\u7075\u6d3b&#xff1b;\u4e0e std::function \u7684\u7ed3\u5408\u4f7f\u7528&#xff0c;\u66f4\u80fd\u8ba9\u8c03\u6574\u540e\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u83b7\u5f97\u7edf\u4e00\u7684\u7c7b\u578b&#xff0c;\u8fdb\u4e00\u6b65\u63d0\u5347\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u3002\u867d\u7136\u5728\u7b80\u5355\u573a\u666f\u4e0b&#xff0c;Lambda \u8868\u8fbe\u5f0f\u4e5f\u80fd\u5b9e\u73b0\u7c7b\u4f3c\u7684\u53c2\u6570\u56fa\u5b9a\u529f\u80fd&#xff0c;\u4f46\u5728\u590d\u6742\u7684\u53c2\u6570\u9002\u914d\u573a\u666f\u4e2d&#xff0c;std::bind \u7684\u4f18\u52bf\u4f9d\u7136\u4e0d\u53ef\u66ff\u4ee3\u3002<\/p>\n<p>\u8fd9\u4e09\u5927\u5de5\u5177\u5e76\u975e\u5b64\u7acb\u5b58\u5728&#xff0c;\u800c\u662f\u76f8\u4e92\u8865\u5145\u3001\u76f8\u8f85\u76f8\u6210&#xff0c;\u6784\u6210\u4e86\u73b0\u4ee3 C&#043;&#043; \u5904\u7406\u53ef\u8c03\u7528\u5bf9\u8c61\u7684 \u201c\u94c1\u4e09\u89d2\u201d&#xff1a;Lambda \u63d0\u4f9b\u7b80\u6d01\u7684\u903b\u8f91\u5b9e\u73b0&#xff0c;std::function \u63d0\u4f9b\u7edf\u4e00\u7684\u7c7b\u578b\u7ba1\u7406&#xff0c;std::bind \u63d0\u4f9b\u7075\u6d3b\u7684\u53c2\u6570\u9002\u914d\u3002\u5f53\u4e09\u8005\u7ed3\u5408\u4f7f\u7528\u65f6&#xff0c;\u5176\u5a01\u529b\u66f4\u662f\u76f8\u5f97\u76ca\u5f70&#xff1a;\u7528 Lambda \u7f16\u5199\u6838\u5fc3\u903b\u8f91&#xff0c;\u7528 std::bind \u8c03\u6574\u53c2\u6570\u683c\u5f0f&#xff0c;\u518d\u7528 std::function \u7edf\u4e00\u7c7b\u578b\u4f20\u9012 \u2014\u2014 \u8fd9\u79cd\u7ec4\u5408\u4e0d\u4ec5\u80fd\u5e94\u5bf9\u5404\u79cd\u590d\u6742\u7684\u7f16\u7a0b\u573a\u666f&#xff0c;\u66f4\u80fd\u8ba9\u4ee3\u7801\u4fdd\u6301\u7b80\u6d01\u3001\u4f18\u96c5\u3001\u9ad8\u6548\u3002\u4f8b\u5982&#xff0c;\u5728\u5f02\u6b65\u7f16\u7a0b\u4e2d&#xff0c;\u6211\u4eec\u53ef\u4ee5\u7528 Lambda \u7f16\u5199\u56de\u8c03\u903b\u8f91&#xff0c;\u7528 std::bind \u56fa\u5b9a\u4e0a\u4e0b\u6587\u53c2\u6570&#xff0c;\u518d\u7528 std::function \u5c06\u56de\u8c03\u51fd\u6570\u4f20\u9012\u7ed9\u5f02\u6b65\u63a5\u53e3&#xff0c;\u6574\u4e2a\u6d41\u7a0b\u6e05\u6670\u800c\u9ad8\u6548\u3002<\/p>\n<p>\u5b66\u4e60\u8fd9\u4e09\u5927\u7279\u6027\u7684\u8fc7\u7a0b&#xff0c;\u4e5f\u662f\u57f9\u517b \u201c\u73b0\u4ee3 C&#043;&#043; \u601d\u7ef4\u201d \u7684\u8fc7\u7a0b\u3002\u73b0\u4ee3 C&#043;&#043; \u7684\u8bbe\u8ba1\u54f2\u5b66\u4e4b\u4e00&#xff0c;\u5c31\u662f \u201c\u7528\u8bed\u6cd5\u7cd6\u7b80\u5316\u590d\u6742\u903b\u8f91&#xff0c;\u7528\u7c7b\u578b\u5b89\u5168\u4fdd\u8bc1\u4ee3\u7801\u53ef\u9760&#xff0c;\u7528\u7075\u6d3b\u63a5\u53e3\u63d0\u5347\u5f00\u53d1\u6548\u7387\u201d\u2014\u2014 \u800c Lambda\u3001std::function \u4e0e std::bind \u6b63\u662f\u8fd9\u4e00\u54f2\u5b66\u7684\u96c6\u4e2d\u4f53\u73b0\u3002\u5728\u5b66\u4e60\u8fc7\u7a0b\u4e2d&#xff0c;\u4f60\u53ef\u80fd\u66fe\u4e3a Lambda \u7684\u6355\u6349\u5217\u8868\u89c4\u5219\u611f\u5230\u56f0\u60d1&#xff1a;\u503c\u6355\u6349\u4e0e\u5f15\u7528\u6355\u6349\u7684\u533a\u522b\u3001mutable \u4fee\u9970\u7b26\u7684\u4f5c\u7528\u3001\u6df7\u5408\u6355\u6349\u7684\u8bed\u6cd5\u9650\u5236&#xff1b;\u4e5f\u66fe\u4e3a std::function \u7684\u7c7b\u578b\u5339\u914d\u70e6\u607c&#xff1a;\u7c7b\u6210\u5458\u51fd\u6570\u7684\u5305\u88c5\u65b9\u5f0f\u3001\u7a7a\u72b6\u6001\u7684\u5224\u65ad&#xff1b;\u8fd8\u66fe\u4e3a std::bind \u7684\u5360\u4f4d\u7b26\u4f7f\u7528\u7ea0\u7ed3&#xff1a;\u5360\u4f4d\u7b26\u7684\u987a\u5e8f\u3001\u5f15\u7528\u53c2\u6570\u7684\u4f20\u9012\u3001\u4e0e Lambda \u7684\u9009\u62e9\u3002\u4f46\u8fd9\u4e9b\u7ec6\u8282\u7684\u6253\u78e8&#xff0c;\u6b63\u662f\u63d0\u5347\u7f16\u7a0b\u529f\u5e95\u7684\u5173\u952e \u2014\u2014 \u6bcf\u4e00\u4e2a\u8bed\u6cd5\u89c4\u5219\u7684\u80cc\u540e&#xff0c;\u90fd\u8574\u542b\u7740\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u903b\u8f91\u4e0e\u8bed\u8a00\u8bbe\u8ba1\u8005\u7684\u8003\u91cf&#xff1b;\u6bcf\u4e00\u6b21\u8c03\u8bd5\u9519\u8bef\u7684\u7ecf\u5386&#xff0c;\u90fd\u80fd\u8ba9\u4f60\u5bf9\u7279\u6027\u7684\u7406\u89e3\u66f4\u52a0\u6df1\u523b\u3002<\/p>\n<p>\u8bf7\u8bb0\u4f4f&#xff0c;\u7f16\u7a0b\u7684\u672c\u8d28\u662f\u89e3\u51b3\u95ee\u9898&#xff0c;\u800c\u5de5\u5177\u7684\u4ef7\u503c\u5728\u4e8e\u8ba9\u89e3\u51b3\u95ee\u9898\u7684\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u3001\u4f18\u96c5\u3002Lambda\u3001std::function \u4e0e std::bind \u4e4b\u6240\u4ee5\u80fd\u6210\u4e3a\u73b0\u4ee3 C&#043;&#043; \u7684\u6838\u5fc3\u5de5\u5177&#xff0c;\u6b63\u662f\u56e0\u4e3a\u5b83\u4eec\u7cbe\u51c6\u5730\u51fb\u4e2d\u4e86\u5f00\u53d1\u8005\u7684\u75db\u70b9&#xff0c;\u8ba9\u539f\u672c\u7e41\u7410\u7684\u5de5\u4f5c\u53d8\u5f97\u7b80\u5355\u3002\u4f46\u5de5\u5177\u7684\u5a01\u529b\u80fd\u5426\u5145\u5206\u53d1\u6325&#xff0c;\u5173\u952e\u5728\u4e8e\u4f7f\u7528\u8005\u662f\u5426\u80fd\u719f\u7ec3\u638c\u63e1\u5176\u7528\u6cd5\u3001\u7406\u89e3\u5176\u539f\u7406\u3002\u56e0\u6b64&#xff0c;\u5728\u5b66\u4e60\u4e4b\u540e&#xff0c;\u4e0d\u59a8\u591a\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5b9e\u8df5&#xff1a;\u5c1d\u8bd5\u7528 Lambda \u66ff\u4ee3\u539f\u672c\u7684\u4eff\u51fd\u6570&#xff0c;\u7528 std::function \u7edf\u4e00\u56de\u8c03\u51fd\u6570\u7c7b\u578b&#xff0c;\u7528 std::bind \u8c03\u6574\u53c2\u6570\u9002\u914d\u573a\u666f\u3002\u53ea\u6709\u5728\u5b9e\u8df5\u4e2d\u4e0d\u65ad\u6478\u7d22\u3001\u603b\u7ed3&#xff0c;\u624d\u80fd\u771f\u6b63\u5c06\u8fd9\u4e9b\u5de5\u5177\u5185\u5316\u4e3a\u81ea\u5df1\u7684\u80fd\u529b&#xff0c;\u8ba9\u5b83\u4eec\u6210\u4e3a\u4f60\u7f16\u7a0b\u5de5\u5177\u7bb1\u4e2d\u6700\u53ef\u9760\u7684 \u201c\u4f19\u4f34\u201d\u3002<\/p>\n<p>C&#043;&#043; \u662f\u4e00\u95e8\u4e0d\u65ad\u6f14\u8fdb\u7684\u8bed\u8a00&#xff0c;\u4ece C&#043;&#043;11 \u5230 C&#043;&#043;23&#xff0c;\u6bcf\u4e00\u4e2a\u7248\u672c\u90fd\u5728\u5f15\u5165\u65b0\u7684\u7279\u6027&#xff0c;\u8ba9\u8bed\u8a00\u53d8\u5f97\u66f4\u52a0\u5f3a\u5927\u3001\u66f4\u52a0\u6613\u7528\u3002Lambda\u3001std::function \u4e0e std::bind \u4f5c\u4e3a C&#043;&#043;11 \u7684\u6838\u5fc3\u7279\u6027&#xff0c;\u4e0d\u4ec5\u662f\u73b0\u4ee3 C&#043;&#043; \u7f16\u7a0b\u7684\u57fa\u7840&#xff0c;\u66f4\u662f\u901a\u5f80\u66f4\u9ad8\u9636\u7279\u6027\u7684\u6865\u6881\u3002\u638c\u63e1\u4e86\u5b83\u4eec&#xff0c;\u4f60\u5c06\u80fd\u66f4\u8f7b\u677e\u5730\u7406\u89e3\u540e\u7eed\u7248\u672c\u4e2d\u7684\u65b0\u7279\u6027&#xff0c;\u5982 C&#043;&#043;17 \u7684 std::invoke\u3001C&#043;&#043;20 \u7684\u6982\u5ff5\u4e0e\u8303\u56f4\u5e93\u7b49\u3002\u5b66\u4e60 C&#043;&#043; \u7684\u9053\u8def\u6216\u8bb8\u6f2b\u957f&#xff0c;\u4f46\u6bcf\u4e00\u6b21\u5bf9\u65b0\u7279\u6027\u7684\u638c\u63e1&#xff0c;\u6bcf\u4e00\u6b21\u4ee3\u7801\u8d28\u91cf\u7684\u63d0\u5347&#xff0c;\u90fd\u662f\u5bf9\u81ea\u5df1\u7684\u80af\u5b9a\u4e0e\u6fc0\u52b1\u3002<\/p>\n<p>\u6700\u540e&#xff0c;\u613f\u4f60\u4ee5\u8fd9\u4e09\u5927\u5de5\u5177\u4e3a\u7ffc&#xff0c;\u5728\u73b0\u4ee3 C&#043;&#043; \u7684\u4e16\u754c\u4e2d\u81ea\u7531\u9a70\u9a8b \u2014\u2014 \u7528 Lambda \u7684\u7b80\u6d01\u52fe\u52d2\u903b\u8f91&#xff0c;\u7528 std::function \u7684\u7edf\u4e00\u7ba1\u7406\u7c7b\u578b&#xff0c;\u7528 std::bind \u7684\u7075\u6d3b\u9002\u914d\u53c2\u6570&#xff0c;\u5199\u51fa\u66f4\u52a0\u4f18\u96c5\u3001\u9ad8\u6548\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u4e0d\u8981\u754f\u60e7\u5b66\u4e60\u4e2d\u7684\u96be\u70b9&#xff0c;\u56e0\u4e3a\u6bcf\u4e00\u6b21\u7a81\u7834\u90fd\u5c06\u8ba9\u4f60\u53d8\u5f97\u66f4\u52a0\u5f3a\u5927&#xff1b;\u4e0d\u8981\u6ee1\u8db3\u4e8e\u8868\u9762\u7684\u7528\u6cd5&#xff0c;\u56e0\u4e3a\u6df1\u5165\u539f\u7406\u624d\u80fd\u771f\u6b63\u638c\u63e1\u672c\u8d28\u3002\u613f\u4f60\u4fdd\u6301\u5bf9\u6280\u672f\u7684\u70ed\u7231\u4e0e\u597d\u5947\u5fc3&#xff0c;\u5728 C&#043;&#043; \u7684\u5b66\u4e60\u4e4b\u8def\u4e0a\u4e0d\u65ad\u524d\u884c&#xff0c;\u89e3\u9501\u66f4\u591a\u8bed\u8a00\u7684\u9b45\u529b&#xff0c;\u5b9e\u73b0\u81ea\u5df1\u7684\u7f16\u7a0b\u7406\u60f3\u3002<\/p>\n<p>\u795d\u4f60\u5728\u73b0\u4ee3 C&#043;&#043; \u7684\u7f16\u7a0b\u65c5\u7a0b\u4e2d&#xff0c;\u6536\u83b7\u6ee1\u6ee1&#xff0c;\u4e00\u8def\u7e41\u82b1&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u7bc7\u4ecb\u7ecd&#xff1a;hello \u5927\u5bb6&#xff0c;\u672c\u7bc7\u535a\u5ba2\u6211\u4eec\u6765\u5b66\u4e60C11\u4e2d\u7684lambda\u3001function\u3001bind\u3002\u5728 C11 \u6807\u51c6\u8bde\u751f\u4e4b\u524d&#xff0c;\u5f00\u53d1\u8005\u5728\u5904\u7406\u8f7b\u91cf\u7ea7\u51fd\u6570\u903b\u8f91\u3001\u7edf\u4e00\u53ef\u8c03\u7528\u5bf9\u8c61\u7c7b\u578b\u3001\u8c03\u6574\u51fd\u6570\u53c2\u6570\u683c\u5f0f\u7b49\u573a\u666f\u65f6&#xff0c;\u5f80\u5f80\u9762\u4e34\u8bf8\u591a\u63a3\u8098&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u89c4\u5219\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u4eff\u51fd\u6570\u7c7b&#xff0c;\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u9700\u8981\u590d\u6742\u7684\u6a21\u677f\u7279\u5316&#xff0c;\u8c03\u6574\u51fd\u6570\u53c2\u6570\u987a\u5e8f\u5219\u9700\u8981\u91cd\u5199\u51fd\u6570\u91cd\u8f7d\u7248\u672c\u3002\u8fd9\u4e9b\u7e41\u7410\u7684\u64cd\u4f5c\u4e0d\u4ec5\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u5197\u4f59\u5ea6&amp;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10778,7274,4502,55,5364],"topic":[],"class_list":["post-94101","post","type-post","status-publish","format-standard","hentry","category-server","tag-function","tag-7274","tag-bind","tag-c","tag-lambda"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/94101.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5f00\u7bc7\u4ecb\u7ecd&#xff1a;hello \u5927\u5bb6&#xff0c;\u672c\u7bc7\u535a\u5ba2\u6211\u4eec\u6765\u5b66\u4e60C11\u4e2d\u7684lambda\u3001function\u3001bind\u3002\u5728 C11 \u6807\u51c6\u8bde\u751f\u4e4b\u524d&#xff0c;\u5f00\u53d1\u8005\u5728\u5904\u7406\u8f7b\u91cf\u7ea7\u51fd\u6570\u903b\u8f91\u3001\u7edf\u4e00\u53ef\u8c03\u7528\u5bf9\u8c61\u7c7b\u578b\u3001\u8c03\u6574\u51fd\u6570\u53c2\u6570\u683c\u5f0f\u7b49\u573a\u666f\u65f6&#xff0c;\u5f80\u5f80\u9762\u4e34\u8bf8\u591a\u63a3\u8098&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u89c4\u5219\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u4eff\u51fd\u6570\u7c7b&#xff0c;\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u9700\u8981\u590d\u6742\u7684\u6a21\u677f\u7279\u5316&#xff0c;\u8c03\u6574\u51fd\u6570\u53c2\u6570\u987a\u5e8f\u5219\u9700\u8981\u91cd\u5199\u51fd\u6570\u91cd\u8f7d\u7248\u672c\u3002\u8fd9\u4e9b\u7e41\u7410\u7684\u64cd\u4f5c\u4e0d\u4ec5\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u5197\u4f59\u5ea6&amp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/94101.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-13T11:08:57+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"40 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94101.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/94101.html\",\"name\":\"\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-13T11:08:57+00:00\",\"dateModified\":\"2026-08-13T11:08:57+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/94101.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/94101.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94101.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/94101.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5f00\u7bc7\u4ecb\u7ecd&#xff1a;hello \u5927\u5bb6&#xff0c;\u672c\u7bc7\u535a\u5ba2\u6211\u4eec\u6765\u5b66\u4e60C11\u4e2d\u7684lambda\u3001function\u3001bind\u3002\u5728 C11 \u6807\u51c6\u8bde\u751f\u4e4b\u524d&#xff0c;\u5f00\u53d1\u8005\u5728\u5904\u7406\u8f7b\u91cf\u7ea7\u51fd\u6570\u903b\u8f91\u3001\u7edf\u4e00\u53ef\u8c03\u7528\u5bf9\u8c61\u7c7b\u578b\u3001\u8c03\u6574\u51fd\u6570\u53c2\u6570\u683c\u5f0f\u7b49\u573a\u666f\u65f6&#xff0c;\u5f80\u5f80\u9762\u4e34\u8bf8\u591a\u63a3\u8098&#xff1a;\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u6392\u5e8f\u89c4\u5219\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u4eff\u51fd\u6570\u7c7b&#xff0c;\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u9700\u8981\u590d\u6742\u7684\u6a21\u677f\u7279\u5316&#xff0c;\u8c03\u6574\u51fd\u6570\u53c2\u6570\u987a\u5e8f\u5219\u9700\u8981\u91cd\u5199\u51fd\u6570\u91cd\u8f7d\u7248\u672c\u3002\u8fd9\u4e9b\u7e41\u7410\u7684\u64cd\u4f5c\u4e0d\u4ec5\u589e\u52a0\u4e86\u4ee3\u7801\u7684\u5197\u4f59\u5ea6&amp;","og_url":"https:\/\/www.wsisp.com\/helps\/94101.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-13T11:08:57+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"40 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/94101.html","url":"https:\/\/www.wsisp.com\/helps\/94101.html","name":"\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-13T11:08:57+00:00","dateModified":"2026-08-13T11:08:57+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/94101.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/94101.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/94101.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5bf9\u4e8eC++\uff1aC++11\u4e2dlambda\u3001function\u3001bind\u7684\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=94101"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94101\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=94101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=94101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=94101"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=94101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}