associative.html revision 1.1
1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter��17.��Associative</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="containers.html" title="Part��VII.�� Containers" /><link rel="prev" href="vector.html" title="vector" /><link rel="next" href="bitset.html" title="bitset" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter��17.��Associative</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="vector.html">Prev</a>��</td><th width="60%" align="center">Part��VII.�� 4 Containers 5 6</th><td width="20%" align="right">��<a accesskey="n" href="bitset.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter��17.��Associative"><div class="titlepage"><div><div><h2 class="title"><a id="manual.containers.associative"></a>Chapter��17.��Associative</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="associative.html#containers.associative.insert_hints">Insertion Hints</a></span></dt><dt><span class="sect1"><a href="bitset.html">bitset</a></span></dt><dd><dl><dt><span class="sect2"><a href="bitset.html#associative.bitset.size_variable">Size Variable</a></span></dt><dt><span class="sect2"><a href="bitset.html#associative.bitset.type_string">Type String</a></span></dt></dl></dd></dl></div><div class="sect1" title="Insertion Hints"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="containers.associative.insert_hints"></a>Insertion Hints</h2></div></div></div><p> 7 Section [23.1.2], Table 69, of the C++ standard lists this 8 function for all of the associative containers (map, set, etc): 9 </p><pre class="programlisting"> 10 a.insert(p,t); 11 </pre><p> 12 where 'p' is an iterator into the container 'a', and 't' is the 13 item to insert. The standard says that <span class="quote">���<span class="quote"><code class="code">t</code> is 14 inserted as close as possible to the position just prior to 15 <code class="code">p</code>.</span>���</span> (Library DR #233 addresses this topic, 16 referring to <a class="ulink" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1780.html" target="_top">N1780</a>. 17 Since version 4.2 GCC implements the resolution to DR 233, so 18 that insertions happen as close as possible to the hint. For 19 earlier releases the hint was only used as described below. 20 </p><p> 21 Here we'll describe how the hinting works in the libstdc++ 22 implementation, and what you need to do in order to take 23 advantage of it. (Insertions can change from logarithmic 24 complexity to amortized constant time, if the hint is properly 25 used.) Also, since the current implementation is based on the 26 SGI STL one, these points may hold true for other library 27 implementations also, since the HP/SGI code is used in a lot of 28 places. 29 </p><p> 30 In the following text, the phrases <span class="emphasis"><em>greater 31 than</em></span> and <span class="emphasis"><em>less than</em></span> refer to the 32 results of the strict weak ordering imposed on the container by 33 its comparison object, which defaults to (basically) 34 <span class="quote">���<span class="quote"><</span>���</span>. Using those phrases is semantically sloppy, 35 but I didn't want to get bogged down in syntax. I assume that if 36 you are intelligent enough to use your own comparison objects, 37 you are also intelligent enough to assign <span class="quote">���<span class="quote">greater</span>���</span> 38 and <span class="quote">���<span class="quote">lesser</span>���</span> their new meanings in the next 39 paragraph. *grin* 40 </p><p> 41 If the <code class="code">hint</code> parameter ('p' above) is equivalent to: 42 </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p> 43 <code class="code">begin()</code>, then the item being inserted should 44 have a key less than all the other keys in the container. 45 The item will be inserted at the beginning of the container, 46 becoming the new entry at <code class="code">begin()</code>. 47 </p></li><li class="listitem"><p> 48 <code class="code">end()</code>, then the item being inserted should have 49 a key greater than all the other keys in the container. The 50 item will be inserted at the end of the container, becoming 51 the new entry before <code class="code">end()</code>. 52 </p></li><li class="listitem"><p> 53 neither <code class="code">begin()</code> nor <code class="code">end()</code>, then: 54 Let <code class="code">h</code> be the entry in the container pointed to 55 by <code class="code">hint</code>, that is, <code class="code">h = *hint</code>. Then 56 the item being inserted should have a key less than that of 57 <code class="code">h</code>, and greater than that of the item preceding 58 <code class="code">h</code>. The new item will be inserted between 59 <code class="code">h</code> and <code class="code">h</code>'s predecessor. 60 </p></li></ul></div><p> 61 For <code class="code">multimap</code> and <code class="code">multiset</code>, the 62 restrictions are slightly looser: <span class="quote">���<span class="quote">greater than</span>���</span> 63 should be replaced by <span class="quote">���<span class="quote">not less than</span>���</span>and <span class="quote">���<span class="quote">less 64 than</span>���</span> should be replaced by <span class="quote">���<span class="quote">not greater 65 than.</span>���</span> (Why not replace greater with 66 greater-than-or-equal-to? You probably could in your head, but 67 the mathematicians will tell you that it isn't the same thing.) 68 </p><p> 69 If the conditions are not met, then the hint is not used, and the 70 insertion proceeds as if you had called <code class="code"> a.insert(t) 71 </code> instead. (<span class="emphasis"><em>Note </em></span> that GCC releases 72 prior to 3.0.2 had a bug in the case with <code class="code">hint == 73 begin()</code> for the <code class="code">map</code> and <code class="code">set</code> 74 classes. You should not use a hint argument in those releases.) 75 </p><p> 76 This behavior goes well with other containers' 77 <code class="code">insert()</code> functions which take an iterator: if used, 78 the new item will be inserted before the iterator passed as an 79 argument, same as the other containers. 80 </p><p> 81 <span class="emphasis"><em>Note </em></span> also that the hint in this 82 implementation is a one-shot. The older insertion-with-hint 83 routines check the immediately surrounding entries to ensure that 84 the new item would in fact belong there. If the hint does not 85 point to the correct place, then no further local searching is 86 done; the search begins from scratch in logarithmic time. 87 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="vector.html">Prev</a>��</td><td width="20%" align="center"><a accesskey="u" href="containers.html">Up</a></td><td width="40%" align="right">��<a accesskey="n" href="bitset.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">vector��</td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top">��bitset</td></tr></table></div></body></html> 88