{"id":304,"date":"2012-03-22T12:05:21","date_gmt":"2012-03-22T12:05:21","guid":{"rendered":"http:\/\/ahsiu.com\/?p=304"},"modified":"2012-03-22T12:05:21","modified_gmt":"2012-03-22T12:05:21","slug":"c-map%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.xiunian.wang\/?p=304","title":{"rendered":"c++ map\u7684\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p><span style=\"color: #38761d;\">&#8212;&#8212;&#8212;&#8212;&#8212;-\u6765\u81ea\u70d9\u5370\u7a7a\u95f4&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/span><\/p>\n<p><span style=\"color: #38761d;\">Map\u662fc++\u7684\u4e00\u4e2a\u6807\u51c6\u5bb9\u5668\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u597d\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u5728\u4e00\u4e9b\u7a0b\u5e8f\u4e2d\u5efa\u7acb\u4e00\u4e2amap\u53ef\u4ee5\u8d77\u5230\u4e8b\u534a\u529f\u500d\u7684\u6548\u679c\uff0c\u603b\u7ed3\u4e86\u4e00\u4e9bmap\u57fa\u672c\u7b80\u5355\u5b9e\u7528\u7684\u64cd\u4f5c\uff01 1. <wbr><wbr>map\u6784\u9020\u51fd\u6570\uff1b <wbr><wbr><wbr><wbr>map&lt;string , int &gt;mapstring; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>map&lt;int ,string &gt;mapint; <wbr><wbr><wbr><wbr>map&lt;sring, char&gt;mapstring; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>map&lt; char ,string&gt;mapchar; <wbr><wbr><wbr><wbr>map&lt;char ,int&gt;mapchar; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>map&lt;int ,char &gt;mapint; <\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\">2. <wbr><wbr>map\u6dfb\u52a0\u6570\u636e\uff1b <wbr><wbr><wbr><wbr>map&lt;int ,string&gt; maplive; <wbr><wbr><wbr><wbr> <wbr><wbr><wbr><wbr>1.maplive.insert(pair&lt;int,string&gt;(102,&#8221;aclive&#8221;)); <wbr><wbr><wbr><wbr>2.maplive.insert(map&lt;int,string&gt;::value_type(321,&#8221;hai&#8221;)); <wbr><wbr><wbr><wbr>3, maplive[112]=&#8221;April&#8221;;\/\/map\u4e2d\u6700\u7b80\u5355\u6700\u5e38\u7528\u7684\u63d2\u5165\u6dfb\u52a0\uff01 3\uff0cmap\u4e2d\u5143\u7d20\u7684\u67e5\u627e\uff1a<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"font-size: small;\"><span style=\"color: #38761d;\"><wbr><wbr><wbr><wbr>find()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u6307\u5411\u952e\u503c\u4e3akey\u7684\u5143\u7d20\uff0c\u5982\u679c\u6ca1\u627e\u5230\u5c31\u8fd4\u56de\u6307\u5411map\u5c3e\u90e8\u7684\u8fed\u4ee3\u5668\u3002 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>map&lt;int ,string &gt;::iterator l_it;; <wbr><wbr> <wbr><wbr><wbr><wbr><wbr><wbr>l_it=maplive.find(112); <wbr><wbr><wbr><wbr><wbr><wbr>if(l_it==maplive.end()) <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout&lt;&lt;&#8220;we do not find 112&#8243;&lt;&lt;endl; <wbr><wbr><wbr><wbr>else cout&lt;&lt;&#8220;wo find 112&#8243;&lt;&lt;endl; 4,map\u4e2d\u5143\u7d20\u7684\u5220\u9664\uff1a <wbr><wbr><wbr><wbr>\u5982\u679c\u5220\u9664112\uff1b <wbr><wbr><wbr><wbr>map&lt;int ,string &gt;::iterator l_it;; <wbr><wbr><wbr><wbr>l_it=maplive.find(112); <wbr><wbr><wbr><wbr>if(l_it==maplive.end()) <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout&lt;&lt;&#8220;we do not find 112&#8243;&lt;&lt;endl; <wbr><wbr><wbr><wbr>else <wbr><wbr>maplive.erase(l_it); <wbr><wbr>\/\/delete 112; 5,map\u4e2d swap\u7684\u7528\u6cd5\uff1a <wbr><wbr>Map\u4e2d\u7684swap\u4e0d\u662f\u4e00\u4e2a\u5bb9\u5668\u4e2d\u7684\u5143\u7d20\u4ea4\u6362\uff0c\u800c\u662f\u4e24\u4e2a\u5bb9\u5668\u4ea4\u6362\uff1b <wbr><wbr>For example\uff1a <wbr><wbr>#include &lt;map&gt; <wbr><wbr>#include &lt;iostream&gt;<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>using namespace std; <\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr>int main( ) <wbr><wbr>{ <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>map &lt;int, int&gt; m1, m2, m3; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>map &lt;int, int&gt;::iterator m1_Iter;<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 1, 10 ) ); <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 2, 20 ) ); <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 3, 30 ) ); <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m2.insert ( pair &lt;int, int&gt; <wbr><wbr>( 10, 100 ) ); <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m2.insert ( pair &lt;int, int&gt; <wbr><wbr>( 20, 200 ) ); <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m3.insert ( pair &lt;int, int&gt; <wbr><wbr>( 30, 300 ) );<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>cout &lt;&lt; &#8220;The original map m1 is:&#8221;; <wbr><wbr><wbr><wbr>for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout &lt;&lt; &#8221; &#8221; &lt;&lt; m1_Iter-&gt;second; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout <wbr><wbr><wbr><wbr>&lt;&lt; &#8220;.&#8221; &lt;&lt; endl;<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>\/\/ This is the member function version of swap <wbr><wbr><wbr><wbr>\/\/m2 is said to be the argument map; m1 the target map <wbr><wbr><wbr><wbr>m1.swap( m2 );<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>cout &lt;&lt; &#8220;After swapping with m2, map m1 is:&#8221;; <wbr><wbr><wbr><wbr>for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout &lt;&lt; &#8221; &#8221; &lt;&lt; m1_Iter -&gt; second; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout <wbr><wbr>&lt;&lt; &#8220;.&#8221; &lt;&lt; endl; <wbr><wbr><wbr><wbr>cout &lt;&lt; &#8220;After swapping with m2, map m2 is:&#8221;; <wbr><wbr><wbr><wbr>for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ ) <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout &lt;&lt; &#8221; &#8221; &lt;&lt; m1_Iter -&gt; second; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout <wbr><wbr>&lt;&lt; &#8220;.&#8221; &lt;&lt; endl; <wbr><wbr><wbr><wbr>\/\/ This is the specialized template version of swap <wbr><wbr><wbr><wbr>swap( m1, m3 );<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>cout &lt;&lt; &#8220;After swapping with m3, map m1 is:&#8221;; <wbr><wbr><wbr><wbr>for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout &lt;&lt; &#8221; &#8221; &lt;&lt; m1_Iter -&gt; second; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout <wbr><wbr><wbr><wbr>&lt;&lt; &#8220;.&#8221; &lt;&lt; endl; }<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\">6.map\u7684sort\u95ee\u9898\uff1a <wbr><wbr>Map\u4e2d\u7684\u5143\u7d20\u662f\u81ea\u52a8\u6309key\u5347\u5e8f\u6392\u5e8f,\u6240\u4ee5\u4e0d\u80fd\u5bf9map\u7528sort\u51fd\u6570\uff1a <wbr><wbr>For example\uff1a <wbr><wbr>#include &lt;map&gt; <wbr><wbr>#include &lt;iostream&gt;<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>using namespace std; <\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr>int main( ) <wbr><wbr>{ <wbr><wbr><wbr><wbr><wbr><wbr>map &lt;int, int&gt; m1; <wbr><wbr><wbr><wbr>map &lt;int, int&gt;::iterator m1_Iter;<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 1, 20 ) ); <wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 4, 40 ) ); <wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 3, 60 ) ); <wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 2, 50 ) ); <wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 6, 40 ) ); <wbr><wbr><wbr><wbr>m1.insert ( pair &lt;int, int&gt; <wbr><wbr>( 7, 30 ) );<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\"><wbr><wbr><wbr><wbr>cout &lt;&lt; &#8220;The original map m1 is:&#8221;&lt;&lt;endl; <wbr><wbr><wbr><wbr>for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cout &lt;&lt; <wbr><wbr>m1_Iter-&gt;first&lt;&lt;&#8221; &#8220;&lt;&lt;m1_Iter-&gt;second&lt;&lt;endl; <wbr><wbr><wbr><wbr> } <wbr><wbr>The original map m1 is: <wbr><wbr>1 20 <wbr><wbr>2 50 <wbr><wbr>3 60 <wbr><wbr>4 40 <wbr><wbr>6 40 <wbr><wbr>7 30 <wbr><wbr>\u8bf7\u6309\u4efb\u610f\u952e\u7ee7\u7eed. . .<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p><span style=\"color: #38761d; font-size: small;\">7\uff0c <wbr><wbr><wbr><wbr>map\u7684\u57fa\u672c\u64cd\u4f5c\u51fd\u6570\uff1a <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>C++ Maps\u662f\u4e00\u79cd\u5173\u8054\u5f0f\u5bb9\u5668\uff0c\u5305\u542b\u201c\u5173\u952e\u5b57\/\u503c\u201d\u5bf9 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>begin() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u6307\u5411map\u5934\u90e8\u7684\u8fed\u4ee3\u5668 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>clear(\uff09 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u5220\u9664\u6240\u6709\u5143\u7d20 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>count() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u6307\u5b9a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>empty() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u5982\u679cmap\u4e3a\u7a7a\u5219\u8fd4\u56detrue <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>end() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u6307\u5411map\u672b\u5c3e\u7684\u8fed\u4ee3\u5668 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>equal_range() <wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u7279\u6b8a\u6761\u76ee\u7684\u8fed\u4ee3\u5668\u5bf9 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>erase() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u5220\u9664\u4e00\u4e2a\u5143\u7d20 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>find() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u67e5\u627e\u4e00\u4e2a\u5143\u7d20 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>get_allocator() <wbr><wbr>\u8fd4\u56demap\u7684\u914d\u7f6e\u5668 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>insert() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u63d2\u5165\u5143\u7d20 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>key_comp() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u6bd4\u8f83\u5143\u7d20key\u7684\u51fd\u6570 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>lower_bound() <wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u952e\u503c&gt;=\u7ed9\u5b9a\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>max_size() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u53ef\u4ee5\u5bb9\u7eb3\u7684\u6700\u5927\u5143\u7d20\u4e2a\u6570 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>rbegin() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u4e00\u4e2a\u6307\u5411map\u5c3e\u90e8\u7684\u9006\u5411\u8fed\u4ee3\u5668 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>rend() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u4e00\u4e2a\u6307\u5411map\u5934\u90e8\u7684\u9006\u5411\u8fed\u4ee3\u5668 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>size() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56demap\u4e2d\u5143\u7d20\u7684\u4e2a\u6570 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>swap() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u4ea4\u6362\u4e24\u4e2amap <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>upper_bound() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u952e\u503c&gt;\u7ed9\u5b9a\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>value_comp() <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>\u8fd4\u56de\u6bd4\u8f83\u5143\u7d20value\u7684\u51fd\u6570<\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/wbr><\/span><\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-\u6765\u81ea\u9177\u52e4\u7f51&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Map\u662fSTL\u7684\u4e00\u4e2a\u5173\u8054\u5bb9\u5668\uff0c\u5b83\u63d0\u4f9b\u4e00\u5bf9\u4e00\uff08\u5176\u4e2d\u7b2c\u4e00\u4e2a\u53ef\u4ee5\u79f0\u4e3a\u5173\u952e\u5b57\uff0c\u6bcf\u4e2a\u5173\u952e\u5b57\u53ea\u80fd\u5728map\u4e2d\u51fa\u73b0\u4e00\u6b21\uff0c\u7b2c\u4e8c\u4e2a\u53ef\u80fd\u79f0\u4e3a\u8be5\u5173\u952e\u5b57\u7684\u503c\uff09\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\uff0c\u7531\u4e8e\u8fd9\u4e2a\u7279\u6027\uff0c\u5b83\u5b8c\u6210\u6709\u53ef\u80fd\u5728\u6211\u4eec\u5904\u7406\u4e00\u5bf9\u4e00\u6570\u636e\u7684\u65f6\u5019\uff0c\u5728\u7f16\u7a0b\u4e0a\u63d0\u4f9b\u5feb\u901f\u901a\u9053\u3002\u8fd9\u91cc\u8bf4\u4e0bmap\u5185\u90e8\u6570\u636e\u7684\u7ec4\u7ec7\uff0cmap\u5185\u90e8\u81ea\u5efa\u4e00\u9897\u7ea2\u9ed1\u6811(\u4e00\u79cd\u975e\u4e25\u683c\u610f\u4e49\u4e0a\u7684\u5e73\u8861\u4e8c\u53c9\u6811)\uff0c\u8fd9\u9897\u6811\u5177\u6709\u5bf9\u6570\u636e\u81ea\u52a8\u6392\u5e8f\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u5728map\u5185\u90e8\u6240\u6709\u7684\u6570\u636e\u90fd\u662f\u6709\u5e8f\u7684\uff0c\u540e\u8fb9\u6211\u4eec\u4f1a\u89c1\u8bc6\u5230\u6709\u5e8f\u7684\u597d\u5904\u3002<\/p>\n<p>\u4e0b\u9762\u4e3e\u4f8b\u8bf4\u660e\u4ec0\u4e48\u662f\u4e00\u5bf9\u4e00\u7684\u6570\u636e\u6620\u5c04\u3002\u6bd4\u5982\u4e00\u4e2a\u73ed\u7ea7\u4e2d\uff0c\u6bcf\u4e2a\u5b66\u751f\u7684\u5b66\u53f7\u8ddf\u4ed6\u7684\u59d3\u540d\u5c31\u5b58\u5728\u7740\u4e00\u4e00\u6620\u5c04\u7684\u5173\u7cfb\uff0c\u8fd9\u4e2a\u6a21\u578b\u7528map\u53ef\u80fd\u8f7b\u6613\u63cf\u8ff0\uff0c\u5f88\u660e\u663e\u5b66\u53f7\u7528int\u63cf\u8ff0\uff0c\u59d3\u540d\u7528\u5b57\u7b26\u4e32\u63cf\u8ff0(\u672c\u7bc7\u6587\u7ae0\u4e2d\u4e0d\u7528char *\u6765\u63cf\u8ff0\u5b57\u7b26\u4e32\uff0c\u800c\u662f\u91c7\u7528STL\u4e2dstring\u6765\u63cf\u8ff0),\u4e0b\u9762\u7ed9\u51famap\u63cf\u8ff0\u4ee3\u7801\uff1a<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 map\u7684\u6784\u9020\u51fd\u6570<\/p>\n<p>map\u5171\u63d0\u4f9b\u4e866\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u5757\u6d89\u53ca\u5230\u5185\u5b58\u5206\u914d\u5668\u8fd9\u4e9b\u4e1c\u897f\uff0c\u7565\u8fc7\u4e0d\u8868\uff0c\u5728\u4e0b\u9762\u6211\u4eec\u5c06\u63a5\u89e6\u5230\u4e00\u4e9bmap\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u8fd9\u91cc\u8981\u8bf4\u4e0b\u7684\u5c31\u662f\uff0c\u6211\u4eec\u901a\u5e38\u7528\u5982\u4e0b\u65b9\u6cd5\u6784\u9020\u4e00\u4e2amap\uff1a<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u6570\u636e\u7684\u63d2\u5165<\/p>\n<p>\u5728\u6784\u9020map\u5bb9\u5668\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5f80\u91cc\u9762\u63d2\u5165\u6570\u636e\u4e86\u3002\u8fd9\u91cc\u8bb2\u4e09\u79cd\u63d2\u5165\u6570\u636e\u7684\u65b9\u6cd5\uff1a<\/p>\n<p>\u7b2c\u4e00\u79cd\uff1a\u7528insert\u51fd\u6570\u63d2\u5165pair\u6570\u636e\uff0c\u4e0b\u9762\u4e3e\u4f8b\u8bf4\u660e(\u4ee5\u4e0b\u4ee3\u7801\u867d\u7136\u662f\u968f\u624b\u5199\u7684\uff0c\u5e94\u8be5\u53ef\u4ee5\u5728VC\u548cGCC\u4e0b\u7f16\u8bd1\u901a\u8fc7\uff0c\u5927\u5bb6\u53ef\u4ee5\u8fd0\u884c\u4e0b\u770b\u4ec0\u4e48\u6548\u679c\uff0c\u5728VC\u4e0b\u8bf7\u52a0\u5165\u8fd9\u6761\u8bed\u53e5\uff0c\u5c4f\u853d4786\u8b66\u544a\u00a0 \uff03pragma warning (disable:4786) )<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(1, \u201cstudent_one\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(2, \u201cstudent_two\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(3, \u201cstudent_three\u201d));<\/p>\n<p>map&lt;int, string&gt;::iterator\u00a0 iter;<\/p>\n<p>for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;iter-&gt;first&lt;&lt;\u201d\u00a0\u00a0 \u201d&lt;&lt;iter-&gt;second&lt;&lt;end;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u7b2c\u4e8c\u79cd\uff1a\u7528insert\u51fd\u6570\u63d2\u5165value_type\u6570\u636e\uff0c\u4e0b\u9762\u4e3e\u4f8b\u8bf4\u660e<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent.insert(map&lt;int, string&gt;::value_type (1, \u201cstudent_one\u201d));<\/p>\n<p>mapStudent.insert(map&lt;int, string&gt;::value_type (2, \u201cstudent_two\u201d));<\/p>\n<p>mapStudent.insert(map&lt;int, string&gt;::value_type (3, \u201cstudent_three\u201d));<\/p>\n<p>map&lt;int, string&gt;::iterator\u00a0 iter;<\/p>\n<p>for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;iter-&gt;first&lt;&lt;\u201d\u00a0\u00a0 \u201d&lt;&lt;iter-&gt;second&lt;&lt;end;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u7b2c\u4e09\u79cd\uff1a\u7528\u6570\u7ec4\u65b9\u5f0f\u63d2\u5165\u6570\u636e\uff0c\u4e0b\u9762\u4e3e\u4f8b\u8bf4\u660e<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent[1] =\u00a0 \u201cstudent_one\u201d;<\/p>\n<p>mapStudent[2] =\u00a0 \u201cstudent_two\u201d;<\/p>\n<p>mapStudent[3] =\u00a0 \u201cstudent_three\u201d;<\/p>\n<p>map&lt;int, string&gt;::iterator\u00a0 iter;<\/p>\n<p>for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;iter-&gt;first&lt;&lt;\u201d\u00a0\u00a0 \u201d&lt;&lt;iter-&gt;second&lt;&lt;end;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u4ee5\u4e0a\u4e09\u79cd\u7528\u6cd5\uff0c\u867d\u7136\u90fd\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u63d2\u5165\uff0c\u4f46\u662f\u5b83\u4eec\u662f\u6709\u533a\u522b\u7684\uff0c\u5f53\u7136\u4e86\u7b2c\u4e00\u79cd\u548c\u7b2c\u4e8c\u79cd\u5728\u6548\u679c\u4e0a\u662f\u5b8c\u6210\u4e00\u6837\u7684\uff0c\u7528insert\u51fd\u6570\u63d2\u5165\u6570\u636e\uff0c\u5728\u6570\u636e\u7684\u63d2\u5165\u4e0a\u6d89\u53ca\u5230\u96c6\u5408\u7684\u552f\u4e00\u6027\u8fd9\u4e2a\u6982\u5ff5\uff0c\u5373\u5f53map\u4e2d\u6709\u8fd9\u4e2a\u5173\u952e\u5b57\u65f6\uff0cinsert\u64cd\u4f5c\u662f\u63d2\u5165\u6570\u636e\u4e0d\u4e86\u7684\uff0c\u4f46\u662f\u7528\u6570\u7ec4\u65b9\u5f0f\u5c31\u4e0d\u540c\u4e86\uff0c\u5b83\u53ef\u4ee5\u8986\u76d6\u4ee5\u524d\u8be5\u5173\u952e\u5b57\u5bf9\u5e94\u7684\u503c\uff0c\u7528\u7a0b\u5e8f\u8bf4\u660e<\/p>\n<p>mapStudent.insert(map&lt;int, string&gt;::value_type (1, \u201cstudent_one\u201d));<\/p>\n<p>mapStudent.insert(map&lt;int, string&gt;::value_type (1, \u201cstudent_two\u201d));<\/p>\n<p>\u4e0a\u9762\u8fd9\u4e24\u6761\u8bed\u53e5\u6267\u884c\u540e\uff0cmap\u4e2d1\u8fd9\u4e2a\u5173\u952e\u5b57\u5bf9\u5e94\u7684\u503c\u662f\u201cstudent_one\u201d\uff0c\u7b2c\u4e8c\u6761\u8bed\u53e5\u5e76\u6ca1\u6709\u751f\u6548\uff0c\u90a3\u4e48\u8fd9\u5c31\u6d89\u53ca\u5230\u6211\u4eec\u600e\u4e48\u77e5\u9053insert\u8bed\u53e5\u662f\u5426\u63d2\u5165\u6210\u529f\u7684\u95ee\u9898\u4e86\uff0c\u53ef\u4ee5\u7528pair\u6765\u83b7\u5f97\u662f\u5426\u63d2\u5165\u6210\u529f\uff0c\u7a0b\u5e8f\u5982\u4e0b<\/p>\n<p>Pair&lt;map&lt;int, string&gt;::iterator, bool&gt; Insert_Pair;<\/p>\n<p>Insert_Pair = mapStudent.insert(map&lt;int, string&gt;::value_type (1, \u201cstudent_one\u201d));<\/p>\n<p>\u6211\u4eec\u901a\u8fc7pair\u7684\u7b2c\u4e8c\u4e2a\u53d8\u91cf\u6765\u77e5\u9053\u662f\u5426\u63d2\u5165\u6210\u529f\uff0c\u5b83\u7684\u7b2c\u4e00\u4e2a\u53d8\u91cf\u8fd4\u56de\u7684\u662f\u4e00\u4e2amap\u7684\u8fed\u4ee3\u5668\uff0c\u5982\u679c\u63d2\u5165\u6210\u529f\u7684\u8bddInsert_Pair.second\u5e94\u8be5\u662ftrue\u7684\uff0c\u5426\u5219\u4e3afalse\u3002<\/p>\n<p>\u4e0b\u9762\u7ed9\u51fa\u5b8c\u6210\u4ee3\u7801\uff0c\u6f14\u793a\u63d2\u5165\u6210\u529f\u4e0e\u5426\u95ee\u9898<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>Pair&lt;map&lt;int, string&gt;::iterator, bool&gt; Insert_Pair;<\/p>\n<p>Insert_Pair \uff1d mapStudent.insert(pair&lt;int, string&gt;(1, \u201cstudent_one\u201d));<\/p>\n<p>If(Insert_Pair.second == true)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;\u201dInsert Successfully\u201d&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>Else<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;\u201dInsert Failure\u201d&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>Insert_Pair \uff1d mapStudent.insert(pair&lt;int, string&gt;(1, \u201cstudent_two\u201d));<\/p>\n<p>If(Insert_Pair.second == true)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;\u201dInsert Successfully\u201d&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>Else<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;\u201dInsert Failure\u201d&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>map&lt;int, string&gt;::iterator\u00a0 iter;<\/p>\n<p>for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;iter-&gt;first&lt;&lt;\u201d\u00a0\u00a0 \u201d&lt;&lt;iter-&gt;second&lt;&lt;end;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u5927\u5bb6\u53ef\u4ee5\u7528\u5982\u4e0b\u7a0b\u5e8f\uff0c\u770b\u4e0b\u7528\u6570\u7ec4\u63d2\u5165\u5728\u6570\u636e\u8986\u76d6\u4e0a\u7684\u6548\u679c<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent[1] =\u00a0 \u201cstudent_one\u201d;<\/p>\n<p>mapStudent[1] =\u00a0 \u201cstudent_two\u201d;<\/p>\n<p>mapStudent[2] =\u00a0 \u201cstudent_three\u201d;<\/p>\n<p>map&lt;int, string&gt;::iterator\u00a0 iter;<\/p>\n<p>for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;iter-&gt;first&lt;&lt;\u201d\u00a0\u00a0 \u201d&lt;&lt;iter-&gt;second&lt;&lt;end;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>3.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 map\u7684\u5927\u5c0f<\/p>\n<p>\u5728\u5f80map\u91cc\u9762\u63d2\u5165\u4e86\u6570\u636e\uff0c\u6211\u4eec\u600e\u4e48\u77e5\u9053\u5f53\u524d\u5df2\u7ecf\u63d2\u5165\u4e86\u591a\u5c11\u6570\u636e\u5462\uff0c\u53ef\u4ee5\u7528size\u51fd\u6570\uff0c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>Int nSize = mapStudent.size();<\/p>\n<p>4.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u6570\u636e\u7684\u904d\u5386<\/p>\n<p>\u8fd9\u91cc\u4e5f\u63d0\u4f9b\u4e09\u79cd\u65b9\u6cd5\uff0c\u5bf9map\u8fdb\u884c\u904d\u5386<\/p>\n<p>\u7b2c\u4e00\u79cd\uff1a\u5e94\u7528\u524d\u5411\u8fed\u4ee3\u5668\uff0c\u4e0a\u9762\u4e3e\u4f8b\u7a0b\u5e8f\u4e2d\u5230\u5904\u90fd\u662f\u4e86\uff0c\u7565\u8fc7\u4e0d\u8868<\/p>\n<p>\u7b2c\u4e8c\u79cd\uff1a\u5e94\u7528\u53cd\u76f8\u8fed\u4ee3\u5668\uff0c\u4e0b\u9762\u4e3e\u4f8b\u8bf4\u660e\uff0c\u8981\u4f53\u4f1a\u6548\u679c\uff0c\u8bf7\u81ea\u4e2a\u52a8\u624b\u8fd0\u884c\u7a0b\u5e8f<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(1, \u201cstudent_one\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(2, \u201cstudent_two\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(3, \u201cstudent_three\u201d));<\/p>\n<p>map&lt;int, string&gt;::reverse_iterator\u00a0 iter;<\/p>\n<p>for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;iter-&gt;first&lt;&lt;\u201d\u00a0\u00a0 \u201d&lt;&lt;iter-&gt;second&lt;&lt;end;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u7b2c\u4e09\u79cd\uff1a\u7528\u6570\u7ec4\u65b9\u5f0f\uff0c\u7a0b\u5e8f\u8bf4\u660e\u5982\u4e0b<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(1, \u201cstudent_one\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(2, \u201cstudent_two\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(3, \u201cstudent_three\u201d));<\/p>\n<p>int nSize = mapStudent.size()<\/p>\n<p>\/\/\u6b64\u5904\u6709\u8bef\uff0c\u5e94\u8be5\u662f for(int nIndex = 1; nIndex &lt;= nSize; nIndex++)<\/p>\n<p>\/\/by rainfish<\/p>\n<p>for(int nIndex = 0; nIndex &lt; nSize; nIndex++)<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;mapStudent[nIndex]&lt;&lt;end;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>5.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u6570\u636e\u7684\u67e5\u627e\uff08\u5305\u62ec\u5224\u5b9a\u8fd9\u4e2a\u5173\u952e\u5b57\u662f\u5426\u5728map\u4e2d\u51fa\u73b0\uff09<\/p>\n<p>\u5728\u8fd9\u91cc\u6211\u4eec\u5c06\u4f53\u4f1a\uff0cmap\u5728\u6570\u636e\u63d2\u5165\u65f6\u4fdd\u8bc1\u6709\u5e8f\u7684\u597d\u5904\u3002<\/p>\n<p>\u8981\u5224\u5b9a\u4e00\u4e2a\u6570\u636e\uff08\u5173\u952e\u5b57\uff09\u662f\u5426\u5728map\u4e2d\u51fa\u73b0\u7684\u65b9\u6cd5\u6bd4\u8f83\u591a\uff0c\u8fd9\u91cc\u6807\u9898\u867d\u7136\u662f\u6570\u636e\u7684\u67e5\u627e\uff0c\u5728\u8fd9\u91cc\u5c06\u7a7f\u63d2\u7740\u5927\u91cf\u7684map\u57fa\u672c\u7528\u6cd5\u3002<\/p>\n<p>\u8fd9\u91cc\u7ed9\u51fa\u4e09\u79cd\u6570\u636e\u67e5\u627e\u65b9\u6cd5<\/p>\n<p>\u7b2c\u4e00\u79cd\uff1a\u7528count\u51fd\u6570\u6765\u5224\u5b9a\u5173\u952e\u5b57\u662f\u5426\u51fa\u73b0\uff0c\u5176\u7f3a\u70b9\u662f\u65e0\u6cd5\u5b9a\u4f4d\u6570\u636e\u51fa\u73b0\u4f4d\u7f6e,\u7531\u4e8emap\u7684\u7279\u6027\uff0c\u4e00\u5bf9\u4e00\u7684\u6620\u5c04\u5173\u7cfb\uff0c\u5c31\u51b3\u5b9a\u4e86count\u51fd\u6570\u7684\u8fd4\u56de\u503c\u53ea\u6709\u4e24\u4e2a\uff0c\u8981\u4e48\u662f0\uff0c\u8981\u4e48\u662f1\uff0c\u51fa\u73b0\u7684\u60c5\u51b5\uff0c\u5f53\u7136\u662f\u8fd4\u56de1\u4e86<\/p>\n<p>\u7b2c\u4e8c\u79cd\uff1a\u7528find\u51fd\u6570\u6765\u5b9a\u4f4d\u6570\u636e\u51fa\u73b0\u4f4d\u7f6e\uff0c\u5b83\u8fd4\u56de\u7684\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5f53\u6570\u636e\u51fa\u73b0\u65f6\uff0c\u5b83\u8fd4\u56de\u6570\u636e\u6240\u5728\u4f4d\u7f6e\u7684\u8fed\u4ee3\u5668\uff0c\u5982\u679cmap\u4e2d\u6ca1\u6709\u8981\u67e5\u627e\u7684\u6570\u636e\uff0c\u5b83\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u7b49\u4e8eend\u51fd\u6570\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\uff0c\u7a0b\u5e8f\u8bf4\u660e<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(1, \u201cstudent_one\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(2, \u201cstudent_two\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(3, \u201cstudent_three\u201d));<\/p>\n<p>map&lt;int, string&gt;::iterator iter;<\/p>\n<p>iter = mapStudent.find(1);<\/p>\n<p>if(iter != mapStudent.end())<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;\u201dFind, the value is \u201d&lt;&lt;iter-&gt;second&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>Else<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;\u201dDo not Find\u201d&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u7b2c\u4e09\u79cd\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u7528\u6765\u5224\u5b9a\u6570\u636e\u662f\u5426\u51fa\u73b0\uff0c\u662f\u663e\u5f97\u7b28\u4e86\u70b9\uff0c\u4f46\u662f\uff0c\u6211\u6253\u7b97\u5728\u8fd9\u91cc\u8bb2\u89e3<\/p>\n<p>Lower_bound\u51fd\u6570\u7528\u6cd5\uff0c\u8fd9\u4e2a\u51fd\u6570\u7528\u6765\u8fd4\u56de\u8981\u67e5\u627e\u5173\u952e\u5b57\u7684\u4e0b\u754c(\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668)<\/p>\n<p>Upper_bound\u51fd\u6570\u7528\u6cd5\uff0c\u8fd9\u4e2a\u51fd\u6570\u7528\u6765\u8fd4\u56de\u8981\u67e5\u627e\u5173\u952e\u5b57\u7684\u4e0a\u754c(\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668)<\/p>\n<p>\u4f8b\u5982\uff1amap\u4e2d\u5df2\u7ecf\u63d2\u5165\u4e861\uff0c2\uff0c3\uff0c4\u7684\u8bdd\uff0c\u5982\u679clower_bound(2)\u7684\u8bdd\uff0c\u8fd4\u56de\u76842\uff0c\u800cupper-bound\uff082\uff09\u7684\u8bdd\uff0c\u8fd4\u56de\u7684\u5c31\u662f3<\/p>\n<p>Equal_range\u51fd\u6570\u8fd4\u56de\u4e00\u4e2apair\uff0cpair\u91cc\u9762\u7b2c\u4e00\u4e2a\u53d8\u91cf\u662fLower_bound\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\uff0cpair\u91cc\u9762\u7b2c\u4e8c\u4e2a\u8fed\u4ee3\u5668\u662fUpper_bound\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\uff0c\u5982\u679c\u8fd9\u4e24\u4e2a\u8fed\u4ee3\u5668\u76f8\u7b49\u7684\u8bdd\uff0c\u5219\u8bf4\u660emap\u4e2d\u4e0d\u51fa\u73b0\u8fd9\u4e2a\u5173\u952e\u5b57\uff0c\u7a0b\u5e8f\u8bf4\u660e<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent[1] =\u00a0 \u201cstudent_one\u201d;<\/p>\n<p>mapStudent[3] =\u00a0 \u201cstudent_three\u201d;<\/p>\n<p>mapStudent[5] =\u00a0 \u201cstudent_five\u201d;<\/p>\n<p>map&lt;int, string&gt;::iterator\u00a0 iter;<\/p>\n<p>iter = mapStudent.lower_bound(2);<\/p>\n<p>{<\/p>\n<p>\/\/\u8fd4\u56de\u7684\u662f\u4e0b\u754c3\u7684\u8fed\u4ee3\u5668<\/p>\n<p>Cout&lt;&lt;iter-&gt;second&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>iter = mapStudent.lower_bound(3);<\/p>\n<p>{<\/p>\n<p>\/\/\u8fd4\u56de\u7684\u662f\u4e0b\u754c3\u7684\u8fed\u4ee3\u5668<\/p>\n<p>Cout&lt;&lt;iter-&gt;second&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>iter = mapStudent.upper_bound(2);<\/p>\n<p>{<\/p>\n<p>\/\/\u8fd4\u56de\u7684\u662f\u4e0a\u754c3\u7684\u8fed\u4ee3\u5668<\/p>\n<p>Cout&lt;&lt;iter-&gt;second&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>iter = mapStudent.upper_bound(3);<\/p>\n<p>{<\/p>\n<p>\/\/\u8fd4\u56de\u7684\u662f\u4e0a\u754c5\u7684\u8fed\u4ee3\u5668<\/p>\n<p>Cout&lt;&lt;iter-&gt;second&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Pair&lt;map&lt;int, string&gt;::iterator, map&lt;int, string&gt;::iterator&gt; mapPair;<\/p>\n<p>mapPair = mapStudent.equal_range(2);<\/p>\n<p>if(mapPair.first == mapPair.second) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/p>\n<p>cout&lt;&lt;\u201dDo not Find\u201d&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>Else<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;\u201dFind\u201d&lt;&lt;endl; }<\/p>\n<p>mapPair = mapStudent.equal_range(3);<\/p>\n<p>if(mapPair.first == mapPair.second) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {<\/p>\n<p>cout&lt;&lt;\u201dDo not Find\u201d&lt;&lt;endl;<\/p>\n<p>}<\/p>\n<p>Else<\/p>\n<p>{<\/p>\n<p>Cout&lt;&lt;\u201dFind\u201d&lt;&lt;endl; }<\/p>\n<p>}<\/p>\n<p>6.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u6570\u636e\u7684\u6e05\u7a7a\u4e0e\u5224\u7a7a<\/p>\n<p>\u6e05\u7a7amap\u4e2d\u7684\u6570\u636e\u53ef\u4ee5\u7528clear()\u51fd\u6570\uff0c\u5224\u5b9amap\u4e2d\u662f\u5426\u6709\u6570\u636e\u53ef\u4ee5\u7528empty()\u51fd\u6570\uff0c\u5b83\u8fd4\u56detrue\u5219\u8bf4\u660e\u662f\u7a7amap<\/p>\n<p>7.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u6570\u636e\u7684\u5220\u9664<\/p>\n<p>\u8fd9\u91cc\u8981\u7528\u5230erase\u51fd\u6570\uff0c\u5b83\u6709\u4e09\u4e2a\u91cd\u8f7d\u4e86\u7684\u51fd\u6570\uff0c\u4e0b\u9762\u5728\u4f8b\u5b50\u4e2d\u8be6\u7ec6\u8bf4\u660e\u5b83\u4eec\u7684\u7528\u6cd5<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>Map&lt;int, string&gt; mapStudent;<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(1, \u201cstudent_one\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(2, \u201cstudent_two\u201d));<\/p>\n<p>mapStudent.insert(pair&lt;int, string&gt;(3, \u201cstudent_three\u201d));<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u5982\u679c\u4f60\u8981\u6f14\u793a\u8f93\u51fa\u6548\u679c\uff0c\u8bf7\u9009\u62e9\u4ee5\u4e0b\u7684\u4e00\u79cd\uff0c\u4f60\u770b\u5230\u7684\u6548\u679c\u4f1a\u6bd4\u8f83\u597d<\/p>\n<p>\/\/\u5982\u679c\u8981\u5220\u96641,\u7528\u8fed\u4ee3\u5668\u5220\u9664<\/p>\n<p>map&lt;int, string&gt;::iterator iter;<\/p>\n<p>iter = mapStudent.find(1);<\/p>\n<p>mapStudent.erase(iter);<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u5982\u679c\u8981\u5220\u96641\uff0c\u7528\u5173\u952e\u5b57\u5220\u9664<\/p>\n<p>Int n = mapStudent.erase(1);\/\/\u5982\u679c\u5220\u9664\u4e86\u4f1a\u8fd4\u56de1\uff0c\u5426\u5219\u8fd4\u56de0<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u7528\u8fed\u4ee3\u5668\uff0c\u6210\u7247\u7684\u5220\u9664<\/p>\n<p>\/\/\u4e00\u4e0b\u4ee3\u7801\u628a\u6574\u4e2amap\u6e05\u7a7a<\/p>\n<p>mapStudent.earse(mapStudent.begin(), mapStudent.end());<\/p>\n<p>\/\/\u6210\u7247\u5220\u9664\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e5f\u662fSTL\u7684\u7279\u6027\uff0c\u5220\u9664\u533a\u95f4\u662f\u4e00\u4e2a\u524d\u95ed\u540e\u5f00\u7684\u96c6\u5408<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u81ea\u4e2a\u52a0\u4e0a\u904d\u5386\u4ee3\u7801\uff0c\u6253\u5370\u8f93\u51fa\u5427<\/p>\n<p>}<\/p>\n<p>8.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u5176\u4ed6\u4e00\u4e9b\u51fd\u6570\u7528\u6cd5<\/p>\n<p>\u8fd9\u91cc\u6709swap,key_comp,value_comp,get_allocator\u7b49\u51fd\u6570\uff0c\u611f\u89c9\u5230\u8fd9\u4e9b\u51fd\u6570\u5728\u7f16\u7a0b\u7528\u7684\u4e0d\u662f\u5f88\u591a\uff0c\u7565\u8fc7\u4e0d\u8868\uff0c\u6709\u5174\u8da3\u7684\u8bdd\u53ef\u4ee5\u81ea\u4e2a\u7814\u7a76<\/p>\n<p>9.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u6392\u5e8f<\/p>\n<p>\u8fd9\u91cc\u8981\u8bb2\u7684\u662f\u4e00\u70b9\u6bd4\u8f83\u9ad8\u6df1\u7684\u7528\u6cd5\u4e86,\u6392\u5e8f\u95ee\u9898\uff0cSTL\u4e2d\u9ed8\u8ba4\u662f\u91c7\u7528\u5c0f\u4e8e\u53f7\u6765\u6392\u5e8f\u7684\uff0c\u4ee5\u4e0a\u4ee3\u7801\u5728\u6392\u5e8f\u4e0a\u662f\u4e0d\u5b58\u5728\u4efb\u4f55\u95ee\u9898\u7684\uff0c\u56e0\u4e3a\u4e0a\u9762\u7684\u5173\u952e\u5b57\u662fint\u578b\uff0c\u5b83\u672c\u8eab\u652f\u6301\u5c0f\u4e8e\u53f7\u8fd0\u7b97\uff0c\u5728\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u6bd4\u5982\u5173\u952e\u5b57\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\uff0c\u6d89\u53ca\u5230\u6392\u5e8f\u5c31\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u5c0f\u4e8e\u53f7\u64cd\u4f5c\uff0cinsert\u7b49\u51fd\u6570\u5728\u7f16\u8bd1\u7684\u65f6\u5019\u8fc7\u4e0d\u53bb\uff0c\u4e0b\u9762\u7ed9\u51fa\u4e24\u4e2a\u65b9\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898<\/p>\n<p>\u7b2c\u4e00\u79cd\uff1a\u5c0f\u4e8e\u53f7\u91cd\u8f7d\uff0c\u7a0b\u5e8f\u4e3e\u4f8b<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Typedef struct tagStudentInfo<\/p>\n<p>{<\/p>\n<p>Int\u00a0\u00a0\u00a0\u00a0\u00a0 nID;<\/p>\n<p>String\u00a0\u00a0 strName;<\/p>\n<p>}StudentInfo, *PStudentInfo;\u00a0 \/\/\u5b66\u751f\u4fe1\u606f<\/p>\n<p>&nbsp;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>int nSize;<\/p>\n<p>\/\/\u7528\u5b66\u751f\u4fe1\u606f\u6620\u5c04\u5206\u6570<\/p>\n<p>map&lt;StudentInfo, int&gt;mapStudent;<\/p>\n<p>map&lt;StudentInfo, int&gt;::iterator iter;<\/p>\n<p>StudentInfo studentInfo;<\/p>\n<p>studentInfo.nID = 1;<\/p>\n<p>studentInfo.strName = \u201cstudent_one\u201d;<\/p>\n<p>mapStudent.insert(pair&lt;StudentInfo, int&gt;(studentInfo, 90));<\/p>\n<p>studentInfo.nID = 2;<\/p>\n<p>studentInfo.strName = \u201cstudent_two\u201d;<\/p>\n<p>mapStudent.insert(pair&lt;StudentInfo, int&gt;(studentInfo, 80));<\/p>\n<p>&nbsp;<\/p>\n<p>for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)<\/p>\n<p>cout&lt;&lt;iter-&gt;first.nID&lt;&lt;endl&lt;&lt;iter-&gt;first.strName&lt;&lt;endl&lt;&lt;iter-&gt;second&lt;&lt;endl;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u662f\u65e0\u6cd5\u7f16\u8bd1\u901a\u8fc7\u7684\uff0c\u53ea\u8981\u91cd\u8f7d\u5c0f\u4e8e\u53f7\uff0c\u5c31OK\u4e86\uff0c\u5982\u4e0b\uff1a<\/p>\n<p>Typedef struct tagStudentInfo<\/p>\n<p>{<\/p>\n<p>Int\u00a0\u00a0\u00a0\u00a0\u00a0 nID;<\/p>\n<p>String\u00a0\u00a0 strName;<\/p>\n<p>Bool operator &lt; (tagStudentInfo const&amp; _A) const<\/p>\n<p>{<\/p>\n<p>\/\/\u8fd9\u4e2a\u51fd\u6570\u6307\u5b9a\u6392\u5e8f\u7b56\u7565\uff0c\u6309nID\u6392\u5e8f\uff0c\u5982\u679cnID\u76f8\u7b49\u7684\u8bdd\uff0c\u6309strName\u6392\u5e8f<\/p>\n<p>If(nID &lt; _A.nID)\u00a0 return true;<\/p>\n<p>If(nID == _A.nID) return strName.compare(_A.strName) &lt; 0;<\/p>\n<p>Return false;<\/p>\n<p>}<\/p>\n<p>}StudentInfo, *PStudentInfo;\u00a0 \/\/\u5b66\u751f\u4fe1\u606f<\/p>\n<p>\u7b2c\u4e8c\u79cd\uff1a\u4eff\u51fd\u6570\u7684\u5e94\u7528\uff0c\u8fd9\u4e2a\u65f6\u5019\u7ed3\u6784\u4f53\u4e2d\u6ca1\u6709\u76f4\u63a5\u7684\u5c0f\u4e8e\u53f7\u91cd\u8f7d\uff0c\u7a0b\u5e8f\u8bf4\u660e<\/p>\n<p>#include &lt;map&gt;<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>Using namespace std;<\/p>\n<p>Typedef struct tagStudentInfo<\/p>\n<p>{<\/p>\n<p>Int\u00a0\u00a0\u00a0\u00a0\u00a0 nID;<\/p>\n<p>String\u00a0\u00a0 strName;<\/p>\n<p>}StudentInfo, *PStudentInfo;\u00a0 \/\/\u5b66\u751f\u4fe1\u606f<\/p>\n<p>&nbsp;<\/p>\n<p>Classs sort<\/p>\n<p>{<\/p>\n<p>Public:<\/p>\n<p>Bool operator() (StudentInfo const &amp;_A, StudentInfo const &amp;_B) const<\/p>\n<p>{<\/p>\n<p>If(_A.nID &lt; _B.nID) return true;<\/p>\n<p>If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) &lt; 0;<\/p>\n<p>Return false;<\/p>\n<p>}<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>Int main()<\/p>\n<p>{<\/p>\n<p>\/\/\u7528\u5b66\u751f\u4fe1\u606f\u6620\u5c04\u5206\u6570<\/p>\n<p>Map&lt;StudentInfo, int, sort&gt;mapStudent;<\/p>\n<p>StudentInfo studentInfo;<\/p>\n<p>studentInfo.nID = 1;<\/p>\n<p>studentInfo.strName = \u201cstudent_one\u201d;<\/p>\n<p>mapStudent.insert(pair&lt;StudentInfo, int&gt;(studentInfo, 90));<\/p>\n<p>studentInfo.nID = 2;<\/p>\n<p>studentInfo.strName = \u201cstudent_two\u201d;<\/p>\n<p>mapStudent.insert(pair&lt;StudentInfo, int&gt;(studentInfo, 80));<\/p>\n<p>}<\/p>\n<p>10.\u00a0\u00a0 \u53e6\u5916<\/p>\n<p>\u7531\u4e8eSTL\u662f\u4e00\u4e2a\u7edf\u4e00\u7684\u6574\u4f53\uff0cmap\u7684\u5f88\u591a\u7528\u6cd5\u90fd\u548cSTL\u4e2d\u5176\u5b83\u7684\u4e1c\u897f\u7ed3\u5408\u5728\u4e00\u8d77\uff0c\u6bd4\u5982\u5728\u6392\u5e8f\u4e0a\uff0c\u8fd9\u91cc\u9ed8\u8ba4\u7528\u7684\u662f\u5c0f\u4e8e\u53f7\uff0c\u5373less&lt;&gt;\uff0c\u5982\u679c\u8981\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\u5462\uff0c\u8fd9\u91cc\u6d89\u53ca\u5230\u7684\u4e1c\u897f\u5f88\u591a\uff0c\u5728\u6b64\u65e0\u6cd5\u4e00\u4e00\u52a0\u4ee5\u8bf4\u660e\u3002<\/p>\n<p>\u8fd8\u8981\u8bf4\u660e\u7684\u662f\uff0cmap\u4e2d\u7531\u4e8e\u5b83\u5185\u90e8\u6709\u5e8f\uff0c\u7531\u7ea2\u9ed1\u6811\u4fdd\u8bc1\uff0c\u56e0\u6b64\u5f88\u591a\u51fd\u6570\u6267\u884c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662flog2N\u7684\uff0c\u5982\u679c\u7528map\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u800cSTL\u00a0 Algorithm\u4e5f\u53ef\u4ee5\u5b8c\u6210\u8be5\u529f\u80fd\uff0c\u5efa\u8bae\u7528map\u81ea\u5e26\u51fd\u6570\uff0c\u6548\u7387\u9ad8\u4e00\u4e9b\u3002<\/p>\n<p>\u4e0b\u9762\u8bf4\u4e0b\uff0cmap\u5728\u7a7a\u95f4\u4e0a\u7684\u7279\u6027\uff0c\u5426\u5219\uff0c\u4f30\u8ba1\u4f60\u7528\u8d77\u6765\u4f1a\u6709\u65f6\u5019\u8868\u73b0\u7684\u6bd4\u8f83\u90c1\u95f7\uff0c\u7531\u4e8emap\u7684\u6bcf\u4e2a\u6570\u636e\u5bf9\u5e94\u7ea2\u9ed1\u6811\u4e0a\u7684\u4e00\u4e2a\u8282\u70b9\uff0c\u8fd9\u4e2a\u8282\u70b9\u5728\u4e0d\u4fdd\u5b58\u4f60\u7684\u6570\u636e\u65f6\uff0c\u662f\u5360\u752816\u4e2a\u5b57\u8282\u7684\uff0c\u4e00\u4e2a\u7236\u8282\u70b9\u6307\u9488\uff0c\u5de6\u53f3\u5b69\u5b50\u6307\u9488\uff0c\u8fd8\u6709\u4e00\u4e2a\u679a\u4e3e\u503c\uff08\u6807\u793a\u7ea2\u9ed1\u7684\uff0c\u76f8\u5f53\u4e8e\u5e73\u8861\u4e8c\u53c9\u6811\u4e2d\u7684\u5e73\u8861\u56e0\u5b50\uff09\uff0c\u6211\u60f3\u5927\u5bb6\u5e94\u8be5\u77e5\u9053\uff0c\u8fd9\u4e9b\u5730\u65b9\u5f88\u8d39\u5185\u5b58\u4e86\u5427\uff0c\u4e0d\u8bf4\u4e86\u2026\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8212;&#8212;&#8212;&#8212;&#8212;-\u6765\u81ea\u70d9\u5370\u7a7a\u95f4&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Map\u662fc++\u7684\u4e00\u4e2a\u6807\u51c6\u5bb9\u5668\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u597d\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u5728\u4e00\u4e9b\u7a0b\u5e8f\u4e2d\u5efa\u7acb\u4e00\u4e2amap\u53ef\u4ee5\u8d77\u5230\u4e8b\u534a\u529f\u500d\u7684\u6548\u679c\uff0c\u603b\u7ed3\u4e86\u4e00\u4e9bmap\u57fa\u672c\u7b80\u5355\u5b9e\u7528\u7684\u64cd\u4f5c\uff01 1. map\u6784\u9020\u51fd\u6570\uff1b map&lt;string , int &gt;mapstring; map&lt;int ,string &gt;mapint; map&lt;sring, char&gt;mapstring; map&lt; char ,string&gt;mapchar; map&lt;char ,int&gt;mapchar; map&lt;int ,char &gt;mapint; 2. map\u6dfb\u52a0\u6570\u636e\uff1b map&lt;int ,string&gt; maplive; 1.maplive.insert(pair&lt;int,string&gt;(102,&#8221;aclive&#8221;)); 2.maplive.insert(map&lt;int,string&gt;::value_type(321,&#8221;hai&#8221;)); 3, maplive[112]=&#8221;April&#8221;;\/\/map\u4e2d\u6700\u7b80\u5355\u6700\u5e38\u7528\u7684\u63d2\u5165\u6dfb\u52a0\uff01 3\uff0cmap\u4e2d\u5143\u7d20\u7684\u67e5\u627e\uff1a find()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u6307\u5411\u952e\u503c\u4e3akey\u7684\u5143\u7d20\uff0c\u5982\u679c\u6ca1\u627e\u5230\u5c31\u8fd4\u56de\u6307\u5411map\u5c3e\u90e8\u7684\u8fed\u4ee3\u5668\u3002 map&lt;int ,string &gt;::iterator l_it;; l_it=maplive.find(112); if(l_it==maplive.end()) cout&lt;&lt;&#8220;we do not find 112&#8243;&lt;&lt;endl; else cout&lt;&lt;&#8220;wo find 112&#8243;&lt;&lt;endl; 4,map\u4e2d\u5143\u7d20\u7684\u5220\u9664\uff1a \u5982\u679c\u5220\u9664112\uff1b map&lt;int ,string &gt;::iterator l_it;; l_it=maplive.find(112); if(l_it==maplive.end()) &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.xiunian.wang\/?p=304\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;c++ map\u7684\u4f7f\u7528\u65b9\u6cd5&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/304"}],"collection":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":0,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}