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"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Associative</title><meta name="generator" content="DocBook XSL-NS Stylesheets V1.78.1" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><link rel="home" href="../index.html" title="The GNU C++ Library" /><link rel="up" href="containers.html" title="Chapter 9.  Containers" /><link rel="prev" href="containers.html" title="Chapter 9.  Containers" /><link rel="next" href="unordered_associative.html" title="Unordered Associative" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Associative</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="containers.html">Prev</a> </td><th width="60%" align="center">Chapter 9. 
3  Containers
4  
5</th><td width="20%" align="right"> <a accesskey="n" href="unordered_associative.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="std.containers.associative"></a>Associative</h2></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="containers.associative.insert_hints"></a>Insertion Hints</h3></div></div></div><p>
6     Section [23.1.2], Table 69, of the C++ standard lists this
7     function for all of the associative containers (map, set, etc):
8   </p><pre class="programlisting">
9      a.insert(p,t);
10   </pre><p>
11     where 'p' is an iterator into the container 'a', and 't' is the
12     item to insert.  The standard says that <span class="quote">“<span class="quote"><code class="code">t</code> is
13     inserted as close as possible to the position just prior to
14     <code class="code">p</code>.</span>”</span> (Library DR #233 addresses this topic,
15     referring to <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1780.html" target="_top">N1780</a>.
16     Since version 4.2 GCC implements the resolution to DR 233, so
17     that insertions happen as close as possible to the hint. For
18     earlier releases the hint was only used as described below.
19   </p><p>
20     Here we'll describe how the hinting works in the libstdc++
21     implementation, and what you need to do in order to take
22     advantage of it.  (Insertions can change from logarithmic
23     complexity to amortized constant time, if the hint is properly
24     used.)  Also, since the current implementation is based on the
25     SGI STL one, these points may hold true for other library
26     implementations also, since the HP/SGI code is used in a lot of
27     places.
28   </p><p>
29     In the following text, the phrases <span class="emphasis"><em>greater
30     than</em></span> and <span class="emphasis"><em>less than</em></span> refer to the
31     results of the strict weak ordering imposed on the container by
32     its comparison object, which defaults to (basically)
33     <span class="quote">“<span class="quote">&lt;</span>”</span>.  Using those phrases is semantically sloppy,
34     but I didn't want to get bogged down in syntax.  I assume that if
35     you are intelligent enough to use your own comparison objects,
36     you are also intelligent enough to assign <span class="quote">“<span class="quote">greater</span>”</span>
37     and <span class="quote">“<span class="quote">lesser</span>”</span> their new meanings in the next
38     paragraph.  *grin*
39   </p><p>
40     If the <code class="code">hint</code> parameter ('p' above) is equivalent to:
41   </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
42	  <code class="code">begin()</code>, then the item being inserted should
43	  have a key less than all the other keys in the container.
44	  The item will be inserted at the beginning of the container,
45	  becoming the new entry at <code class="code">begin()</code>.
46      </p></li><li class="listitem"><p>
47	  <code class="code">end()</code>, then the item being inserted should have
48	  a key greater than all the other keys in the container.  The
49	  item will be inserted at the end of the container, becoming
50	  the new entry before <code class="code">end()</code>.
51      </p></li><li class="listitem"><p>
52	  neither <code class="code">begin()</code> nor <code class="code">end()</code>, then:
53	  Let <code class="code">h</code> be the entry in the container pointed to
54	  by <code class="code">hint</code>, that is, <code class="code">h = *hint</code>.  Then
55	  the item being inserted should have a key less than that of
56	  <code class="code">h</code>, and greater than that of the item preceding
57	  <code class="code">h</code>.  The new item will be inserted between
58	  <code class="code">h</code> and <code class="code">h</code>'s predecessor.
59	  </p></li></ul></div><p>
60     For <code class="code">multimap</code> and <code class="code">multiset</code>, the
61     restrictions are slightly looser: <span class="quote">“<span class="quote">greater than</span>”</span>
62     should be replaced by <span class="quote">“<span class="quote">not less than</span>”</span>and <span class="quote">“<span class="quote">less
63     than</span>”</span> should be replaced by <span class="quote">“<span class="quote">not greater
64     than.</span>”</span> (Why not replace greater with
65     greater-than-or-equal-to?  You probably could in your head, but
66     the mathematicians will tell you that it isn't the same thing.)
67   </p><p>
68     If the conditions are not met, then the hint is not used, and the
69     insertion proceeds as if you had called <code class="code"> a.insert(t)
70     </code> instead.  (<span class="emphasis"><em>Note </em></span> that GCC releases
71     prior to 3.0.2 had a bug in the case with <code class="code">hint ==
72     begin()</code> for the <code class="code">map</code> and <code class="code">set</code>
73     classes.  You should not use a hint argument in those releases.)
74   </p><p>
75     This behavior goes well with other containers'
76     <code class="code">insert()</code> functions which take an iterator: if used,
77     the new item will be inserted before the iterator passed as an
78     argument, same as the other containers.
79   </p><p>
80     <span class="emphasis"><em>Note </em></span> also that the hint in this
81     implementation is a one-shot.  The older insertion-with-hint
82     routines check the immediately surrounding entries to ensure that
83     the new item would in fact belong there.  If the hint does not
84     point to the correct place, then no further local searching is
85     done; the search begins from scratch in logarithmic time.
86   </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="containers.associative.bitset"></a>bitset</h3></div></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="associative.bitset.size_variable"></a>Size Variable</h4></div></div></div><p>
87	No, you cannot write code of the form
88      </p><pre class="programlisting">
89      #include &lt;bitset&gt;
90
91      void foo (size_t n)
92      {
93	  std::bitset&lt;n&gt;   bits;
94	  ....
95      }
96   </pre><p>
97     because <code class="code">n</code> must be known at compile time.  Your
98     compiler is correct; it is not a bug.  That's the way templates
99     work.  (Yes, it <span class="emphasis"><em>is</em></span> a feature.)
100   </p><p>
101     There are a couple of ways to handle this kind of thing.  Please
102     consider all of them before passing judgement.  They include, in
103     no particular order:
104   </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>A very large N in <code class="code">bitset&lt;N&gt;</code>.</p></li><li class="listitem"><p>A container&lt;bool&gt;.</p></li><li class="listitem"><p>Extremely weird solutions.</p></li></ul></div><p>
105     <span class="emphasis"><em>A very large N in
106     <code class="code">bitset&lt;N&gt;</code>.  </em></span> It has been
107     pointed out a few times in newsgroups that N bits only takes up
108     (N/8) bytes on most systems, and division by a factor of eight is
109     pretty impressive when speaking of memory.  Half a megabyte given
110     over to a bitset (recall that there is zero space overhead for
111     housekeeping info; it is known at compile time exactly how large
112     the set is) will hold over four million bits.  If you're using
113     those bits as status flags (e.g.,
114     <span class="quote">“<span class="quote">changed</span>”</span>/<span class="quote">“<span class="quote">unchanged</span>”</span> flags), that's a
115     <span class="emphasis"><em>lot</em></span> of state.
116   </p><p>
117     You can then keep track of the <span class="quote">“<span class="quote">maximum bit used</span>”</span>
118     during some testing runs on representative data, make note of how
119     many of those bits really need to be there, and then reduce N to
120     a smaller number.  Leave some extra space, of course.  (If you
121     plan to write code like the incorrect example above, where the
122     bitset is a local variable, then you may have to talk your
123     compiler into allowing that much stack space; there may be zero
124     space overhead, but it's all allocated inside the object.)
125   </p><p>
126     <span class="emphasis"><em>A container&lt;bool&gt;.  </em></span> The
127     Committee made provision for the space savings possible with that
128     (N/8) usage previously mentioned, so that you don't have to do
129     wasteful things like <code class="code">Container&lt;char&gt;</code> or
130     <code class="code">Container&lt;short int&gt;</code>.  Specifically,
131     <code class="code">vector&lt;bool&gt;</code> is required to be specialized for
132     that space savings.
133   </p><p>
134     The problem is that <code class="code">vector&lt;bool&gt;</code> doesn't
135     behave like a normal vector anymore.  There have been
136     journal articles which discuss the problems (the ones by Herb
137     Sutter in the May and July/August 1999 issues of C++ Report cover
138     it well).  Future revisions of the ISO C++ Standard will change
139     the requirement for <code class="code">vector&lt;bool&gt;</code>
140     specialization.  In the meantime, <code class="code">deque&lt;bool&gt;</code>
141     is recommended (although its behavior is sane, you probably will
142     not get the space savings, but the allocation scheme is different
143     than that of vector).
144   </p><p>
145     <span class="emphasis"><em>Extremely weird solutions.  </em></span> If
146     you have access to the compiler and linker at runtime, you can do
147     something insane, like figuring out just how many bits you need,
148     then writing a temporary source code file.  That file contains an
149     instantiation of <code class="code">bitset</code> for the required number of
150     bits, inside some wrapper functions with unchanging signatures.
151     Have your program then call the compiler on that file using
152     Position Independent Code, then open the newly-created object
153     file and load those wrapper functions.  You'll have an
154     instantiation of <code class="code">bitset&lt;N&gt;</code> for the exact
155     <code class="code">N</code> that you need at the time.  Don't forget to delete
156     the temporary files.  (Yes, this <span class="emphasis"><em>can</em></span> be, and
157     <span class="emphasis"><em>has been</em></span>, done.)
158   </p><p>
159     This would be the approach of either a visionary genius or a
160     raving lunatic, depending on your programming and management
161     style.  Probably the latter.
162   </p><p>
163     Which of the above techniques you use, if any, are up to you and
164     your intended application.  Some time/space profiling is
165     indicated if it really matters (don't just guess).  And, if you
166     manage to do anything along the lines of the third category, the
167     author would love to hear from you...
168   </p><p>
169     Also note that the implementation of bitset used in libstdc++ has
170     <a class="link" href="ext_containers.html#manual.ext.containers.sgi" title="Backwards Compatibility">some extensions</a>.
171   </p></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="associative.bitset.type_string"></a>Type String</h4></div></div></div><p>
172      </p><p>
173     Bitmasks do not take char* nor const char* arguments in their
174     constructors.  This is something of an accident, but you can read
175     about the problem: follow the library's <span class="quote">“<span class="quote">Links</span>”</span> from
176     the homepage, and from the C++ information <span class="quote">“<span class="quote">defect
177     reflector</span>”</span> link, select the library issues list.  Issue
178     number 116 describes the problem.
179   </p><p>
180     For now you can simply make a temporary string object using the
181     constructor expression:
182   </p><pre class="programlisting">
183      std::bitset&lt;5&gt; b ( std::string("10110") );
184   </pre><p>
185     instead of
186   </p><pre class="programlisting">
187      std::bitset&lt;5&gt; b ( "10110" );    // invalid
188    </pre></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="containers.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="unordered_associative.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 9. 
189  Containers
190  
191 </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Unordered Associative</td></tr></table></div></body></html>