{"id":179,"date":"2021-12-16T01:53:08","date_gmt":"2021-12-15T17:53:08","guid":{"rendered":"http:\/\/139.224.63.49\/?p=179"},"modified":"2022-02-17T17:13:15","modified_gmt":"2022-02-17T09:13:15","slug":"c-stl%e4%b8%ad%e7%9a%84%e5%9d%91","status":"publish","type":"post","link":"http:\/\/iamnear.top\/?p=179","title":{"rendered":"c++ stl\u4e2d\u7684\u5751"},"content":{"rendered":"\n<p>\u4e3b\u8981\u6458\u81eahttps:\/\/www.zhihu.com\/question\/355171938<\/p>\n\n\n\n<p>no.1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std::string GetString()\n{\n    std::string str = \"hello world\";\n    return std::move(str);    \/\/ bad\n    return str;    \/\/ ok\n}<\/code><\/pre>\n\n\n\n<p>\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u5728compiler explorer\u4e2d\u505a\u4e86\u6d4b\u8bd5\uff08<a href=\"https:\/\/gcc.godbolt.org\/\">https:\/\/gcc.godbolt.org\/<\/a>\uff09\uff0c\u5728\u6ca1\u6709\u4f18\u5316\u7684\u7248\u672c\uff0cgcc\u7684\u6c47\u7f16\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u5230\uff0cbad\u7684\u90a3\u79cd\u60c5\u51b5\uff0c\u591a\u505a\u4e86\u4e00\u6b21\u79fb\u52a8\u6784\u9020\uff0c\u800cok\u7684\u90a3\u79cd\u60c5\u51b5\u5219\u6ca1\u6709\u3002<\/p>\n\n\n\n<p>\u8fd9\u6b21\u770b\u6c47\u7f16\u4ee3\u7801\u4e5f\u8ba9\u6211\u66f4\u52a0\u719f\u6089\u4e86\u4e00\u4e22\u4e2264\u4f4dx64\u7684c++\u6c47\u7f16\uff0c\u4e0d\u518d\u662f32\u4f4d\u4e2decx\u4f5c\u4e3athis\u6307\u9488\uff0c64\u4f4d\u4e2d\u51fd\u6570\u7684\u4f20\u53c2\u662f\u5f53\u53c2\u6570\u5c11\u4e8e7\u4e2a\u65f6\uff0c \u53c2\u6570\u4ece\u5de6\u5230\u53f3\u653e\u5165\u5bc4\u5b58\u5668: rdi, rsi, rdx, rcx, r8, r9<\/p>\n\n\n\n<p>\u53c2\u6570\u4e3a7\u4e2a\u4ee5\u4e0a\u65f6\uff0c \u524d 6 \u4e2a\u4e0e\u524d\u9762\u4e00\u6837\uff0c \u4f46\u540e\u9762\u7684\u4f9d\u6b21\u4ece \u201c\u53f3\u5411\u5de6\u201d \u653e\u5165\u6808\u4e2d\uff0c\u5373\u548c32\u4f4d\u6c47\u7f16\u4e00\u6837\u3002<\/p>\n\n\n\n<p>\u53c2\u8003<a href=\"http:\/\/abcdxyzk.github.io\/blog\/2012\/11\/23\/assembly-args\/\">http:\/\/abcdxyzk.github.io\/blog\/2012\/11\/23\/assembly-args\/<\/a><\/p>\n\n\n\n<p>\u6211\u8fd8\u5728https:\/\/quick-bench.com\u4e2d\u5bf9\u4e24\u79cd\u4e0d\u540c\u7684\u8fd4\u56de\u65b9\u5f0f\u7684\u901f\u5ea6\u8fdb\u884c\u4e86\u6d4b\u8bd5<\/p>\n\n\n\n<p>\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;string&gt;\n#include &lt;iostream&gt;\n\/\/ Type your code here, or load an example.\nstd::string GetString1()\n{\n    std::string str = \"hello world\";\n    return std::move(str);    \/\/ bad\n}\nstd::string GetString2()\n{\n    std::string str = \"hello world\";\n    return str;\n}\nvoid func1(benchmark::State&amp; state)\n{\n    for (auto _ : state) {\n        std::string s = GetString1();\n        benchmark::DoNotOptimize(s);\n    }\n}\nvoid func2(benchmark::State&amp; state)\n{\n    for (auto _ : state) {\n        std::string s = GetString2();\n        benchmark::DoNotOptimize(s);\n    }\n}\nBENCHMARK(func1);\nBENCHMARK(func2);<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"922\" height=\"658\" data-id=\"183\" src=\"http:\/\/139.224.63.49\/wp-content\/uploads\/2021\/12\/\u6355\u83b74.png\" alt=\"\" class=\"wp-image-183\" srcset=\"http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b74.png 922w, http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b74-300x214.png 300w, http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b74-768x548.png 768w\" sizes=\"auto, (max-width: 922px) 100vw, 922px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"923\" height=\"679\" data-id=\"182\" src=\"http:\/\/139.224.63.49\/wp-content\/uploads\/2021\/12\/\u6355\u83b73.png\" alt=\"\" class=\"wp-image-182\" srcset=\"http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b73.png 923w, http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b73-300x221.png 300w, http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b73-768x565.png 768w\" sizes=\"auto, (max-width: 923px) 100vw, 923px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"931\" height=\"680\" data-id=\"181\" src=\"http:\/\/139.224.63.49\/wp-content\/uploads\/2021\/12\/\u6355\u83b72.png\" alt=\"\" class=\"wp-image-181\" srcset=\"http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b72.png 931w, http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b72-300x219.png 300w, http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b72-768x561.png 768w\" sizes=\"auto, (max-width: 931px) 100vw, 931px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"926\" height=\"662\" data-id=\"180\" src=\"http:\/\/139.224.63.49\/wp-content\/uploads\/2021\/12\/\u6355\u83b7.png\" alt=\"\" class=\"wp-image-180\" srcset=\"http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b7.png 926w, http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b7-300x214.png 300w, http:\/\/iamnear.top\/wp-content\/uploads\/2021\/12\/\u6355\u83b7-768x549.png 768w\" sizes=\"auto, (max-width: 926px) 100vw, 926px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5373\u4f7f\u7528\u4e86O3\u6216\u8005OFast\uff0cgcc\u7f16\u8bd1\u7684\u7a0b\u5e8f\u4e2d\uff0c\u8fd8\u662ffunc1\u8fd8\u662f\u4f1a\u663e\u8457\u6162\u4e8efunc2,\u5728clang\u4e2d\u5219\u4f3c\u4e4e\u88ab\u4f18\u5316\u6210\u4e00\u6837\u7684\u4e86\u3002<\/p>\n\n\n\n<p>no.2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;numeric&gt;\nusing namespace std;\nint main()\n{\n    vector&lt;float&gt; v{0.2,0.3,0.4};\n    double sum = accumulate(begin(v),end(v),0);\n    cout&lt;&lt;\"sum = \"&lt;&lt;sum&lt;&lt;endl;\/\/output: sum = 0\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u95ee\u9898\u662f\u7531\u4e8eaccumulate\u7684\u7c7b\u578b\u53c2\u6570\u5bfc\u81f4\u7684\uff0c\u89c1https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/accumulate<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"common-mistakes\">Common mistakes<\/h4>\n\n\n\n<p>If left to type inference, <code>op<\/code> operates on values of the same type as <code>init<\/code> which can result in unwanted casting of the iterator elements. For example, std::accumulate(v.begin(), v.end(), 0) likely does not give the result one wishes for when <code>v<\/code> is <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/container\/vector\">std::vector<\/a>&lt;double&gt;.<\/p>\n\n\n\n<p>\u6211\u81ea\u5df1\u518d\u8865\u5145\u4e2a\u5751\uff1a<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"std-lower-bound\">std::lower_bound<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>default (1)<\/th><td>template &lt;class ForwardIterator, class T&gt; ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T&amp; val);<\/td><\/tr><tr><th>custom (2)<\/th><td>template &lt;class ForwardIterator, class T, class Compare&gt; ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T&amp; val, Compare comp);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5bf9comp\u53c2\u6570\u7684\u8981\u6c42\u4e3a\uff1a<\/p>\n\n\n\n<p>comp Binary function that accepts two arguments (the first of the type pointed by <samp>ForwardIterator<\/samp>, and the second, always <var>val<\/var>), and returns a value convertible to <code>bool<\/code>. The value returned indicates whether the first argument is considered to go before the second.<br>The function shall not modify any of its arguments.<br>This can either be a function pointer or a function object.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"std-upper-bound\">std::upper_bound<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>default (1)<\/th><td>template &lt;class ForwardIterator, class T&gt; ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T&amp; val);<\/td><\/tr><tr><th>custom (2)<\/th><td>template &lt;class ForwardIterator, class T, class Compare&gt; ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T&amp; val, Compare comp);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5bf9comp\u51fd\u6570\u7684\u8981\u6c42\u4e3a\uff1a<\/p>\n\n\n\n<p>Binary function that accepts two arguments (the first is always <var>val<\/var>, and the second of the type pointed by <samp>ForwardIterator<\/samp>), and returns a value convertible to <code>bool<\/code>. The value returned indicates whether the first argument is considered to go before the second.<br>The function shall not modify any of its arguments.<br>This can either be a function pointer or a function object.<\/p>\n\n\n\n<p>\u867d\u7136lower_bound\u548cupper_bound\u51fd\u6570\u7684\u7b7e\u540d\u7279\u522b\u76f8\u4f3c\uff0c\u4f46\u662fcomp\u51fd\u6570\u662f\u4e0d\u540c\u7684\uff01\uff01lower_bound\u8981\u6c42\u7684comp\u662fcmp(*iterator,val)->bool \u800cupper_bound\u8981\u6c42\u7684comp\u662fcmp(val,*iterator)->bool \u6240\u4ee5\u8fd9\u4e24\u4e2acmp\u7684\u4e24\u4e2a\u53c2\u6570\u987a\u5e8f\u662f\u76f8\u53cd\u7684\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u8981\u6458\u81eahttps:\/\/www.zhihu.com\/question\/355171938 no.1 &hellip; <a href=\"http:\/\/iamnear.top\/?p=179\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">c++ stl\u4e2d\u7684\u5751<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":{"0":"post-179","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-uncategorized","7":"h-entry","9":"h-as-article"},"_links":{"self":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=179"}],"version-history":[{"count":3,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":205,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions\/205"}],"wp:attachment":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}