blob: 5d7b26c44adb81b1b1cf3b097b02cb7804563e59 [file] [log] [blame]
<HTML>
<HEAD>
<TITLE>The map Data Abstraction</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="9.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="9-2.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library User's Guide</B></DIV>
<H2>9.1 The map Data Abstraction</H2>
<A NAME="idx163"><!></A>
<P>A <B><I><A HREF="../stdlibref/map.html">map</A></I></B> is an indexed data structure, similar to a <B><I><A HREF="../stdlibref/vector.html">vector</A></I></B> or a <B><I><A HREF="../stdlibref/deque.html">deque</A></I></B>. However, a <B><I>map</I></B> differs from a <B><I>vector</I></B> or <B><I>deque</I></B> in two important respects:</P>
<UL>
<LI><P CLASS="LIST">First, in a <B><I><A HREF="../stdlibref/map.html">map</A></I></B> the index values or<I> key values</I> need not be <SAMP>int</SAMP>, but can be any ordered datatype. For example, a <B><I>map</I></B> can be indexed by real numbers, or by strings. Any datatype for which a comparison operator can be defined can be used as a key. As with a <B><I><A HREF="../stdlibref/vector.html">vector</A></I></B> or <B><I><A HREF="../stdlibref/deque.html">deque</A></I></B>, elements can be accessed through the subscript operator or other techniques. </P></LI>
<LI><P CLASS="LIST">Second, a <B><I><A HREF="../stdlibref/map.html">map</A></I></B> is an ordered data structure. This means that elements are maintained in sequence, the ordering determined by key values. Because <B><I>map</I></B>s maintain values in order, they can very rapidly find the element specified by any given key. Searching is performed in logarithmic time. Like a <B><I><A HREF="../stdlibref/list.html">list</A></I></B>, maps are not limited in size, but expand or contract as necessary as new elements are added or removed. In large part, a <B><I>map</I></B> can simply be considered a <B><I><A HREF="../stdlibref/set.html">set</A></I></B> that maintains a collection of pairs.</P></LI>
</UL>
<P>In other programming languages, a map-like data structure is sometimes referred to as a dictionary, a table, or an associative array. In the C++ Standard Library, there are two varieties of maps:</P>
<UL>
<LI><P CLASS="LIST">The <B><I><A HREF="../stdlibref/map.html">map</A></I></B> data structure demands unique keys; that is, there is a one-to-one association between key elements and their corresponding values. In a <B><I>map</I></B>, the insertion of a new value that uses an existing key is ignored. </P></LI>
<A NAME="idx164"><!></A>
<LI><P CLASS="LIST">The <B><I><A HREF="../stdlibref/multimap.html">multimap</A></I></B> permits multiple different entries to be indexed by the same key. </P></LI>
</UL>
<P>Both data structures provide relatively fast insertion, deletion, and access operations in logarithmic time. </P>
<A NAME="911"><H3>9.1.1 Include files</H3></A>
<A NAME="idx165"><!></A>
<P>Whenever you use a <B><I><A HREF="../stdlibref/map.html">map</A></I></B> or a <B><I><A HREF="../stdlibref/multimap.html">multimap</A></I></B>, you must include the <SAMP>map</SAMP> header file.</P>
<UL><PRE>
#include &lt;map&gt;
</PRE></UL>
<BR>
<HR>
<A HREF="9.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="9-2.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>