{"id":1004,"date":"2007-10-08T15:27:45","date_gmt":"2007-10-08T07:27:45","guid":{"rendered":"http:\/\/127.0.0.1\/website_linux\/blog\/?p=1004"},"modified":"2007-12-29T20:09:43","modified_gmt":"2007-12-29T12:09:43","slug":"guru-of-the-week-%e6%9d%a1%e6%ac%be02-%e4%b8%b4%e6%97%b6%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/opgogo.com\/blog2\/?p=1004","title":{"rendered":"Guru of the Week \u6761\u6b3e02: \u4e34\u65f6\u5bf9\u8c61"},"content":{"rendered":"<p>GotW #02 Temporary Objects<\/p>\n<p>\u8457\u8005\uff1aHerb Sutter<\/p>\n<p>\u7ffb\u8bd1\uff1akingofark<\/p>\n<p>[\u58f0\u660e]\uff1a\u672c\u6587\u5185\u5bb9\u53d6\u81eawww.gotw.ca\u7f51\u7ad9\u4e0a\u7684Guru of the Week\u680f\u76ee\uff0c\u5176\u8457\u4f5c\u6743\u5f52\u539f\u8457\u8005\u672c\u4eba\u6240\u6709\u3002\u8bd1\u8005kingofark\u5728\u672a\u7ecf\u539f\u8457\u8005\u672c\u4eba\u540c\u610f\u7684\u60c5\u51b5\u4e0b\u7ffb\u8bd1\u672c\u6587\u3002\u672c\u7ffb\u8bd1\u5185\u5bb9\u4ec5\u4f9b\u81ea\u5b66\u548c\u53c2\u8003\u7528\uff0c\u8bf7\u6240\u6709\u9605\u8bfb\u8fc7\u672c\u6587\u7684\u4eba\u4e0d\u8981\u64c5\u81ea\u8f6c\u8f7d\u3001\u4f20\u64ad\u672c\u7ffb\u8bd1\u5185\u5bb9\uff1b\u4e0b\u8f7d\u672c\u7ffb\u8bd1\u5185\u5bb9\u7684\u4eba\u8bf7\u5728\u9605\u8bfb\u6d4f\u89c8\u540e\uff0c\u7acb\u5373\u5220\u9664\u5176\u5907\u4efd\u3002\u8bd1\u8005kingofark\u5bf9\u8fdd\u53cd\u4e0a\u8ff0\u4e24\u6761\u539f\u5219\u7684\u4eba\u4e0d\u8d1f\u4efb\u4f55\u8d23\u4efb\u3002\u7279\u6b64\u58f0\u660e\u3002<\/p>\n<p>Revision 1.0<\/p>\n<p>Guru of the Week \u6761\u6b3e02: \u4e34\u65f6\u5bf9\u8c61<\/p>\n<p>\u96be\u5ea6\uff1a5 \/ 10<\/p>\n<p>\uff08\u628a\u4f60\u7684\u5fc3\u8840\u4e4b\u4f5c\uff08\u5305\u62ec\u4f60\u7684\u7a0b\u5e8f\u4e4b\u6027\u80fd\u5728\u5185\uff09\u5f53\u6210\u5783\u573e\u629b\u51fa\u7a97\u5916\u7684\u7f6a\u4eba\uff0c\u5f80\u5f80\u662f\u4e00\u4e9b\u610f\u60f3\u4e0d\u5230\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\uff09<\/p>\n<p>[\u95ee\u9898]<\/p>\n<p>\u8bd5\u60f3\u4f60\u6b63\u5728\u9605\u8bfb\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u5458\u5199\u597d\u7684\u51fd\u6570\u4ee3\u7801\uff08\u5982\u4e0b\uff09\uff0c\u800c\u8fd9\u4e2a\u51fd\u6570\u4e2d\u5374\u5728\u81f3\u5c11\u4e09\u4e2a\u5730\u65b9\u7528\u5230\u4e86\u4e0d\u5fc5\u8981\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\u90a3\u4e48\uff0c\u4f60\u80fd\u53d1\u73b0\u5176\u4e2d\u7684\u51e0\u4e2a\u5462\uff1f\u7a0b\u5e8f\u5458\u53c8\u8be5\u5982\u4f55\u4fee\u6539\u4ee3\u7801\u5462\uff1f<\/p>\n<p>string FindAddr( list&lt;Employee&gt; l, string name ){for( list&lt;Employee&gt;::iterator i = l.begin();i != l.end();i++ ){if( *i == name ){return (*i).addr;}}return &#8220;&#8221;;}<\/p>\n<p>[\u89e3\u7b54]<\/p>\n<p>\u4fe1\u4e0d\u4fe1\u7531\u4f60\uff0c\u8fd9\u77ed\u77ed\u7684\u51e0\u884c\u4ee3\u7801\u4e2d\u5c31\u8d77\u7801\u6709\u4e09\u4e2a\u5730\u65b9\u660e\u663e\u7684\u4f7f\u7528\u4e86\u4e0d\u5fc5\u8981\u7684\u4e34\u65f6\u5bf9\u8c61\uff0c\u5176\u4e2d\u6709\u4e24\u4e2a\u6bd4\u8f83\u5fae\u5999\uff0c\u7b2c\u4e09\u4e2a\u5219\u662f\u4e00\u8ba1\u906e\u773c\u6cd5\uff08red herring\uff09\u3002<\/p>\n<p>*string FindAddr( list&lt;Employee&gt; l, string name )&#8212;-\u7b2c1\u5904&#8212;&#8212;&#8211;\u7b2c2\u5904&#8212;-<\/p>\n<p>1\u548c2\uff1a\u4e24\u4e2a\u53c2\u6570\u90fd\u5e94\u8be5\u4f7f\u7528\u5e38\u91cf\u5f15\u7528\uff08const reference\uff09\u3002\u4f7f\u7528\u4f20\u503c\uff08pass-by-value\uff09\u65b9\u5f0f\u5c06\u4f1a\u5bfc\u81f4\u51fd\u6570\u5bf9list\u548cstring\u8fdb\u884c\u62f7\u8d1d\uff0c\u5176\u6027\u80fd\u4ee3\u4ef7\u662f\u9ad8\u6602\u7684\u3002<\/p>\n<p>[\u89c4\u5219]\uff1a\u8bf7\u4f7f\u7528const&amp;\u800c\u4e0d\u662f\u4f20\u503c\u62f7\u8d1d\u3002<\/p>\n<p>*for( list&lt;Employee&gt;::iterator i = l.begin();i != l.end();i++ )<\/p>\n<p>-\u7b2c3\u5904-<\/p>\n<p>3\uff1a\u8fd9\u4e00\u5904\u771f\u662f\u66f4\u4e3a\u5fae\u5999\u3002\u5148\u589e\uff08preincrement\uff09\u64cd\u4f5c\u6bd4\u540e\u589e\uff08postincrement\uff09\u64cd\u4f5c\u6548\u7387\u66f4\u9ad8\uff0c\u8fd9\u662f\u56e0\u4e3a\u5728\u8fdb\u884c\u540e\u589e\uff08postincrement\uff09\u64cd\u4f5c\u65f6\uff0c\u5bf9\u8c61\u4e0d\u4f46\u5fc5\u987b\u81ea\u5df1\u9012\u589e\uff0c\u800c\u4e14\u8fd8\u8981\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u9012\u589e\u524d\u4e4b\u503c\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\u8981\u77e5\u9053\uff0c\u5c31\u8fdeint\u8fd9\u6837\u7684\u5185\u5efa\u7c7b\u578b\u4e5f\u662f\u5982\u6b64\uff01<\/p>\n<p>[\u5b66\u4e60\u6307\u5bfc]\uff1a\u8bf7\u4f7f\u7528\u5148\u589e\uff08preincrement\uff09\u64cd\u4f5c\uff0c\u907f\u514d\u4f7f\u7528\u540e\u589e\uff08postincrement\uff09\u64cd\u4f5c\u3002<\/p>\n<p>*if( *i == name )&#8211; \u7b2c4\u5904 &#8212;<\/p>\n<p>4\uff1a\u8fd9\u91cc\u6ca1\u6709\u4f53\u73b0Employee\u7c7b\uff0c\u4f46\u5982\u679c\u60f3\u8ba9\u5b83\u884c\u5f97\u901a\uff0c\u5219\u8981\u4e48\u6765\u4e00\u4e2a\u8f6c\u6362\u6210string\u7684\u64cd\u4f5c\uff0c\u8981\u4e48\u901a\u8fc7\u4e00\u4e2a\u8f6c\u6362\u6784\u9020\u51fd\u6570\uff08constructor\uff09\u6765\u5f97\u5230\u4e00\u4e2astring\u3002\u7136\u800c\u4e24\u79cd\u65b9\u6cd5\u90fd\u4f1a\u4ea7\u751f\u4e34\u65f6\u5bf9\u8c61\uff0c\u4ece\u800c\u5bfc\u81f4\u5bf9string\u6216\u8005Employee\u7684operator=\u4e4b\u8c03\u7528\u3002\uff08\uff09<\/p>\n<p>[\u5b66\u4e60\u6307\u5bfc]\uff1a\u65f6\u523b\u6ce8\u610f\u56e0\u4e3a\u53c2\u6570\u8f6c\u6362\u64cd\u4f5c\u800c\u4ea7\u751f\u7684\u9690\u85cf\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\u4e00\u4e2a\u907f\u514d\u5b83\u7684\u597d\u529e\u6cd5\u5c31\u662f\u5c3d\u53ef\u80fd\u663e\u5f0f\uff08explicit\uff09\u7684\u4f7f\u7528\u6784\u9020\u51fd\u6570\uff08constructor\uff09\u3002<\/p>\n<p>*return &#8220;&#8221;;<\/p>\n<p>&#8211;\u7b2c5\u5904&#8211;<\/p>\n<p>5\uff1a\u8fd9\u91cc\u4ea7\u751f\u4e86\u4e00\u4e2a\u4e34\u65f6\u7684\uff08\u7a7a\u7684\uff09string\u5bf9\u8c61\u3002\u66f4\u597d\u7684\u505a\u6cd5\u662f\uff0c\u58f0\u660e\u4e00\u4e2a\u5c40\u90e8string\u5bf9\u8c61\u6765\u50a8\u5b58\u8fd4\u56de\u503c\uff0c\u7136\u540e\u7528\u5355\u72ec\u4e00\u4e2areturn\u8bed\u53e5\u8fd4\u56de\u8fd9\u4e2astring\u3002\u8fd9\u4f7f\u5f97\u7f16\u8bd1\u5668\u53ef\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff08\u6bd4\u5982\uff0c\u5f62\u5982\u201cstring a = FindAddr(l, \u201cHarold\u201d);\u201d\u7684\u4ee3\u7801\uff09\u542f\u7528\u201c\u8fd4\u56de\u503c\u4f18\u5316\u201d\u5904\u7406\u6765\u7701\u7565\u6389\u5c40\u90e8\u5bf9\u8c61\u3002<\/p>\n<p>[\u89c4\u5219]\uff1a\u8bf7\u9075\u5faa\u6240\u8c13\u7684\u201c\u5355\u5165\u53e3\/\u5355\u51fa\u53e3\u201d\uff08single-entry\/single-exit\uff09\u89c4\u5219\u3002\u7edd\u4e0d\u8981\u5728\u4e00\u4e2a\u51fd\u6570\u91cc\u9762\u5199\u6709\u591a\u4e2areturn\u8bed\u53e5\u3002<\/p>\n<p>[\u4f5c\u8005\u8bb0\uff1a\u5f53\u8fdb\u884c\u4e86\u8fdb\u4e00\u6b65\u7684\u6027\u80fd\u6d4b\u8bd5\u4e4b\u540e\uff0c\u6211\u4e0d\u518d\u8ba4\u540c\u4e0a\u9762\u7684\u90a3\u6761\u5efa\u8bae\u3002\u6211\u5df2\u7ecf\u5728\u300aExceptional C++\u300b\u4e2d\u4fee\u6539\u4e86\u8fd9\u4e00\u70b9\u3002]<\/p>\n<p>*string FindAddr( list&lt;Employee&gt; l, string name )<\/p>\n<p>&#8211;\u7b2c*\u5904&#8211;<\/p>\n<p>*\uff1a\u8fd9\u53ef\u662f\u4e00\u8ba1\u906e\u773c\u6cd5\uff08red herring\uff09\u3002\u770b\u4e0a\u53bb\uff0c\u597d\u50cf\u4f60\u53ef\u4ee5\u5f88\u7b80\u5355\u7684\u901a\u8fc7\u628a\u8fd4\u56de\u7c7b\u578b\u58f0\u660e\u4e3astring&amp;\u800c\u4e0d\u662fstring\uff0c\u6765\u907f\u514d\u5728\u6240\u6709\u53ef\u80fd\u7684\u8fd4\u56de\u95ee\u9898\u4e2d\u4ea7\u751f\u4e34\u65f6\u5bf9\u8c61\u3002\u5bf9\u5417\uff1f\u9519\u4e86\uff01\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u53ea\u662f\u5728\u4ee3\u7801\u8bd5\u56fe\u4f7f\u7528\u5f15\u7528\uff08reference\uff09\u7684\u65f6\u5019\u5c31\u5d29\u6e83\uff08\u56e0\u4e3a\u90a3\u4e2a\u6240\u6307\u5411\u7684\u5c40\u90e8\u5bf9\u8c61\u65e9\u5df2\u4e0d\u5b58\u5728\uff09\uff0c\u90a3\u5c31\u7b97\u4f60\u591f\u8d70\u8fd0\u7684\u4e86\uff01\u5982\u679c\u4f60\u4e0d\u8d70\u8fd0\u7684\u8bdd\uff0c\u4f60\u7684\u4ee3\u7801\u5c06\u770b\u4e0a\u53bb\u4f3c\u4e4e\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\uff0c\u5374\u65f6\u4e0d\u65f6\u7684\u51b7\u4e0d\u9632\u5931\u8d25\u51e0\u6b21\uff0c\u4ece\u800c\u4f7f\u4f60\u4e0d\u5f97\u4e0d\u5728\u8c03\u8bd5\u7a0b\u5e8f\u7684\u8fc7\u7a0b\u4e2d\u6e21\u8fc7\u4e00\u4e2a\u53c8\u4e00\u4e2a\u6f2b\u6f2b\u957f\u591c\u3002<\/p>\n<p>[\u89c4\u5219]\uff1a\u7edd\u5bf9\u7edd\u5bf9\uff08\uff01\uff09\u4e0d\u8981\u8fd4\u56de\u5bf9\u5c40\u90e8\u5bf9\u8c61\u7684\u5f15\u7528\uff08reference\uff09\u3002<\/p>\n<p>[\u4f5c\u8005\u8bb0\uff1a\u6709\u4e00\u4e9b\u8d34\u5b50\u6b63\u786e\u7684\u6307\u51fa\uff0c\u4f60\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u9047\u5230\u9519\u8bef\u65f6\u624d\u8fd4\u56de\u7684\u9759\u6001\u5bf9\u8c61\uff0c\u4ece\u800c\u5b9e\u73b0\u5728\u4e0d\u6539\u53d8\u51fd\u6570\u8bed\u4e49\u7684\u60c5\u51b5\u4e0b\u8fd4\u56de\u4e00\u4e2a\u5f15\u7528\uff08reference\uff09\u3002\u540c\u65f6\u8fd9\u4e5f\u610f\u5473\u7740\uff0c\u5728\u8fd4\u56de\u5f15\u7528\uff08reference\uff09\u7684\u65f6\u5019\uff0c\u4f60\u5fc5\u987b\u6ce8\u610f\u5bf9\u8c61\u7684\u751f\u5b58\u5468\u671f\u3002]<\/p>\n<p>\u5176\u5b9e\u8fd8\u6709\u5f88\u591a\u53ef\u4ee5\u4f18\u5316\u7684\u5730\u65b9\uff0c\u8bf8\u5982\u201c\u907f\u514d\u5bf9end()\u8fdb\u884c\u591a\u4f59\u7684\u8c03\u7528\u201d\u7b49\u7b49\u3002\u7a0b\u5e8f\u5458\u53ef\u4ee5\uff08\u4e5f\u5e94\u8be5\uff09\u4f7f\u7528\u4e00\u4e2aconst_iterator\u3002\u629b\u5f00\u8fd9\u4e9b\u4e0d\u8c08\uff0c\u6211\u4eec\u4ecd\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u7684\u6b63\u786e\u4ee3\u7801\uff1a<\/p>\n<p>string FindAddr( const list&lt;Employee&gt;&amp; l, const string&amp; name ){string addr;for( list&lt;Employee&gt;::const_iterator i = l.begin();i != l.end();++i ){if( (*i).name == name ){addr = (*i).addr;break;}}return addr;}<\/p>\n<p>[kingofark\u6ce8\uff1a<\/p>\n<p>red herring: Something that draws attention away from the central issue.<\/p>\n<p>a red herring: \u906e\u773c\u6cd5; \u8f6c\u79fb\u6ce8\u610f\u529b\u7684\u4e1c\u897f]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GotW #02 Temporary Objects \u8457\u8005\uff1aHerb Sutter \u7ffb\u8bd1\uff1akingofark [\u58f0\u660e]\uff1a\u672c\u6587\u5185\u5bb9\u53d6\u81eawww.gotw.ca\u7f51\u7ad9\u4e0a\u7684Guru of the Week\u680f\u76ee\uff0c&hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[28],"tags":[],"class_list":["post-1004","post","type-post","status-publish","format-standard","hentry","category-computer"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts\/1004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1004"}],"version-history":[{"count":0,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=\/wp\/v2\/posts\/1004\/revisions"}],"wp:attachment":[{"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opgogo.com\/blog2\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}