{"id":376,"date":"2020-04-02T11:18:57","date_gmt":"2020-04-02T03:18:57","guid":{"rendered":"https:\/\/www.notown.club\/?p=376"},"modified":"2023-05-28T17:35:13","modified_gmt":"2023-05-28T09:35:13","slug":"c%e5%87%bd%e6%95%b0%ef%bc%88%e7%ac%94%e8%ae%b0%e4%b8%89%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=376","title":{"rendered":"C++\u51fd\u6570\uff08\u7b14\u8bb0\u4e09\uff09"},"content":{"rendered":"\n<p class=\"has-medium-font-size\"><strong>\u51fd\u6570\u6307\u9488<\/strong><\/p>\n\n\n\n<p>\u4e00\u4e2a\u6307\u5411\u4e86\u51fd\u6570\u7684\u6307\u9488<\/p>\n\n\n\n<p>\u5b9a\u4e49\u51fd\u6570\u6307\u9488\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>bool lengthCompare(const string &amp;, const string &amp;){};\/\/\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<br>bool (*pf) (const string &amp;, const string &amp;) = lengthCompare\/\/pf\u4e3a\u6307\u5411lengthCompare\u7684\u51fd\u6570\u6307\u9488\u3002\uff08\u62ec\u53f7\u4e0d\u80fd\u7701\u7565\uff01\uff01\uff09\u7136\u800c\uff0c\u8fd9\u4e00\u53e5\u4ee3\u7801\u4e2d\uff0clengthCompare\u524d\u9762\u8fd8\u53ef\u4ee5\u8ddf\u4e2a\u53d6\u5730\u5740\u7b26\uff0c\u8fd9\u662f\u53ef\u9009\u7684\uff0c\u5e76\u4e0d\u5f71\u54cd\u7ed3\u679c\u3002<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u51fd\u6570\u6307\u9488\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>bool var1= pf(\u201chello\u201d,\u201dgoodbye\u201d);\/\/\u6b63\u786e<br>bool var2=(*pf)( \u201chello\u201d,\u201dgoodbye\u201d)\/\/\u7b49\u4ef7\u4e8e\u65e0\u89e3\u5f15\u7528<br>bool var3= lengthCompare ( \u201chello\u201d,\u201dgoodbye\u201d)\/\/\u4e0a\u9762\u4e24\u884c\u90fd\u662f\u76f8\u5f53\u4e8e\u8fd9\u884c\u7684\u3002<\/code><\/pre>\n\n\n\n<p>\u7136\u800c\uff0c\u51fd\u6570\u6307\u9488\u4e0d\u5b58\u5728\u8f6c\u6362\u89c4\u5219\uff0c\u6362\u800c\u8a00\u4e4b\uff1a<br><code>bool cstringCompare(const char*, const char *);<br>pf = cstringCompare; \u8fd9\u662f\u9519\u8bef\u7684\u3002<\/code><br>\u8fd8\u6709\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>void ff(int *);\nvoid ff(unsigned int);\nvoid (*p1)(unsigned int)=ff;\/\/\u6b63\u786e \nvoid (*p2)(int) = ff\/\/\u9519\u8bef\uff0c\u6ca1\u6709\u4efb\u4f55\u4e00\u4e2aff\u80fd\u5339\u914d\u7684\u4e0a<\/code><\/pre>\n\n\n\n<p>\u5c31\u662f\u51fd\u6570\u6307\u9488\u624d\u4e0d\u4f1a\u53bb\u5bfb\u627e\u5339\u914d\u51fd\u6570\u7684\u6700\u4f18\u89e3\uff0c\u6709\u5c31\u6709\uff0c\u6ca1\u6709\u62c9\u5012\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u51fd\u6570\u6307\u9488\u5f62\u53c2<\/strong><\/p>\n\n\n\n<p>\u548c\u6570\u7ec4\u7c7b\u4f3c\uff0c\u4e00\u4e2a\u51fd\u6570\u6216\u8bb8\u4e0d\u80fd\u76f4\u63a5\u6210\u4e3a\uff08\u53e6\u4e00\u4e2a\uff09\u51fd\u6570\u7684\u53c2\u6570\uff0c\u4f46\u662f\u51fd\u6570\u662f\u80fd\u591f\u628a\u51fd\u6570\u7684\u6307\u9488\u5f53\u4f5c\u5f62\u53c2\u7684\u3002<br>\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>void useBigger(const string &amp;s1, const string &amp;s2, bool pf(const string &amp;, const string &amp;));<br>\/\/\u7b2c\u4e09\u4e2a\u5f62\u53c2\u662f\u51fd\u6570\u7c7b\u578b\uff0c\u5b83\u4f1a\u81ea\u52a8\u5730\u8f6c\u6362\u6210\u6307\u5411\u8be5\u51fd\u6570\u7684\u6307\u9488<br>void useBigger(const string &amp;s1, const string &amp;s2, bool (*pf)(const string &amp;, const string &amp;));<br>\/\/\u8fd9\u8ddf\u4e0a\u9762\u7684\u662f\u7b49\u4ef7\u7684\u3002<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u65f6\u53ef\u4ee5\u76f4\u63a5\u4f20\u4e00\u4e2a\u51fd\u6570\u8fdb\u6765\uff0c\u4f5c\u4e3a\u5b9e\u53c2\uff0c\u5b83\u4f1a\u81ea\u52a8\u8f6c\u6210\u6307\u9488<br><code>useBigger(s1, s2, lengthCompare);<\/code><br>(\u7528decltype, typedef\u80fd\u51cf\u5c11\u4e00\u4e9b\u91cd\u590d\u5199\u957f\u8bed\u53e5\u7684\u9ebb\u70e6)<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u8fd4\u56de\u6307\u5411\u51fd\u6570\u7684\u6307\u9488<\/strong><\/p>\n\n\n\n<p>\u548c\u6570\u7ec4\u7c7b\u4f3c\uff0c\u867d\u7136\u4e0d\u80fd\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\uff0c\u4f46\u662f\u80fd\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u51fd\u6570\u7684\u6307\u9488. \u7528\u5b83\u7684\u6700\u597d\u65b9\u6cd5\u5c31\u662f\u4f7f\u7528\u7c7b\u578b\u522b\u540d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>using F =int (int <em>, int); <\/em>\nusing PF =int (*) (int *, int);<\/code>\n\n<code>PF f1(int)\/\/\u6b63\u786e\uff0cPF\u662f\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\uff0cf1\u8fd4\u56de\u6307\u5411\u51fd\u6570\u7684\u6307\u9488\nF f2(int) \/\/\u9519\u8bef\uff0cF\u662f\u51fd\u6570\u7c7b\u578b\uff0cf2\u4e0d\u80fd\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\nF *f3(int) \/\/ok<\/code><\/pre>\n\n\n\n<p>\u5f53\u7136\u4e5f\u53ef\u4ee5\u7279\u522b\u6076\u5fc3\u7684\u8fd9\u6837\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<p><code>int (*f4(int)) (int ,int);<\/code><\/p>\n\n\n\n<p>\u6309\u7167\u7531\u5185\u800c\u5916\u7684\u987a\u5e8f\u7406\u89e3\uff0c \u56e0\u4e3af4\u6709\u5f62\u53c2\u5217\u8868\uff0c\u6240\u4ee5f4\u662f\u4e2a\u51fd\u6570\uff0cf4\u524d\u9762\u6709\u4e2a*\uff0c\u6240\u4ee5f4\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6307\u9488\uff0c\u800c\u56e0\u4e3a\u6307\u9488\u7c7b\u578b\u672c\u8eab\u4e5f\u6709\u5f62\u53c2\u5217\u8868\uff0c\u56e0\u6b64\u5c31\u662f\uff0c\u8fd4\u56de\u4e00\u4e2a\u6307\u9488\uff0c\u8be5\u6307\u9488\u6307\u5411\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6709\u4e24\u4e2a\u5b9e\u53c2\u4e14\u8fd4\u56de\u4e00\u4e2aint\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5c3e\u7f6e\u8fd4\u56de\u7c7b\u578b\u7684\u65b9\u5f0f<br> <code>auto f5(int) -&gt; int(*)(int *, int);<\/code><\/p>\n\n\n\n<p>\u56e0\u4e3a\u6709\u4e86\u51fd\u6570\u6307\u9488\u8fd9\u79cd\u9a9a\u4e1c\u897f\uff0c\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u7c7b\u4f3c\u4e8e\u5c06\u51fd\u6570\uff08\u6307\u9488\uff09\u653e\u8fdb\u4e00\u4e2avector\u91cc\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\u6570\u7ec4\u7684\u7136\u540e\u8fdb\u884c\u5404\u79cd\u9a9a\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u6307\u9488 \u4e00\u4e2a\u6307\u5411\u4e86\u51fd\u6570\u7684\u6307\u9488 \u5b9a\u4e49\u51fd\u6570\u6307\u9488\uff1a bool lengthCompare(const string&hellip;<a href=\"https:\/\/www.notown.top\/?p=376\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++\u51fd\u6570\uff08\u7b14\u8bb0\u4e09\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[11],"tags":[],"class_list":["post-376","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=376"}],"version-history":[{"count":6,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions"}],"predecessor-version":[{"id":633,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/376\/revisions\/633"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}