﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Next.-随笔分类-STL 学习</title><link>http://www.cppblog.com/bersaty/category/18283.html</link><description>生来介为忙一场</description><language>zh-cn</language><lastBuildDate>Sat, 03 Dec 2011 04:15:20 GMT</lastBuildDate><pubDate>Sat, 03 Dec 2011 04:15:20 GMT</pubDate><ttl>60</ttl><item><title>STL（map）</title><link>http://www.cppblog.com/bersaty/archive/2011/12/03/161349.html</link><dc:creator>bersaty</dc:creator><author>bersaty</author><pubDate>Sat, 03 Dec 2011 04:07:00 GMT</pubDate><guid>http://www.cppblog.com/bersaty/archive/2011/12/03/161349.html</guid><wfw:comment>http://www.cppblog.com/bersaty/comments/161349.html</wfw:comment><comments>http://www.cppblog.com/bersaty/archive/2011/12/03/161349.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/bersaty/comments/commentRss/161349.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/bersaty/services/trackbacks/161349.html</trackback:ping><description><![CDATA[发现做ACM题用STL 就像开挂，很多写了一堆代码的东西一下就KO了，不过还是要学习本质的东西~<br />PS：C++手册真好用~<br />
<p><strong>Map</strong></p>
<p>Maps are a kind of associative containers that stores elements formed by the combination of a <em>key value</em> and a <em>mapped value</em>. 
<p>In a <tt>map</tt>, the <em>key value</em> is generally used to uniquely identify the element, while the <em>mapped value</em> is some sort of value associated to this <em>key</em>. Types of <em>key</em> and <em>mapped</em> value may differ. For example, a typical example of a map is a telephone guide where the name is the <em>key</em> and the telephone number is the <em>mapped value</em>. 
<p>Internally, the elements in the map are sorted from lower to higher <em>key value</em> following a specific strict weak ordering criterion set on construction. 
<p>As associative containers, they are especially designed to be efficient accessing its elements by their <em>key</em> (unlike sequence containers, which are more efficient accessing elements by their relative or absolute position). 
<p>Therefore, the main characteristics of a <tt>map</tt> as an associative container are:<br />
<ul><li>Unique key values: no two elements in the <tt>map</tt> have keys that compare equal to each other. For a similar associative container allowing for multiple elements with equivalent keys, see <a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/multimap">multimap</a>.</li><li>Each element is composed of a <em>key</em> and a <em>mapped</em> value. For a simpler associative container where the element value itself is its key, see <a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/set">set</a>.</li><li>Elements follow a strict weak ordering at all times. Unordered associative arrays, like <tt>unordered_map</tt>, are available in implementations following TR1. </li></ul>
<p>Maps are also unique among associative containers in that the implement the direct access operator (<tt>operator[]</tt>) which allows for direct access of the mapped value. 
<p>In their implementation in the C++ Standard Template Library, <tt>map</tt> containers take four template parameters:<br />
<table class="snippet">
<tbody>
<tr>
<td class="code"><pre><span class="kw">template</span> &lt; <span class="kw">class</span> Key, <span class="kw">class</span> T, <span class="kw">class</span> Compare = less&lt;Key&gt;,
           <span class="kw">class</span> Allocator = allocator&lt;pair&lt;<span class="kw">const</span> Key,T&gt; &gt; &gt; <span class="kw">class</span> map;
</pre></td></tr></tbody></table>Where the template parameters have the following meanings:<br />
<ul><li><strong><tt>Key</tt>:</strong> Type of the key values. Each element in a <tt>map</tt> is uniquely identified by its key value.</li><li><strong><tt>T</tt>:</strong> Type of the mapped value. Each element in a <tt>map</tt> is used to store some data as its mapped value.</li><li><strong><tt>Compare</tt>:</strong> Comparison class: A class that takes two arguments of the <em>key type</em> and returns a <tt>bool</tt>. The expression <tt>comp(a,b)</tt>, where <em>comp</em> is an object of this comparison class and <em>a</em> and <em>b</em> are key values, shall return <tt>true</tt> if <em>a</em> is to be placed at an earlier position than <em>b</em> in a strict weak ordering operation. This can either be a class implementing a <em>function call operator</em> or a pointer to a function (see <a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/map_3A_3Amap">constructor</a> for an example). This defaults to <tt>less&lt;Key&gt;</tt>, which returns the same as applying the <em>less-than operator</em> (<tt>a&lt;b</tt>).<br />The <tt>map</tt> object uses this expression to determine the position of the elements in the container. All elements in a <tt>map</tt> container are ordered following this rule at all times.</li><li><strong><tt>Allocator</tt>:</strong> Type of the allocator object used to define the storage allocation model. By default, the <tt>allocator</tt> class template is used, which defines the simplest memory allocation model and is value-independent. </li></ul>In the reference for the <tt>map</tt> member functions, these same names (<em>Key</em>, <em>T</em>, <em>Compare</em> and <em>Allocator</em>) are assumed for the template parameters. 
<p>This container class supports <em>bidirectional iterators</em>.<br />Iterators to elements of <tt>map</tt> containers access to both the <em>key</em> and the <em>mapped value</em>. For this, the class defines what is called its <tt>value_type</tt>, which is a <a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/pair">pair</a> class with its <tt>first</tt> value corresponding to the <tt>const</tt> version of the <em>key</em> type (template parameter <em>Key</em>) and its <tt>second</tt> value corresponding to the <em>mapped value</em> (template parameter <em>T</em>):<br />
<table class="snippet">
<tbody>
<tr>
<td class="code"><pre><span class="kw">typedef</span> pair&lt;<span class="kw">const</span> Key, T&gt; value_type;
</pre></td></tr></tbody></table>
<p>Iterators of a <tt>map</tt> container point to elements of this <tt>value_type</tt>. Thus, for an iterator called <tt>it</tt> that points to an element of a <tt>map</tt>, its key and mapped value can be accessed respectivelly with:<br />
<table class="snippet">
<tbody>
<tr>
<td class="code"><pre>map&lt;Key,T&gt;::iterator it;
(*it).first;             <span class="comm">// the key value (of type Key)</span>
(*it).second;            <span class="comm">// the mapped value (of type T)</span>
(*it);                   <span class="comm">// the "element value" (of type pair&lt;const Key,T&gt;)</span>
</pre></td></tr></tbody></table>Naturally, any other direct access operator, such as <tt>-&gt;</tt> or <tt>[]</tt> can be used, for example:<br />
<table class="snippet">
<tbody>
<tr>
<td class="code"><pre>it-&gt;first;               <span class="comm">// same as (*it).first   (the key value)</span>
it-&gt;second;              <span class="comm">// same as (*it).second  (the mapped value)</span>
</pre></td></tr></tbody></table>
<p>
<h3>Member functions</h3>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/map.html"><strong>(constructor)</strong></a></td>
<td class="des">Construct map <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/~map.html"><strong>(destructor)</strong></a></td>
<td class="des">Map destructor <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/operator=.html"><strong>operator=</strong></a></td>
<td class="des">Copy container content <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<p><strong>Iterators</strong>:<br />
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/begin.html"><strong>begin</strong></a></td>
<td class="des">Return iterator to beginning <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/end.html"><strong>end</strong></a></td>
<td class="des">Return iterator to end <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/rbegin.html"><strong>rbegin</strong></a></td>
<td class="des">Return reverse iterator to reverse beginning <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/rend.html"><strong>rend</strong></a></td>
<td class="des">Return reverse iterator to reverse end <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<p><strong>Capacity</strong>:<br />
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/empty.html"><strong>empty</strong></a></td>
<td class="des">Test whether container is empty <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/size.html"><strong>size</strong></a></td>
<td class="des">Return container size <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/max_size.html"><strong>max_size</strong></a></td>
<td class="des">Return maximum size <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<p><strong>Element access</strong>:<br />
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/operator[].html"><strong>operator[]</strong></a></td>
<td class="des">Access element <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<p><strong>Modifiers</strong>:<br />
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/insert.html"><strong>insert</strong></a></td>
<td class="des">Insert element <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/erase.html"><strong>erase</strong></a></td>
<td class="des">Erase elements <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/swap.html"><strong>swap</strong></a></td>
<td class="des">Swap content <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/clear.html"><strong>clear</strong></a></td>
<td class="des">Clear content <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<p><strong>Observers</strong>:<br />
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/key_comp.html"><strong>key_comp</strong></a></td>
<td class="des">Return key comparison object <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/value_comp.html"><strong>value_comp</strong></a></td>
<td class="des">Return value comparison object <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<p><strong>Operations</strong>:<br />
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/find.html"><strong>find</strong></a></td>
<td class="des">Get iterator to element <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/count.html"><strong>count</strong></a></td>
<td class="des">Count elements with a specific key <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/lower_bound.html"><strong>lower_bound</strong></a></td>
<td class="des">Return iterator to lower bound <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/upper_bound.html"><strong>upper_bound</strong></a></td>
<td class="des">Return iterator to upper bound <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/equal_range.html"><strong>equal_range</strong></a></td>
<td class="des">Get range of equal elements <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<p><strong>Allocator</strong>:<br />
<table class="list">
<tbody>
<tr>
<td class="tit"><a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/reference/stl/map/get_allocator.html"><strong>get_allocator</strong></a></td>
<td class="des">Get allocator <small><font color="green">(public member function)</font></small></td></tr></tbody></table>
<p>
<h3>Member types</h3>of <tt>template &lt;class Key, class T, class Compare=less&lt;Key&gt;, class Allocator=allocator&lt;pair &lt;const Key, T&gt; &gt; &gt; class map;</tt> 
<p>
<table class="boxed">
<tbody>
<tr>
<th>member type</th>
<th>definition</th></tr>
<tr>
<td><tt>key_type</tt></td>
<td><tt>Key</tt></td></tr>
<tr>
<td><tt>mapped_type</tt></td>
<td><tt>T</tt></td></tr>
<tr>
<td><tt>value_type</tt></td>
<td><tt>pair&lt;const Key,T&gt;</tt></td></tr>
<tr>
<td><tt>key_compare</tt></td>
<td><tt>Compare</tt></td></tr>
<tr>
<td><tt>value_compare</tt></td>
<td><em>Nested class to compare elements</em> (see member function <a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/map_3A_3Avalue_comp">value_comp</a>)</td></tr>
<tr>
<td><tt>allocator_type</tt></td>
<td><tt>Allocator</tt></td></tr>
<tr>
<td><tt>reference</tt></td>
<td><tt>Allocator::reference</tt></td></tr>
<tr>
<td><tt>const_reference</tt></td>
<td><tt>Allocator::const_reference</tt></td></tr>
<tr>
<td><tt>iterator</tt></td>
<td><em>Bidirectional iterator</em></td></tr>
<tr>
<td><tt>const_iterator</tt></td>
<td><em>Constant bidirectional iterator</em></td></tr>
<tr>
<td><tt>size_type</tt></td>
<td><em>Unsigned integral type</em> (usually same as <a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/size_t">size_t</a>)</td></tr>
<tr>
<td><tt>difference_type</tt></td>
<td><em>Signed integral type</em> (usually same as <a href="mk:@MSITStore:C:\Documents%20and%20Settings\Administrator\桌面\C++函数手册%20(LibraryFunctions).chm::/download/www.cplusplus.com/ptrdiff_t">ptrdiff_t</a>)</td></tr>
<tr>
<td><tt>pointer</tt></td>
<td><tt>Allocator::pointer</tt></td></tr>
<tr>
<td><tt>const_pointer</tt></td>
<td><tt>Allocator::const_pointer</tt></td></tr>
<tr>
<td><tt>reverse_iterator</tt></td>
<td><tt>reverse_iterator&lt;iterator&gt;</tt></td></tr>
<tr>
<td><tt>const_reverse_iterator</tt></td>
<td><tt>reverse_iterator&lt;const_iterator&gt;</tt></td></tr></tbody></table></p><img src ="http://www.cppblog.com/bersaty/aggbug/161349.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/bersaty/" target="_blank">bersaty</a> 2011-12-03 12:07 <a href="http://www.cppblog.com/bersaty/archive/2011/12/03/161349.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>