{"id":98,"date":"2026-03-22T17:23:15","date_gmt":"2026-03-22T09:23:15","guid":{"rendered":"https:\/\/codergodv.xyz\/?p=98"},"modified":"2026-03-22T17:23:16","modified_gmt":"2026-03-22T09:23:16","slug":"c11-%e5%8c%85%e8%a3%85%e5%99%a8","status":"publish","type":"post","link":"https:\/\/codergodv.xyz\/index.php\/2026\/03\/22\/c11-%e5%8c%85%e8%a3%85%e5%99%a8\/","title":{"rendered":"C++11\u2014\u2014\u2014 \u5305\u88c5\u5668"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u5305\u88c5\u5668<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">function\u5305\u88c5\u5668<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">function\u5305\u88c5\u5668\u4ecb\u7ecd<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>function\u5305\u88c5\u5668<\/p>\n<\/blockquote>\n\n\n\n<p>function\u662f\u4e00\u79cd\u51fd\u6570\u5305\u88c5\u5668\uff0c\u4e5f\u53eb\u505a\u9002\u914d\u5668\u3002\u5b83\u53ef\u4ee5\u5bf9\u53ef\u8c03\u7528\u5bf9\u8c61\u8fdb\u884c\u5305\u88c5\uff0cC++\u4e2d\u7684function\u672c\u8d28\u5c31\u662f\u4e00\u4e2a<a href=\"https:\/\/so.csdn.net\/so\/search?q=\u7c7b\u6a21\u677f&amp;spm=1001.2101.3001.7020\">\u7c7b\u6a21\u677f<\/a>\u3002<\/p>\n\n\n\n<p>function\u7c7b\u6a21\u677f\u7684\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;class T&gt; function;     \/\/ undefined\ntemplate &lt;class Ret, class... Args&gt;\nclass function&lt;Ret(Args...)&gt;;<\/code><\/pre>\n\n\n\n<p>\u6a21\u677f\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Ret<\/code>\uff1a\u88ab\u5305\u88c5\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><code>Args...<\/code>\uff1a\u88ab\u5305\u88c5\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u5f62\u53c2\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5305\u88c5\u793a\u4f8b<\/p>\n<\/blockquote>\n\n\n\n<p>function\u5305\u88c5\u5668\u53ef\u4ee5\u5bf9\u53ef\u8c03\u7528\u5bf9\u8c61\u8fdb\u884c\u5305\u88c5\uff0c\u5305\u62ec<a href=\"https:\/\/so.csdn.net\/so\/search?q=\u51fd\u6570\u6307\u9488&amp;spm=1001.2101.3001.7020\">\u51fd\u6570\u6307\u9488<\/a>\uff08\u51fd\u6570\u540d\uff09\u3001\u4eff\u51fd\u6570\uff08\u51fd\u6570\u5bf9\u8c61\uff09\u3001lambda\u8868\u8fbe\u5f0f\u3001\u7c7b\u7684\u6210\u5458\u51fd\u6570\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int f(int a, int b)\n{\n    return a + b;\n}\nstruct Functor\n{\npublic:\n    int operator()(int a, int b)\n    {\n        return a + b;\n    }\n};\nclass Plus\n{\npublic:\n    static int plusi(int a, int b)\n    {\n        return a + b;\n    }\n    double plusd(double a, double b)\n    {\n        return a + b;\n    }\n};\nint main()\n{\n    \/\/1\u3001\u5305\u88c5\u51fd\u6570\u6307\u9488\uff08\u51fd\u6570\u540d\uff09\n    function&lt;int(int, int)&gt; func1 = f;\n    cout &lt;&lt; func1(1, 2) &lt;&lt; endl;\n\n    \/\/2\u3001\u5305\u88c5\u4eff\u51fd\u6570\uff08\u51fd\u6570\u5bf9\u8c61\uff09\n    function&lt;int(int, int)&gt; func2 = Functor();\n    cout &lt;&lt; func2(1, 2) &lt;&lt; endl;\n\n    \/\/3\u3001\u5305\u88c5lambda\u8868\u8fbe\u5f0f\n    function&lt;int(int, int)&gt; func3 = &#91;](int a, int b){return a + b; };\n    cout &lt;&lt; func3(1, 2) &lt;&lt; endl;\n\n    \/\/4\u3001\u7c7b\u7684\u9759\u6001\u6210\u5458\u51fd\u6570\n    \/\/function&lt;int(int, int)&gt; func4 = Plus::plusi;\n    function&lt;int(int, int)&gt; func4 = &amp;Plus::plusi; \/\/&amp;\u53ef\u7701\u7565\n    cout &lt;&lt; func4(1, 2) &lt;&lt; endl;\n\n    \/\/5\u3001\u7c7b\u7684\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\n    function&lt;double(Plus, double, double)&gt; func5 = &amp;Plus::plusd; \/\/&amp;\u4e0d\u53ef\u7701\u7565\n    cout &lt;&lt; func5(Plus(), 1.1, 2.2) &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5305\u88c5\u65f6\u6307\u660e\u8fd4\u56de\u503c\u7c7b\u578b\u548c\u5404\u5f62\u53c2\u7c7b\u578b\uff0c\u7136\u540e\u5c06\u53ef\u8c03\u7528\u5bf9\u8c61\u8d4b\u503c\u7ed9function\u5305\u88c5\u5668\u5373\u53ef\uff0c\u5305\u88c5\u540efunction\u5bf9\u8c61\u5c31\u53ef\u4ee5\u50cf\u666e\u901a\u51fd\u6570\u4e00\u6837\u4f7f\u7528\u4e86\u3002<\/li>\n\n\n\n<li>\u53d6\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u5730\u5740\u53ef\u4ee5\u4e0d\u7528\u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u201c&amp;\u201d\uff0c\u4f46\u53d6\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u5730\u5740\u5fc5\u987b\u4f7f\u7528\u53d6\u5730\u5740\u8fd0\u7b97\u7b26\u201c&amp;\u201d\u3002<\/li>\n\n\n\n<li>\u5305\u88c5\u975e\u9759\u6001\u7684\u6210\u5458\u51fd\u6570\u65f6\u9700\u8981\u6ce8\u610f\uff0c\u975e\u9759\u6001\u6210\u5458\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u9690\u85cfthis\u6307\u9488\uff0c\u56e0\u6b64\u5728\u5305\u88c5\u65f6\u9700\u8981\u6307\u660e\u7b2c\u4e00\u4e2a\u5f62\u53c2\u7684\u7c7b\u578b\u4e3a\u7c7b\u7684\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">function\u5305\u88c5\u5668\u7edf\u4e00\u7c7b\u578b<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u4ee5\u4e0b\u51fd\u6570\u6a21\u677fuseF\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f20\u5165\u8be5\u51fd\u6570\u6a21\u677f\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4efb\u610f\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u6bd4\u5982\u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u3001lambda\u8868\u8fbe\u5f0f\u7b49\u3002<\/li>\n\n\n\n<li>useF\u4e2d\u5b9a\u4e49\u4e86\u9759\u6001\u53d8\u91cfcount\uff0c\u5e76\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u5c06count\u7684\u503c\u548c\u5730\u5740\u8fdb\u884c\u4e86\u6253\u5370\uff0c\u53ef\u5224\u65ad\u591a\u6b21\u8c03\u7528\u65f6\u8c03\u7528\u7684\u662f\u5426\u662f\u540c\u4e00\u4e2auseF\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template&lt;class F, class T&gt;\nT useF(F f, T x)\n{\n    static int count = 0;\n    cout &lt;&lt; \"count: \" &lt;&lt; ++count &lt;&lt; endl;\n    cout &lt;&lt; \"count: \" &lt;&lt; &amp;count &lt;&lt; endl;\n\n    return f(x);\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u4f20\u5165\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7c7b\u578b\u76f8\u540c\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f20\u5165\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u662f\u4e0d\u540c\u7684\uff0c\u90a3\u4e48\u5728\u7f16\u8bd1\u9636\u6bb5\u8be5\u51fd\u6570\u6a21\u677f\u5c31\u4f1a\u88ab\u5b9e\u4f8b\u5316\u591a\u6b21\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double f(double i)\n{\n    return i \/ 2;\n}\nstruct Functor\n{\n    double operator()(double d)\n    {\n        return d \/ 3;\n    }\n};\nint main()\n{\n    \/\/\u51fd\u6570\u6307\u9488\n    cout &lt;&lt; useF(f, 11.11) &lt;&lt; endl;\n\n    \/\/\u4eff\u51fd\u6570\n    cout &lt;&lt; useF(Functor(), 11.11) &lt;&lt; endl;\n\n    \/\/lambda\u8868\u8fbe\u5f0f\n    cout &lt;&lt; useF(&#91;](double d)-&gt;double{return d \/ 4; }, 11.11) &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8e\u51fd\u6570\u6307\u9488\u3001<a href=\"https:\/\/so.csdn.net\/so\/search?q=\u4eff\u51fd\u6570&amp;spm=1001.2101.3001.7020\">\u4eff\u51fd\u6570<\/a>\u3001lambda\u8868\u8fbe\u5f0f\u662f\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u56e0\u6b64useF\u51fd\u6570\u4f1a\u88ab\u5b9e\u4f8b\u5316\u51fa\u4e09\u4efd\uff0c\u4e09\u6b21\u8c03\u7528useF\u51fd\u6570\u6240\u6253\u5370count\u7684\u5730\u5740\u4e5f\u662f\u4e0d\u540c\u7684\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f46\u5b9e\u9645\u8fd9\u91cc\u6839\u672c\u6ca1\u6709\u5fc5\u8981\u5b9e\u4f8b\u5316\u51fa\u4e09\u4efduseF\u51fd\u6570\uff0c\u56e0\u4e3a\u4e09\u6b21\u8c03\u7528useF\u51fd\u6570\u65f6\u4f20\u5165\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u867d\u7136\u662f\u4e0d\u540c\u7c7b\u578b\u7684\uff0c\u4f46\u8fd9\u4e09\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u548c\u5f62\u53c2\u7c7b\u578b\u90fd\u662f\u76f8\u540c\u7684\u3002<\/li>\n\n\n\n<li>\u8fd9\u65f6\u5c31\u53ef\u4ee5\u7528\u5305\u88c5\u5668\u5206\u522b\u5bf9\u7740\u4e09\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\u8fdb\u884c\u5305\u88c5\uff0c\u7136\u540e\u518d\u7528\u8fd9\u4e09\u4e2a\u5305\u88c5\u540e\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u6765\u8c03\u7528useF\u51fd\u6570\uff0c\u8fd9\u65f6\u5c31\u53ea\u4f1a\u5b9e\u4f8b\u5316\u51fa\u4e00\u4efduseF\u51fd\u6570\u3002<\/li>\n\n\n\n<li>\u6839\u672c\u539f\u56e0\u5c31\u662f\u56e0\u4e3a\u5305\u88c5\u540e\uff0c\u8fd9\u4e09\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\u90fd\u662f\u76f8\u540c\u7684function\u7c7b\u578b\uff0c\u56e0\u6b64\u6700\u7ec8\u53ea\u4f1a\u5b9e\u4f8b\u5316\u51fa\u4e00\u4efduseF\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u6a21\u677f\u53c2\u6570\u7684\u7c7b\u578b\u5c31\u662ffunction\u7c7b\u578b\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main()\n{\n    \/\/\u51fd\u6570\u540d\n    function&lt;double(double)&gt; func1 = func;\n    cout &lt;&lt; useF(func1, 11.11) &lt;&lt; endl;\n\n    \/\/\u51fd\u6570\u5bf9\u8c61\n    function&lt;double(double)&gt; func2 = Functor();\n    cout &lt;&lt; useF(func2, 11.11) &lt;&lt; endl;\n\n    \/\/lambda\u8868\u8fbe\u5f0f\n    function&lt;double(double)&gt; func3 = &#91;](double d)-&gt;double{return d \/ 4; };\n    cout &lt;&lt; useF(func3, 11.11) &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u65f6\u4e09\u6b21\u8c03\u7528useF\u51fd\u6570\u6240\u6253\u5370count\u7684\u5730\u5740\u5c31\u662f\u76f8\u540c\u7684\uff0c\u5e76\u4e14count\u5728\u4e09\u6b21\u8c03\u7528\u540e\u4f1a\u88ab\u7d2f\u52a0\u52303\uff0c\u8868\u793a\u8fd9\u4e00\u4e2auseF\u51fd\u6570\u88ab\u8c03\u7528\u4e86\u4e09\u6b21\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">function\u5305\u88c5\u5668\u7b80\u5316\u4ee3\u7801<\/h3>\n\n\n\n<p>\u6c42\u89e3\u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b9a\u4e49\u4e00\u4e2a\u6808\uff0c\u4f9d\u6b21\u904d\u5386\u6240\u7ed9\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u904d\u5386\u5230\u7684\u5b57\u7b26\u4e32\u662f\u6570\u5b57\u5219\u76f4\u63a5\u5165\u6808\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u904d\u5386\u5230\u7684\u5b57\u7b26\u4e32\u662f\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\u7b26\uff0c\u5219\u4ece\u6808\u5b9a\u629b\u51fa\u4e24\u4e2a\u6570\u5b57\u8fdb\u884c\u5bf9\u5e94\u7684\u8fd0\u7b97\uff0c\u5e76\u5c06\u8fd0\u7b97\u540e\u5f97\u5230\u7684\u7ed3\u679c\u538b\u5165\u6808\u4e2d\u3002<\/li>\n\n\n\n<li>\u6240\u7ed9\u5b57\u7b26\u4e32\u904d\u5386\u5b8c\u6bd5\u540e\uff0c\u6808\u9876\u7684\u6570\u5b57\u5c31\u662f\u9006\u6ce2\u5170\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int evalRPN(vector&lt;string&gt;&amp; tokens) {\n        stack&lt;int&gt; st;\n        for (const auto&amp; str : tokens)\n        {\n            int left, right;\n            if (str == \"+\" || str == \"-\" || str == \"*\" || str == \"\/\")\n            {\n                right = st.top();\n                st.pop();\n                left = st.top();\n                st.pop();\n                switch (str&#91;0])\n                {\n                case '+':\n                    st.push(left + right);\n                    break;\n                case '-':\n                    st.push(left - right);\n                    break;\n                case '*':\n                    st.push(left * right);\n                    break;\n                case '\/':\n                    st.push(left \/ right);\n                    break;\n                default:\n                    break;\n                }\n            }\n            else\n            {\n                st.push(stoi(str));\n            }\n        }\n        return st.top();\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7switch\u8bed\u53e5\u6765\u5224\u65ad\u672c\u6b21\u9700\u8981\u8fdb\u884c\u54ea\u79cd\u8fd0\u7b97\uff0c\u5982\u679c\u8fd0\u7b97\u7c7b\u578b\u589e\u52a0\u4e86\uff0c\u6bd4\u5982\u589e\u52a0\u4e86\u6c42\u4f59\u3001\u5e42\u3001\u5bf9\u6570\u7b49\u8fd0\u7b97\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u5728switch\u8bed\u53e5\u7684\u540e\u9762\u4e2d\u7ee7\u7eed\u589e\u52a0case\u8bed\u53e5\u3002<\/p>\n\n\n\n<p>\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u7528\u5305\u88c5\u5668\u6765\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5efa\u7acb\u5404\u4e2a\u8fd0\u7b97\u7b26\u4e0e\u5176\u5bf9\u5e94\u9700\u8981\u6267\u884c\u7684\u51fd\u6570\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u5f53\u9700\u8981\u6267\u884c\u67d0\u4e00\u8fd0\u7b97\u65f6\u5c31\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u8fd0\u7b97\u7b26\u627e\u5230\u5bf9\u5e94\u7684\u51fd\u6570\u8fdb\u884c\u6267\u884c\u3002<\/li>\n\n\n\n<li>\u5f53\u8fd0\u7b97\u7c7b\u578b\u589e\u52a0\u65f6\uff0c\u5c31\u53ea\u9700\u8981\u5efa\u7acb\u65b0\u589e\u8fd0\u7b97\u7b26\u4e0e\u5176\u5bf9\u5e94\u51fd\u6570\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u5373\u53ef\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution {\npublic:\n    int evalRPN(vector&lt;string&gt;&amp; tokens) {\n        stack&lt;int&gt; st;\n        unordered_map&lt;string, function&lt;int(int, int)&gt;&gt; opMap = {\n            { \"+\", &#91;](int a, int b){return a + b; } },\n            { \"-\", &#91;](int a, int b){return a - b; } },\n            { \"*\", &#91;](int a, int b){return a * b; } },\n            { \"\/\", &#91;](int a, int b){return a \/ b; } }\n        };\n        for (const auto&amp; str : tokens)\n        {\n            int left, right;\n            if (str == \"+\" || str == \"-\" || str == \"*\" || str == \"\/\")\n            {\n                right = st.top();\n                st.pop();\n                left = st.top();\n                st.pop();\n                st.push(opMap&#91;str](left, right));\n            }\n            else\n            {\n                st.push(stoi(str));\n            }\n        }\n        return st.top();\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u5efa\u7acb\u7684\u662f\u8fd0\u7b97\u7b26\u4e0efunction\u7c7b\u578b\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u56e0\u6b64\u65e0\u8bba\u662f\u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u8fd8\u662flambda\u8868\u8fbe\u5f0f\u90fd\u53ef\u4ee5\u5728\u5305\u88c5\u540e\u4e0e\u5bf9\u5e94\u7684\u8fd0\u7b97\u7b26\u8fdb\u884c\u7ed1\u5b9a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">function\u5305\u88c5\u5668\u7684\u610f\u4e49<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u7c7b\u578b\u8fdb\u884c\u7edf\u4e00\uff0c\u4fbf\u4e8e\u6211\u4eec\u5bf9\u5176\u8fdb\u884c\u7edf\u4e00\u5316\u7ba1\u7406\u3002<\/li>\n\n\n\n<li>\u5305\u88c5\u540e\u660e\u786e\u4e86\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u8fd4\u56de\u503c\u548c\u5f62\u53c2\u7c7b\u578b\uff0c\u66f4\u52a0\u65b9\u4fbf\u4f7f\u7528\u8005\u4f7f\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">bind\u5305\u88c5\u5668<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">bind\u5305\u88c5\u5668\u4ecb\u7ecd<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>bind\u5305\u88c5\u5668<\/p>\n<\/blockquote>\n\n\n\n<p>bind\u4e5f\u662f\u4e00\u79cd\u51fd\u6570\u5305\u88c5\u5668\uff0c\u4e5f\u53eb\u505a\u9002\u914d\u5668\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u6765\u201c\u9002\u5e94\u201d\u539f\u5bf9\u8c61\u7684\u53c2\u6570\u5217\u8868\uff0cC++\u4e2d\u7684bind\u672c\u8d28\u662f\u4e00\u4e2a\u51fd\u6570\u6a21\u677f\u3002<\/p>\n\n\n\n<p>bind\u51fd\u6570\u6a21\u677f\u7684\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;class Fn, class... Args&gt;\n\/* unspecified *\/ bind(Fn&amp;&amp; fn, Args&amp;&amp;... args);\ntemplate &lt;class Ret, class Fn, class... Args&gt;\n\/* unspecified *\/ bind(Fn&amp;&amp; fn, Args&amp;&amp;... args);<\/code><\/pre>\n\n\n\n<p>\u6a21\u677f\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>fn<\/code>\uff1a\u53ef\u8c03\u7528\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>args...<\/code>\uff1a\u8981\u7ed1\u5b9a\u7684\u53c2\u6570\u5217\u8868\uff1a\u503c\u6216\u5360\u4f4d\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8c03\u7528bind\u7684\u4e00\u822c\u5f62\u5f0f<\/p>\n<\/blockquote>\n\n\n\n<p>\u8c03\u7528bind\u7684\u4e00\u822c\u5f62\u5f0f\u4e3a\uff1a<code>auto newCallable = bind(callable, arg_list);<\/code><\/p>\n\n\n\n<p><strong>\u89e3\u91ca\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>callable<\/code>\uff1a\u9700\u8981\u5305\u88c5\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>newCallable<\/code>\uff1a\u751f\u6210\u7684\u65b0\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>arg_list<\/code>\uff1a\u9017\u53f7\u5206\u9694\u7684\u53c2\u6570\u5217\u8868\uff0c\u5bf9\u5e94\u7ed9\u5b9a\u7684callable\u7684\u53c2\u6570\u3002\u5f53\u8c03\u7528newCallable\u65f6\uff0cnewCallable\u4f1a\u8c03\u7528callable\uff0c\u5e76\u4f20\u7ed9\u5b83arg_list\u4e2d\u7684\u53c2\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>arg_list\u4e2d\u7684\u53c2\u6570\u53ef\u80fd\u5305\u542b\u5f62\u5982_n\u7684\u540d\u5b57\uff0c\u5176\u4e2dn\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u8fd9\u4e9b\u53c2\u6570\u662f\u201c\u5360\u4f4d\u7b26\u201d\uff0c\u8868\u793anewCallable\u7684\u53c2\u6570\uff0c\u5b83\u4eec\u5360\u636e\u4e86\u4f20\u9012\u7ed9newCallable\u7684\u53c2\u6570\u7684\u201c\u4f4d\u7f6e\u201d\u3002\u6570\u503cn\u8868\u793a\u751f\u6210\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u4e2d\u53c2\u6570\u7684\u4f4d\u7f6e\uff0c\u6bd4\u5982_1\u4e3anewCallable\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c_2\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u9664\u4e86\u7528auto\u63a5\u6536\u5305\u88c5\u540e\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u4e5f\u53ef\u4ee5\u7528function\u7c7b\u578b\u6307\u660e\u8fd4\u56de\u503c\u548c\u5f62\u53c2\u7c7b\u578b\u540e\u63a5\u6536\u5305\u88c5\u540e\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">bind\u5305\u88c5\u5668\u7ed1\u5b9a\u56fa\u5b9a\u53c2\u6570<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u65e0\u610f\u4e49\u7684\u7ed1\u5b9a<\/p>\n<\/blockquote>\n\n\n\n<p>\u4e0b\u9762\u8fd9\u79cd\u7ed1\u5b9a\u5c31\u662f\u65e0\u610f\u4e49\u7684\u7ed1\u5b9a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int Plus(int a, int b)\n{\n    return a + b;\n}\nint main()\n{\n    \/\/\u65e0\u610f\u4e49\u7684\u7ed1\u5b9a\n    function&lt;int(int, int)&gt; func = bind(Plus, placeholders::_1, placeholders::_2);\n    cout &lt;&lt; func(1, 2) &lt;&lt; endl; \/\/3\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u7ed1\u5b9a\u65f6\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u5165\u51fd\u6570\u6307\u9488\u8fd9\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u4f46\u540e\u7eed\u4f20\u5165\u7684\u8981\u7ed1\u5b9a\u7684\u53c2\u6570\u5217\u8868\u4f9d\u6b21\u662fplaceholders::_1\u548cplaceholders::_2\uff0c\u8868\u793a\u540e\u7eed\u8c03\u7528\u65b0\u751f\u6210\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6\uff0c\u4f20\u5165\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u7ed9placeholders::_1\uff0c\u4f20\u5165\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u7ed9placeholders::_2\u3002\u6b64\u65f6\u7ed1\u5b9a\u540e\u751f\u6210\u7684\u65b0\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u7684\u4f20\u53c2\u65b9\u5f0f\uff0c\u548c\u539f\u6765\u6ca1\u6709\u7ed1\u5b9a\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u8bf4\u8fd9\u662f\u4e00\u4e2a\u65e0\u610f\u4e49\u7684\u7ed1\u5b9a\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7ed1\u5b9a\u56fa\u5b9a\u53c2\u6570<\/p>\n<\/blockquote>\n\n\n\n<p>\u5982\u679c\u60f3\u628aPlus\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u56fa\u5b9a\u7ed1\u5b9a\u4e3a10\uff0c\u53ef\u4ee5\u5728\u7ed1\u5b9a\u65f6\u5c06\u53c2\u6570\u5217\u8868\u7684placeholders::_2\u8bbe\u7f6e\u4e3a10\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int Plus(int a, int b)\n{\n    return a + b;\n}\nint main()\n{\n    \/\/\u7ed1\u5b9a\u56fa\u5b9a\u53c2\u6570\n    function&lt;int(int)&gt; func = bind(Plus, placeholders::_1, 10);\n    cout &lt;&lt; func(2) &lt;&lt; endl; \/\/12\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u8c03\u7528\u7ed1\u5b9a\u540e\u65b0\u751f\u6210\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6\u5c31\u53ea\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u53c2\u6570\uff0c\u5b83\u4f1a\u5c06\u8be5\u503c\u4e0e10\u76f8\u52a0\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fd4\u56de\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">bind\u5305\u88c5\u5668\u8c03\u6574\u4f20\u53c2\u987a\u5e8f<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u8c03\u6574\u4f20\u53c2\u987a\u5e8f<\/p>\n<\/blockquote>\n\n\n\n<p>\u5bf9\u4e8e\u4e0b\u9762Sub\u7c7b\u4e2d\u7684sub\u6210\u5458\u51fd\u6570\uff0csub\u6210\u5458\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u9690\u85cf\u7684this\u6307\u9488\uff0c\u5982\u679c\u60f3\u8981\u5728\u8c03\u7528sub\u6210\u5458\u51fd\u6570\u65f6\u4e0d\u7528\u5bf9\u8c61\u8fdb\u884c\u8c03\u7528\uff0c\u90a3\u4e48\u53ef\u4ee5\u5c06sub\u6210\u5458\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u56fa\u5b9a\u7ed1\u5b9a\u4e3a\u4e00\u4e2aSub\u5bf9\u8c61\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Sub\n{\npublic:\n    int sub(int a, int b)\n    {\n        return a - b;\n    }\n};\nint main()\n{\n    \/\/\u7ed1\u5b9a\u56fa\u5b9a\u53c2\u6570\n    function&lt;int(int, int)&gt; func = bind(&amp;Sub::sub, Sub(), placeholders::_1, placeholders::_2);\n    cout &lt;&lt; func(1, 2) &lt;&lt; endl; \/\/-1\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u8c03\u7528\u7ed1\u5b9a\u540e\u751f\u6210\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6\uff0c\u5c31\u53ea\u9700\u8981\u4f20\u5165\u7528\u4e8e\u76f8\u51cf\u7684\u4e24\u4e2a\u53c2\u6570\u4e86\uff0c\u56e0\u4e3a\u5728\u8c03\u7528\u65f6\u4f1a\u56fa\u5b9a\u5e2e\u6211\u4eec\u4f20\u5165\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\u7ed9this\u6307\u9488\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u60f3\u8981\u5c06sub\u6210\u5458\u51fd\u6570\u7528\u4e8e\u76f8\u51cf\u7684\u4e24\u4e2a\u53c2\u6570\u7684\u987a\u5e8f\u4ea4\u6362\uff0c\u90a3\u4e48\u76f4\u63a5\u5728\u7ed1\u5b9a\u65f6\u5c06placeholders::_1\u548cplaceholders::_2\u7684\u4f4d\u7f6e\u4ea4\u6362\u4e00\u4e0b\u5c31\u884c\u4e86\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Sub\n{\npublic:\n    int sub(int a, int b)\n    {\n        return a - b;\n    }\n};\nint main()\n{\n    \/\/\u8c03\u6574\u4f20\u53c2\u987a\u5e8f\n    function&lt;int(int, int)&gt; func = bind(&amp;Sub::sub, Sub(), placeholders::_2, placeholders::_1);\n    cout &lt;&lt; func(1, 2) &lt;&lt; endl; \/\/1\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u6839\u672c\u539f\u56e0\u5c31\u662f\u56e0\u4e3a\uff0c\u540e\u7eed\u8c03\u7528\u65b0\u751f\u6210\u7684\u53ef\u8c03\u7528\u5bf9\u8c61\u65f6\uff0c\u4f20\u5165\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f1a\u4f20\u7ed9placeholders::_1\uff0c\u4f20\u5165\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f1a\u4f20\u7ed9placeholders::_2\uff0c\u56e0\u6b64\u53ef\u4ee5\u5728\u7ed1\u5b9a\u65f6\u901a\u8fc7\u63a7\u5236placeholders::_n\u7684\u4f4d\u7f6e\uff0c\u6765\u63a7\u5236\u7b2cn\u4e2a\u53c2\u6570\u7684\u4f20\u9012\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">bind\u5305\u88c5\u5668\u7684\u610f\u4e49<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06\u4e00\u4e2a\u51fd\u6570\u7684\u67d0\u4e9b\u53c2\u6570\u7ed1\u5b9a\u4e3a\u56fa\u5b9a\u7684\u503c\uff0c\u8ba9\u6211\u4eec\u5728\u8c03\u7528\u65f6\u53ef\u4ee5\u4e0d\u7528\u4f20\u9012\u67d0\u4e9b\u53c2\u6570\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u5bf9\u51fd\u6570\u53c2\u6570\u7684\u987a\u5e8f\u8fdb\u884c\u7075\u6d3b\u8c03\u6574\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5305\u88c5\u5668 function\u5305\u88c5\u5668 function\u5305\u88c5\u5668\u4ecb\u7ecd function\u5305\u88c5\u5668 function\u662f\u4e00\u79cd\u51fd\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[22],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-cpp","category-learn","tag-c11"],"_links":{"self":[{"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/posts\/98","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":1,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/posts\/98\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergodv.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}