64 lines
5.1 KiB
HTML
64 lines
5.1 KiB
HTML
<!--
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
contributor license agreements. See the NOTICE file distributed
|
|
with this work for additional information regarding copyright
|
|
ownership. The ASF licenses this file to you under the Apache
|
|
License, Version 2.0 (the License); you may not use this file
|
|
except in compliance with the License. You may obtain a copy of
|
|
the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
implied. See the License for the specific language governing
|
|
permissions and limitations under the License.
|
|
|
|
Copyright 1999-2007 Rogue Wave Software, Inc.
|
|
-->
|
|
|
|
<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 <map>
|
|
</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>
|