{"id":51,"date":"2021-05-25T12:35:43","date_gmt":"2021-05-25T04:35:43","guid":{"rendered":"http:\/\/139.224.63.49\/?p=51"},"modified":"2021-05-25T12:35:43","modified_gmt":"2021-05-25T04:35:43","slug":"c-stl%e4%b8%admap%e7%9a%84emplace%e6%8f%92%e5%85%a5","status":"publish","type":"post","link":"http:\/\/iamnear.top\/?p=51","title":{"rendered":"c++ stl\u4e2dmap\u7684emplace\u63d2\u5165"},"content":{"rendered":"\n<p>map\u5728c++11\u4e2d\u589e\u52a0\u4e86emplace\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u6211\u867d\u7136\u4e4b\u524d\u6ca1\u7528\u8fc7\uff0c\u4f46\u662f\u8fd8\u662f\u660e\u767d\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u6bd5\u7adfvector\u7684emplace_back\u6211\u8fd8\u662f\u7528\u8fc7\u7684\u3002\u4f46\u662fmap\u548cvector\u4e0d\u540c\u7684\u662f\uff0c\u4ed6\u6709\u4e24\u4e2a\u6a21\u677f\u53c2\u6570K\u548cV\u3002\u5982\u679c\u4f60\u7684V\u6709\u591a\u4e2a\u6784\u9020\u51fd\u6570\u53c2\u6570\uff0c\u90a3\u4e48emplace\u64cd\u4f5c\u5c31\u6709\u7279\u6b8a\u7684\u5199\u6cd5\u4e86\u3002<\/p>\n\n\n\n<p>\u4e0a\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;map>\n#include &lt;utility>\nusing namespace std;\nclass A\n{\n    private:\n    int m_value_a;\n    int m_value_b;\n    public:\n    A(int value_a,int value_b):m_value_a(value_a),m_value_b(value_b)\n    {cout&lt;&lt;\"ctor\"&lt;&lt;endl;}\n    A(const A&amp; o)\n    {\n        m_value_a = o.m_value_a;\n        m_value_b = o.m_value_b;\n        cout&lt;&lt;\"copy ctor\"&lt;&lt;endl;\n    }\n};\n\nint main()\n{\n    map&lt;int,A> m;\n    cout&lt;&lt;\"method 1\"&lt;&lt;endl;\n    m.insert({1,{2,3}});\n    cout&lt;&lt;\"method 2\"&lt;&lt;endl;\n    m.emplace(piecewise_construct,forward_as_tuple(1),make_tuple(2,3));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u4e3a\u4e86\u533a\u5206emplace\u7684\u53c2\u6570\u7a76\u7adf\u662fK\u7684\u6784\u9020\u51fd\u6570\u8fd8\u662fV\u7684\u6784\u9020\u51fd\u6570\uff0c\u6240\u4ee5\u8981\u628a\u5404\u81ea\u7684\u6784\u9020\u51fd\u6570\u53c2\u6570\u7528tuple\u5305\u8d77\u6765\u3002\u4e3a\u4e86\u6807\u8bc6\u8fd9\u79cd\u7528\u6cd5\uff0c\u8ba9\u7a0b\u5e8f\u77e5\u9053\u4f60\u4e0d\u662f\u4f20tuple\u4f5c\u4e3a\u53c2\u6570\uff0c\u800c\u53ea\u662f\u7528tuple\u6765\u6253\u5305\u53c2\u6570\uff0c\u6240\u4ee5\u8981\u4f20\u4e00\u4e2apiecewise_construct\u4f5c\u4e3a\u6807\u8bb0\u3002\u8fd9\u4e2a\u7c7b\u662f\u5b9a\u4e49\u5728&lt;utility>\u4e2d\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u6bb5\u7a0b\u5e8f\u6d4b\u8bd5\u4e86insert\u548cemplace\u4e24\u79cd\u63d2\u5165\u65b9\u6cd5\u7684\u6548\u7387\u5dee\u5f02\u3002<\/p>\n\n\n\n<p>\u5728debian\u7cfb\u7edf\uff0cg++8.3.0 \u7248\u672c\u4e0b\u7f16\u8bd1\u8fd0\u884c\uff0c\u7ed3\u679c\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>method 1\nctor\ncopy ctor\ncopy ctor\nmethod 2\nctor<\/code><\/pre>\n\n\n\n<p>insert\u8c03\u7528\u4e86\u4e00\u6b21\u6784\u9020\u51fd\u6570\uff0c\u4e24\u6b21\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/p>\n\n\n\n<p>\u800cemplace\u53ea\u8c03\u7528\u4e86\u4e00\u6b21\u6784\u9020\u51fd\u6570<\/p>\n","protected":false},"excerpt":{"rendered":"<p>map\u5728c++11\u4e2d\u589e\u52a0\u4e86emplace\u64cd\u4f5c\uff0c\u8fd9\u4e2a\u6211\u867d\u7136\u4e4b\u524d\u6ca1\u7528\u8fc7\uff0c\u4f46\u662f\u8fd8\u662f\u660e\u767d\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u6bd5\u7adfve&hellip; <a href=\"http:\/\/iamnear.top\/?p=51\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">c++ stl\u4e2dmap\u7684emplace\u63d2\u5165<\/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-51","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\/51","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=51"}],"version-history":[{"count":1,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":52,"href":"http:\/\/iamnear.top\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions\/52"}],"wp:attachment":[{"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iamnear.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}