{"id":435,"date":"2010-04-26T18:22:39","date_gmt":"2010-04-26T10:22:39","guid":{"rendered":"http:\/\/115.29.108.60\/?p=435"},"modified":"2010-04-26T18:22:39","modified_gmt":"2010-04-26T10:22:39","slug":"c-primer-chapter10","status":"publish","type":"post","link":"http:\/\/115.29.108.60\/?p=435","title":{"rendered":"C++ Primer Chapter10"},"content":{"rendered":"<p>1\u3001\u5173\u8054\u5bb9\u5668\u901a\u8fc7\u952e\u5b58\u50a8\u548c\u8bfb\u53d6\u5143\u7d20\uff0c\u800c\u987a\u5e8f\u5bb9\u5668\u5219\u901a\u8fc7\u5143\u7d20\u5728\u5bb9\u5668\u4e2d\u7684\u4f4d\u7f6e\u987a\u5e8f\u5b58\u50a8\u548c\u8bbf\u95ee\u5143\u7d20\u3002<\/p>\n<p>2\u3001map\u7684\u5143\u7d20\u4ee5\u952e-\u503c\u5bf9\u7684\u5f62\u5f0f\u7ec4\u7ec7\uff1a\u952e\u7528\u4f5c\u5143\u7d20\u5728map\u4e2d\u7684\u7d22\u5f15\uff0c\u800c\u503c\u5219\u8868\u793a\u6240\u5b58\u50a8\u548c\u8bfb\u53d6\u7684\u6570\u636e\u3002set\u4ec5\u5305\u542b\u4e00\u4e2a\u952e\uff0c\u5e76\u6709\u6548\u5730\u652f\u6301\u5173\u4e8e\u67d0\u4e2a\u952e\u662f\u5426\u5b58\u5728\u7684\u67e5\u8be2\u3002<\/p>\n<p>3\u3001map \u5173\u8054\u6570\u7ec4\uff1b\u5143\u7d20\u901a\u8fc7\u952e\u6765\u5b58\u50a8\u548c\u8bfb\u53d6<\/p>\n<p>set \u5927\u5c0f\u53ef\u53d8\u7684\u96c6\u5408\uff0c\u652f\u6301\u901a\u8fc7\u952e\u5b9e\u73b0\u7684\u5feb\u901f\u8bfb\u53d6<\/p>\n<p>multimap \u652f\u6301\u540c\u4e00\u4e2a\u952e\u591a\u6b21\u51fa\u73b0\u7684map\u7c7b\u578b<\/p>\n<p>multiset \u652f\u6301\u540c\u4e00\u4e2a\u952e\u591a\u6b21\u51fa\u73b0\u7684set\u7c7b\u578b<\/p>\n<p>4\u3001pair\u7c7b\u578b\u63d0\u4f9b\u7684\u64cd\u4f5c<\/p>\n<p>pair&lt;T1, T2&gt; p1; \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684pair\u5bf9\u8c61\uff0c\u5b83\u7684\u4e24\u4e2a\u5143\u7d20\u5206\u522b\u662fT1\u548cT2\u7c7b\u578b\uff0c\u91c7\u7528\u503c\u521d\u59cb\u5316<\/p>\n<p>pair&lt;T1, T2&gt; p1(v1, v2); \u521b\u5efa\u4e00\u4e2apair\u5bf9\u8c61\uff0c\u5b83\u7684\u4e24\u4e2a\u5143\u7d20\u5206\u522b\u662fT1\u548cT2\u7c7b\u578b\uff0c\u5176\u4e2dfirst\u6210\u5458\u521d\u59cb\u5316\u4e3av1\uff0c\u800csecond\u6210\u5458\u521d\u59cb\u5316\u4e3av2<\/p>\n<p>make_pair(v1, v2) \u4ee5v1\u548cv2\u503c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684pair\u5bf9\u8c61\uff0c\u5176\u5143\u7d20\u7c7b\u578b\u5206\u522b\u662fv1\u548cv2\u7684\u7c7b\u578b<\/p>\n<p>p1 &lt; p2 \u4e24\u4e2apair\u5bf9\u8c61\u4e4b\u95f4\u7684\u5c0f\u4e8e\u8fd0\u7b97\uff0c\u5176\u5b9a\u4e49\u9075\u5faa\u5b57\u5178\u6b21\u5e8f\uff1a\u5982\u679cp1.first &lt; p2.first \u6216\u8005!(p2.first &lt; p1.first) &amp;&amp; p1.second &lt; p2.second\uff0c\u5219\u8fd4\u56detrue<\/p>\n<p>p1 == p2 \u5982\u679c\u4e24\u4e2apair\u5bf9\u8c61\u7684first\u548csecond\u6210\u5458\u4f9d\u6b21\u76f8\u7b49\uff0c\u5219\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u76f8\u7b49\u3002\u8be5\u8fd0\u7b97\u4f7f\u7528\u5176\u5143\u7d20\u7684==\u64cd\u4f5c\u7b26<\/p>\n<p>p.first \u8fd4\u56dep\u4e2d\u540d\u4e3afirst\u7684\uff08\u516c\u6709\uff09\u6570\u636e\u6210\u5458<\/p>\n<p>p.second \u8fd4\u56dep\u4e2d\u540d\u4e3asecond\u7684\uff08\u516c\u6709\uff09\u6570\u636e\u6210\u5458<\/p>\n<p>5\u3001\u5728\u4f7f\u7528\u5173\u8054\u5bb9\u5668\u65f6\uff0c\u5b83\u7684\u952e\u4e0d\u4f46\u6709\u4e00\u4e2a\u7c7b\u578b\uff0c\u800c\u4e14\u8fd8\u6709\u4e00\u4e2a\u76f8\u5173\u7684\u6bd4\u8f83\u51fd\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6807\u51c6\u5e93\u4f7f\u7528\u952e\u7c7b\u578b\u5b9a\u4e49\u7684&lt;\u64cd\u4f5c\u7b26\u6765\u5b9e\u73b0\u952e\u7684\u6bd4\u8f83\u3002<\/p>\n<p>6\u3001map\u5bf9\u8c61\u7684\u5143\u7d20\u662f\u952e-\u503c\u5bf9\uff0c\u4e5f\u5373\u6bcf\u4e2a\u5143\u7d20\u5305\u542b\u4e24\u4e2a\u90e8\u5206\uff1a\u952e\u4ee5\u53ca\u7531\u952e\u5173\u8054\u7684\u503c\u3002map\u7684value_type\u5c31\u53cd\u6620\u4e86\u8fd9\u4e2a\u4e8b\u5b9e\uff0c\u5b83\u662f\u5b58\u50a8\u5143\u7d20\u7684\u952e\u4ee5\u53ca\u503c\u7684pair\u7c7b\u578b\uff0c\u800c\u4e14\u952e\u4e3aconst\u3002\u4f8b\u5982\uff0cword_count\u6570\u7ec4\u7684value_type\u4e3apair&lt;const string, int&gt;\u7c7b\u578b\u3002<\/p>\n<p>\u5728\u5b66\u4e60map\u7684\u63a5\u53e3\u65f6\uff0c\u9700\u8c28\u8bb0value_type\u662fpair\u7c7b\u578b\uff0c\u5b83\u7684\u503c\u6210\u5458\u53ef\u4ee5\u4fee\u6539\uff0c\u4f46\u952e\u6210\u5458\u4e0d\u80fd\u4fee\u6539\u3002<\/p>\n<p>7\u3001map\u7c7b\u5b9a\u4e49\u7684\u7c7b\u578b<\/p>\n<p>map&lt;K, V&gt;::key_type \u5728map\u5bb9\u5668\u4e2d\uff0c\u7528\u505a\u7d22\u5f15\u7684\u952e\u7684\u7c7b\u578b<\/p>\n<p>map&lt;K, V&gt;::mapped_type \u5728map\u5bb9\u5668\u4e2d\uff0c\u952e\u6240\u5173\u8054\u7684\u503c\u7684\u7c7b\u578b<\/p>\n<p>map&lt;K, V&gt;::value_type \u4e00\u4e2apair\u7c7b\u578b\uff0c\u5b83\u7684first\u5143\u7d20\u5177\u6709const map&lt;K, V&gt;::key_type\u7c7b\u578b\uff0c\u800csecond\u5143\u7d20\u5219\u4e3amap&lt;K, V&gt;::mapped_type\u7c7b\u578b<\/p>\n<p>8\u3001\u4f7f\u7528\u4e0b\u6807\u8bbf\u95eemap\u4e0e\u4f7f\u7528\u4e0b\u6807\u8bbf\u95ee\u6570\u7ec4\u6216vector\u7684\u884c\u4e3a\u622a\u7136\u4e0d\u540c\uff1a\u7528\u4e0b\u6807\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u5143\u7d20\u7b26\u53f7\u5c06\u5bfc\u81f4\u5728map\u5bb9\u5668\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\uff0c\u5b83\u7684\u952e\u5373\u4e3a\u8be5\u4e0b\u6807\u503c\u3002<\/p>\n<p>9\u3001map\u5bb9\u5668\u63d0\u4f9b\u7684insert\u64cd\u4f5c<\/p>\n<p>m.insert(e) e\u662f\u4e00\u4e2a\u7528\u5728m\u4e0a\u7684value_type\u7c7b\u578b\u7684\u503c\u3002\u5982\u679c\u952e\uff08e.first\uff09\u4e0d\u5728m\u4e2d\uff0c\u5219\u63d2\u5165\u4e00\u4e2a\u503c\u4e3ae.second\u7684\u65b0\u5143\u7d20\uff1b\u5982\u679c\u8be5\u952e\u5728m\u4e2d\u5df2\u5b58\u5728\uff0c\u5219\u4fdd\u6301m\u4e0d\u53d8\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2apair\u7c7b\u5bf9\u8c61\uff0c\u5305\u542b\u6307\u5411\u952e\u4e3ae.first\u7684\u5143\u7d20\u7684map\u8fed\u4ee3\u5668\uff0c\u4ee5\u53ca\u4e00\u4e2abool\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u8868\u793a\u662f\u5426\u63d2\u5165\u4e86\u8be5\u5143\u7d20<\/p>\n<p>m.insert(beg, end) beg\u548cend\u662f\u6807\u8bb0\u5143\u7d20\u8303\u56f4\u7684\u8fed\u4ee3\u5668\uff0c\u5176\u4e2d\u7684\u5143\u7d20\u5fc5\u987b\u4e3am.value_type\u7c7b\u578b\u7684\u952e-\u503c\u5bf9\u3002\u5bf9\u4e8e\u8be5\u8303\u56f4\u5185\u7684\u6240\u6709\u5143\u7d20\uff0c\u5982\u679c\u5b83\u7684\u952e\u5728m\u4e2d\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u8be5\u952e\u53ca\u5176\u5173\u8054\u7684\u503c\u63d2\u5165\u5230m\u3002\u8fd4\u56devoid\u7c7b\u578b<\/p>\n<p>m.insert(iter, e) e\u662f\u4e00\u4e2a\u7528\u5728m\u4e0a\u7684value_type\u7c7b\u578b\u7684\u503c\u3002\u5982\u679c\u952e\uff08e.first\uff09\u4e0d\u5728m\u4e2d\uff0c\u5219\u521b\u5efa\u65b0\u5143\u7d20\uff0c\u5e76\u4ee5\u8fed\u4ee3\u5668iter\u4e3a\u4e03\u70b9\u641c\u7d22\u65b0\u5143\u7d20\u5b58\u50a8\u7684\u4f4d\u7f6e\u3002\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6307\u5411m\u4e2d\u5177\u6709\u7ed9\u5b9a\u952e\u7684\u5143\u7d20<\/p>\n<p>10\u3001\u4e0d\u4fee\u6539map\u5bf9\u8c61\u7684\u67e5\u8be2\u64cd\u4f5c<\/p>\n<p>m.count(k) \u8fd4\u56dem\u4e2dk\u7684\u51fa\u73b0\u6b21\u6570<\/p>\n<p>m.find(k) \u5982\u679cm\u5bb9\u5668\u4e2d\u5b58\u5728\u6309k\u7d22\u5f15\u7684\u5143\u7d20\uff0c\u5219\u8fd4\u56de\u6307\u5411\u8be5\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u8d85\u51fa\u672b\u7aef\u8fed\u4ee3\u5668<\/p>\n<p>11\u3001\u8fd4\u56de\u8fed\u4ee3\u5668\u7684\u5173\u8054\u5bb9\u5668\u64cd\u4f5c<\/p>\n<p>m.lower_bound(k) \u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6307\u5411\u952e\u4e0d\u5c0f\u4e8ek\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>m.upper_bound(k) \u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6307\u5411\u952e\u5927\u4e8ek\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/p>\n<p>m.equal_range(k) \u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u7684pair\u5bf9\u8c61\u3002\u5b83\u7684first\u6210\u5458\u7b49\u4ef7\u4e8em.lower_bound(k)\u3002\u800csecond\u6210\u5458\u5219\u7b49\u4ef7\u4e8em.upper_bound(k)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u5173\u8054\u5bb9\u5668\u901a\u8fc7\u952e\u5b58\u50a8\u548c\u8bfb\u53d6\u5143\u7d20\uff0c\u800c\u987a\u5e8f\u5bb9\u5668\u5219\u901a\u8fc7\u5143\u7d20\u5728\u5bb9\u5668\u4e2d\u7684\u4f4d\u7f6e\u987a\u5e8f\u5b58\u50a8\u548c\u8bbf\u95ee\u5143\u7d20\u3002 2\u3001map\u7684\u5143\u7d20\u4ee5\u952e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-435","post","type-post","status-publish","format-standard","hentry","category-cplusplusprimer"],"_links":{"self":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=435"}],"version-history":[{"count":1,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":436,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions\/436"}],"wp:attachment":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}