{"id":177,"date":"2025-07-16T18:42:58","date_gmt":"2025-07-16T10:42:58","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=177"},"modified":"2025-11-24T16:41:11","modified_gmt":"2025-11-24T08:41:11","slug":"c11%e6%96%b0%e7%89%b9%e6%80%a72","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=177","title":{"rendered":"c++11\u65b0\u7279\u6027(2)"},"content":{"rendered":"\n<p>\u672c\u6587\u7ee7\u4e0a\u4e00\u7bc7\u6587\u7ae0<a href=\"http:\/\/www.tgwttt.xyz\/?p=169\">http:\/\/www.tgwttt.xyz\/?p=169<\/a>\u3002<\/p>\n\n\n\n<p>\u4e00 .lambda\u8868\u8fbe\u5f0f\uff1alambda \u8868\u8fbe\u5f0f\u672c\u8d28\u662f\u2f00\u4e2a\u533f\u540d\u51fd\u6570\u5bf9\u8c61\uff0c\u8ddf\u666e\u901a\u51fd\u6570\u4e0d\u540c\u7684\u662f\u4ed6\u53ef\u4ee5\u5b9a\u4e49\u5728\u51fd\u6570\u5185\u90e8\u3002lambda \u8868\u8fbe\u5f0f\u8bed\u6cd5\u4f7f\u2f64\u5c42\u2f7d\u2f94\u6ca1\u6709\u7c7b\u578b\uff0c\u6240\u4ee5\u6211\u4eec\u2f00\u822c\u662f\u2f64auto\u6216\u8005\u6a21\u677f\u53c2\u6570\u5b9a\u4e49\u7684\u5bf9\u8c61\u53bb\u63a5\u6536 lambda \u5bf9\u8c61\u3002lambda\u8868\u8fbe\u5f0f\u7684\u683c\u5f0f\uff1a [capture-list] (parameters)-> return type {function boby}                   capture-list : \u6355\u6349\u5217\u8868                                                                                                                                  parameters \uff1a\u53c2\u6570\u5217\u8868                                                                                                                                                    ->return type \uff1a\u8fd4\u56de\u503c\u7c7b\u578b                                                                                                                                function boby \uff1a\u51fd\u6570\u4f53                                                                                                                                                         \u5982\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684lambda\u5f0f\u7684\u4f8b\u5b50\uff1a                                                                                                                       lambda\u8868\u8fbe\u5f0f<br>int main(){<br>auto add1 = [](int x, int y)->int{<br>return x + y;<br>};<br>auto func1 = []<br>{<br>cout << \"hello bit\" << endl;<br>return 0;<br>};<br>func1();<br>}                                                                                                                                                                                       \u6ce8\u610f\uff1a                                                                                                                                                              \/\/ 1\u3001\u6355\u6349\u4e3a\u7a7a\u4e5f\u4e0d\u80fd\u7701\u7565<br>\/\/ 2\u3001\u53c2\u6570\u4e3a\u7a7a\u53ef\u4ee5\u7701\u7565<br>\/\/ 3\u3001\u8fd4\u56de\u503c\u53ef\u4ee5\u7701\u7565\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd4\u56de\u5bf9\u8c61\u2f83\u52a8\u63a8\u5bfc<br>\/\/ 4\u3001\u51fd\u6570\u4f53\u4e0d\u80fd\u7701\u7565                                                                                                                                                 <strong>\u6355\u6349<\/strong>\uff1alambda\u8868\u8fbe\u5f0f\u7684\u6355\u6349\u6709\u5f88\u591a\u79cd\u65b9\u5f0f                                                                                                                   \u4f20\u503c\u6355\u6349\u5982\uff1a[a,b](int x,int y)  \uff0c\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u80fd\u4fee\u6539a,b\u7684\u503c\uff0c\u4f20\u5165\u6240\u6709\u503c[=](int x,int y)                                                                                              \u5f15\u7528\u6355\u6349\u5982\uff1a[&#038;a,&#038;b](int x,int y),\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fee\u6539a,b\u7684\u503c ,\u5f15\u7528\u6355\u6349\u6240\u6709\u503c[&#038;](int x,int y)<\/p>\n\n\n\n<p><strong>\u7c7b\u7684\u65b0\u529f\u80fd  <\/strong>    <\/p>\n\n\n\n<p>1.defult\u548cdelete:                                                                                                                                                      defult:\u6307\u5b9a\u751f\u6210\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0cdelete\uff1a\u7981\u6b62\u4f7f\u7528\u67d0\u9ed8\u8ba4\u6784\u9020\u51fd\u6570                                                                                  <\/p>\n\n\n\n<p>2.final\u548coverride :                                                                                                                                                               final\u4fee\u9970\u7c7b\u4e0d\u80fd\u88ab\u7ee7\u627f<br>override \u653e\u5728\u5b50\u7c7b\u91cd\u5199\u7684\u5730\u65b9\uff0c\u5982\u679c\u6ca1\u6709\u5b8c\u6210\u91cd\u5199\uff0c\u5219\u4f1a\u62a5\u9519<\/p>\n\n\n\n<p>3.\u9ed8\u8ba4\u7684\u79fb\u52a8\u6784\u9020\u548c\u79fb\u52a8\u8d4b\u503c                                                                                                                                                            \u539f\u6765C++\u7c7b\u4e2d\uff0c\u67096\u4e2a\u9ed8\u8ba4\u6210\u5458\u51fd\u6570\uff1a\u6784\u9020\u51fd\u6570\/\u6790\u6784\u51fd\u6570\/\u62f7\u2ec9\u6784\u9020\u51fd\u6570\/\u62f7\u2ec9\u8d4b\u503c\u91cd\u8f7d\/\u53d6\u5730\u5740\u91cd\u8f7d\/const \u53d6\u5730\u5740\u91cd\u8f7d\uff0c\u6700\u540e\u91cd\u8981\u7684\u662f\u524d4\u4e2a\uff0c\u540e\u4e24\u4e2a\u2f64\u5904\u4e0d\u2f24\uff0c\u9ed8\u8ba4\u6210\u5458\u51fd\u6570\u5c31\u662f\u6211\u4eec\u4e0d\u5199\u7f16\u8bd1\u5668\u4f1a\u2f63\u6210\u2f00\u4e2a\u9ed8\u8ba4\u7684\u3002C++11 \u65b0\u589e\u4e86\u4e24\u4e2a\u9ed8\u8ba4\u6210\u5458\u51fd\u6570\uff0c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u7b97\u7b26\u91cd\u8f7d                                                                <\/p>\n\n\n\n<p>\u5305\u88c5\u5668\uff1a<\/p>\n\n\n\n<p><strong>function:<\/strong><\/p>\n\n\n\n<p>std::function \u662f\u2f00\u4e2a\u7c7b\u6a21\u677f\uff0c\u4e5f\u662f\u2f00\u4e2a\u5305\u88c5\u5668\u3002 std::function \u7684\u5b9e\u4f8b\u5bf9\u8c61\u53ef\u4ee5\u5305\u88c5\u5b58\u50a8\u5176\u4ed6\u7684\u53ef\u4ee5\u8c03\u2f64\u5bf9\u8c61\uff0c\u5305\u62ec\u51fd\u6570\u6307\u9488\u3001\u4eff\u51fd\u6570\u3001 lambda \u3001 bind \u8868\u8fbe\u5f0f\u7b49\uff0c\u5b9a\u4e49<functional>\u5934\u2f42\u4ef6\u4e2d\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p>                                  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function<int(int, int)> f1 = f;\/\/\u666e\u901a\u51fd\u6570f(int a,int b);\nfunction<int(int, int)> f2 = Functor();\/\/\u4eff\u51fd\u6570\nfunction<int(int, int)> f3 = [](int a, int b) {return a + b; };lambda\u8868\u8fbe\u5f0f\ncout << f1(1, 1) << endl;\ncout << f2(1, 1) << endl;\ncout << f3(1, 1) << endl;\n\/\/ \u5305\u88c5\u9759\u6001\u6210\u5458\u51fd\u6570\n\/\/ \u6210\u5458\u51fd\u6570\u8981\u6307\u5b9a\u7c7b\u57df\u5e76\u4e14\u524d\u2faf\u52a0&#038;\u624d\u80fd\u83b7\u53d6\u5730\u5740\nfunction<int(int, int)> f4 = &Plus::plusi;\ncout << f4(1, 1) << endl;\n\/\/ \u5305\u88c5\u666e\u901a\u6210\u5458\u51fd\u6570\n\/\/ \u666e\u901a\u6210\u5458\u51fd\u6570\u8fd8\u6709\u2f00\u4e2a\u9690\u542b\u7684this\u6307\u9488\u53c2\u6570\uff0c\u6240\u4ee5\u7ed1\u5b9a\u65f6\u4f20\u5bf9\u8c61\u6216\u8005\u5bf9\u8c61\u7684\u6307\u9488\u8fc7\u53bb\u90fd\u53ef\u4ee5\nfunction<double(Plus*, double, double)> f5 = &Plus::plusd;\nPlus pd;\ncout << f5(&#038;pd, 1.1, 1.1) << endl;\nfunction<double(Plus, double, double)> f6 = &Plus::plusd;\ncout << f6(pd, 1.1, 1.1) << endl;\ncout << f6(pd, 1.1, 1.1) << endl;\nfunction<double(Plus&#038;&#038;, double, double)> f7 = &Plus::plusd;\ncout << f7(move(pd), 1.1, 1.1) << endl;\ncout << f7(Plus(), 1.1, 1.1) << endl;\n                                                                                      <\/code><\/pre>\n\n\n\n<p><strong>bind<\/strong>\uff1abind \u662f\u2f00\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u5b83\u4e5f\u662f\u2f00\u4e2a\u53ef\u8c03\u2f64\u5bf9\u8c61\u7684\u5305\u88c5\u5668\uff0c\u53ef\u4ee5\u628a\u4ed6\u770b\u505a\u2f00\u4e2a\u51fd\u6570\u9002\u914d\u5668\uff0c\u5bf9\u63a5\u6536<br>\u7684fn\u53ef\u8c03\u2f64\u5bf9\u8c61\u8fdb\u2f8f\u5904\u7406\u540e\u8fd4\u56de\u2f00\u4e2a\u53ef\u8c03\u2f64\u5bf9\u8c61\u3002 bind \u53ef\u4ee5\u2f64\u6765\u8c03\u6574\u53c2\u6570\u4e2a\u6570\u548c\u53c2\u6570\u987a\u5e8f\u3002<br>bind \u4e5f\u5728<functional>\u8fd9\u4e2a\u5934\u2f42\u4ef6\u4e2d\u3002 <\/p>\n\n\n\n<p>\u5b83\u7684\u7b2c\u4e00\u4e2a\u4f5c\u7528\u5c31\u662f\u6307\u5b9a\u53c2\u6570\u4f4d\u7f6e\uff1a\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using placeholders::_1;\nusing placeholders::_2;\nusing placeholders::_3;\nauto sub2 = bind(Sub, _2, _1);\ncout << sub2(10, 5) << endl;<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e8c\u4e2a\u6210\u5458\u51fd\u6570\u5bf9\u8c61\u8fdb\u2f8f\u7ed1\u6b7b\uff0c\u5c31\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u4f20\u9012\u4e86\u4e0b\u9762\u662f\u4f8b\u5b50\uff1a<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8ba1\u7b97\u590d\u5229\u7684lambda\nauto func1 = [](double rate, double money, int year)->double {\n\tdouble ret = money;\n\tfor (int i = 0; i < year; i++)\n\t{\n\t\tret += ret * rate;\n\t} return ret - money;\n\t};\n\u7ed1\u6b7b\u2f00\u4e9b\u53c2\u6570\uff0c\u5b9e\u73b0\u51fa\u2f40\u6301\u4e0d\u540c\u5e74\u534e\u5229\u7387\uff0c\u4e0d\u540c\u2fa6\u989d\u548c\u4e0d\u540c\u5e74\u4efd\u8ba1\u7b97\u51fa\u590d\u5229\u7684\u7ed3\u7b97\u5229\u606f\nfunction<double(double)> func3_1_5 = bind(func1, 0.015, _1, 3);\nfunction<double(double)> func5_1_5 = bind(func1, 0.015, _1, 5);\nfunction<double(double)> func10_2_5 = bind(func1, 0.025, _1, 10);\nfunction<double(double)> func20_3_5 = bind(func1, 0.035, _1, 30);\ncout << func3_1_5(1000000) << endl;\ncout << func5_1_5(1000000) << endl;\ncout << func10_2_5(1000000) << endl;\ncout << func20_3_5(1000000) << endl;<\/code><\/pre>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\u4e86,\u5982\u6709\u4e0d\u5bf9\u6b22\u8fce\u8bc4\u8bba\u533a\u6307\u51fa\uff01\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7ee7\u4e0a\u4e00\u7bc7\u6587\u7ae0http:\/\/www.tgwttt.xyz\/?p=169\u3002 \u4e00 .lambda\u8868\u8fbe\u5f0f\uff1alamb&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-177","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=177"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":871,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/177\/revisions\/871"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}