2000-04-22 04:33:34 +08:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
2001-09-18 07:24:40 +08:00
|
|
|
<html>
|
|
|
|
<head>
|
2001-10-05 03:54:54 +08:00
|
|
|
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
|
2001-09-18 07:24:40 +08:00
|
|
|
<meta NAME="AUTHOR" CONTENT="pme@gcc.gnu.org (Phil Edwards)">
|
|
|
|
<meta NAME="KEYWORDS" CONTENT="HOWTO, libstdc++, GCC, g++, libg++, STL">
|
|
|
|
<meta NAME="DESCRIPTION" CONTENT="HOWTO for the libstdc++ chapter 23.">
|
|
|
|
<meta NAME="GENERATOR" CONTENT="vi and eight fingers">
|
|
|
|
<title>libstdc++-v3 HOWTO: Chapter 23</title>
|
|
|
|
<link REL=StyleSheet HREF="../lib3styles.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<h1 CLASS="centered"><a name="top">Chapter 23: Containers</a></h1>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<p>Chapter 23 deals with container classes and what they offer.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h1>Contents</h1>
|
|
|
|
<ul>
|
|
|
|
<li><a href="#1">Making code unaware of the container/array difference</a>
|
|
|
|
<li><a href="#2">Variable-sized bitmasks</a>
|
|
|
|
<li><a href="#3">Containers and multithreading</a>
|
|
|
|
<li><a href="#4">"Hinting" during insertion</a>
|
|
|
|
<li><a href="#5">Bitmasks and string arguments</a>
|
|
|
|
</ul>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<h2><a name="1">Making code unaware of the container/array difference</a></h2>
|
|
|
|
<p>You're writing some code and can't decide whether to use builtin
|
2000-04-22 04:33:34 +08:00
|
|
|
arrays or some kind of container. There are compelling reasons
|
|
|
|
to use one of the container classes, but you're afraid that you'll
|
|
|
|
eventually run into difficulties, change everything back to arrays,
|
|
|
|
and then have to change all the code that uses those data types to
|
|
|
|
keep up with the change.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>If your code makes use of the standard algorithms, this isn't as
|
2000-04-22 04:33:34 +08:00
|
|
|
scary as it sounds. The algorithms don't know, nor care, about
|
|
|
|
the kind of "container" on which they work, since the
|
|
|
|
algorithms are only given endpoints to work with. For the container
|
2001-09-18 07:24:40 +08:00
|
|
|
classes, these are iterators (usually <code>begin()</code> and
|
|
|
|
<code>end()</code>, but not always). For builtin arrays, these are
|
2001-05-31 05:55:05 +08:00
|
|
|
the address of the first element and the
|
2001-09-18 07:24:40 +08:00
|
|
|
<a href="../24_iterators/howto.html#2">past-the-end</a> element.
|
|
|
|
</p>
|
|
|
|
<p>Some very simple wrapper functions can hide all of that from the
|
2000-04-22 04:33:34 +08:00
|
|
|
rest of the code. For example, a pair of functions called
|
2001-09-18 07:24:40 +08:00
|
|
|
<code>beginof</code> can be written, one that takes an array, another
|
2000-04-22 04:33:34 +08:00
|
|
|
that takes a vector. The first returns a pointer to the first
|
2001-09-18 07:24:40 +08:00
|
|
|
element, and the second returns the vector's <code>begin()</code>
|
2000-04-22 04:33:34 +08:00
|
|
|
iterator.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The functions should be made template functions, and should also
|
2000-04-22 04:33:34 +08:00
|
|
|
be declared inline. As pointed out in the comments in the code
|
2001-09-18 07:24:40 +08:00
|
|
|
below, this can lead to <code>beginof</code> being optimized out of
|
2000-04-22 04:33:34 +08:00
|
|
|
existence, so you pay absolutely nothing in terms of increased
|
|
|
|
code size or execution time.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The result is that if all your algorithm calls look like
|
2000-04-22 04:33:34 +08:00
|
|
|
<PRE>
|
|
|
|
std::transform(beginof(foo), endof(foo), beginof(foo), SomeFunction);</PRE>
|
|
|
|
then the type of foo can change from an array of ints to a vector
|
|
|
|
of ints to a deque of ints and back again, without ever changing any
|
|
|
|
client code.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>This author has a collection of such functions, called "*of"
|
2000-04-22 04:33:34 +08:00
|
|
|
because they all extend the builtin "sizeof". It started
|
|
|
|
with some Usenet discussions on a transparent way to find the length
|
|
|
|
of an array. A simplified and much-reduced version for easier
|
2001-09-18 07:24:40 +08:00
|
|
|
reading is <a href="wrappers_h.txt">given here</a>.
|
|
|
|
</p>
|
|
|
|
<p>Astute readers will notice two things at once: first, that the
|
|
|
|
container class is still a <code>vector<T></code> instead of a
|
|
|
|
more general <code>Container<T></code>. This would mean that
|
|
|
|
three functions for <code>deque</code> would have to be added, another
|
|
|
|
three for <code>list</code>, and so on. This is due to problems with
|
2000-04-22 04:33:34 +08:00
|
|
|
getting template resolution correct; I find it easier just to
|
|
|
|
give the extra three lines and avoid confusion.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Second, the line
|
2000-04-22 04:33:34 +08:00
|
|
|
<PRE>
|
2001-05-31 05:55:05 +08:00
|
|
|
inline unsigned int lengthof (T (&)[sz]) { return sz; } </PRE>
|
2000-04-22 04:33:34 +08:00
|
|
|
looks just weird! Hint: unused parameters can be left nameless.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h2><a name="2">Variable-sized bitmasks</a></h2>
|
|
|
|
<p>No, you cannot write code of the form
|
2000-04-22 04:33:34 +08:00
|
|
|
<!-- Careful, the leading spaces in PRE show up directly. -->
|
|
|
|
<PRE>
|
|
|
|
#include <bitset>
|
|
|
|
|
|
|
|
void foo (size_t n)
|
|
|
|
{
|
|
|
|
std::bitset<n> bits;
|
|
|
|
....
|
|
|
|
} </PRE>
|
2001-09-18 07:24:40 +08:00
|
|
|
because <code>n</code> must be known at compile time. Your compiler is
|
2000-04-22 04:33:34 +08:00
|
|
|
correct; it is not a bug. That's the way templates work. (Yes, it
|
2001-09-18 07:24:40 +08:00
|
|
|
<em>is</em> a feature.)
|
|
|
|
</p>
|
|
|
|
<p>There are a couple of ways to handle this kind of thing. Please
|
2000-04-22 04:33:34 +08:00
|
|
|
consider all of them before passing judgement. They include, in
|
|
|
|
no particular order:
|
2001-09-18 07:24:40 +08:00
|
|
|
<ul>
|
|
|
|
<li>A very large N in <code>bitset<N></code>.
|
|
|
|
<li>A container<bool>.
|
|
|
|
<li>Extremely weird solutions.
|
|
|
|
</ul>
|
|
|
|
</p>
|
|
|
|
<p><B>A very large N in <code>bitset<N></code>. </B> It has
|
2000-04-22 04:33:34 +08:00
|
|
|
been pointed out a few times in newsgroups that N bits only takes up
|
|
|
|
(N/8) bytes on most systems, and division by a factor of eight is pretty
|
|
|
|
impressive when speaking of memory. Half a megabyte given over to a
|
|
|
|
bitset (recall that there is zero space overhead for housekeeping info;
|
|
|
|
it is known at compile time exactly how large the set is) will hold over
|
|
|
|
four million bits. If you're using those bits as status flags (e.g.,
|
2001-09-18 07:24:40 +08:00
|
|
|
"changed"/"unchanged" flags), that's a <em>lot</em>
|
2000-04-22 04:33:34 +08:00
|
|
|
of state.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>You can then keep track of the "maximum bit used" during some
|
2000-04-22 04:33:34 +08:00
|
|
|
testing runs on representative data, make note of how many of those bits
|
|
|
|
really need to be there, and then reduce N to a smaller number. Leave
|
|
|
|
some extra space, of course. (If you plan to write code like the
|
|
|
|
incorrect example above, where the bitset is a local variable, then you
|
|
|
|
may have to talk your compiler into allowing that much stack space;
|
2001-05-31 05:55:05 +08:00
|
|
|
there may be zero space overhead, but it's all allocated inside the
|
2000-04-22 04:33:34 +08:00
|
|
|
object.)
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p><B>A container<bool>. </B> The Committee made provision
|
2000-04-22 04:33:34 +08:00
|
|
|
for the space savings possible with that (N/8) usage previously mentioned,
|
|
|
|
so that you don't have to do wasteful things like
|
2001-09-26 07:51:17 +08:00
|
|
|
<code>Container<char></code> or
|
|
|
|
<code>Container<short int></code>.
|
2001-09-18 07:24:40 +08:00
|
|
|
Specifically, <code>vector<bool></code> is required to be
|
2000-04-22 04:33:34 +08:00
|
|
|
specialized for that space savings.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The problem is that <code>vector<bool></code> doesn't behave like a
|
2000-04-22 04:33:34 +08:00
|
|
|
normal vector anymore. There have been recent journal articles which
|
|
|
|
discuss the problems (the ones by Herb Sutter in the May and
|
|
|
|
July/August 1999 issues of
|
2001-09-18 07:24:40 +08:00
|
|
|
<em>C++ Report</em> cover it well). Future revisions of the ISO C++
|
|
|
|
Standard will change the requirement for <code>vector<bool></code>
|
|
|
|
specialization. In the meantime, <code>deque<bool></code> is
|
2000-04-22 04:33:34 +08:00
|
|
|
recommended (although its behavior is sane, you probably will not get
|
|
|
|
the space savings, but the allocation scheme is different than that
|
|
|
|
of vector).
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p><B>Extremely weird solutions. </B> If you have access to
|
2000-04-22 04:33:34 +08:00
|
|
|
the compiler and linker at runtime, you can do something insane, like
|
|
|
|
figuring out just how many bits you need, then writing a temporary
|
2001-09-26 07:51:17 +08:00
|
|
|
source code file. That file contains an instantiation of
|
|
|
|
<code>bitset</code>
|
2000-04-22 04:33:34 +08:00
|
|
|
for the required number of bits, inside some wrapper functions with
|
|
|
|
unchanging signatures. Have your program then call the
|
|
|
|
compiler on that file using Position Independant Code, then open the
|
|
|
|
newly-created object file and load those wrapper functions. You'll have
|
2001-09-26 07:51:17 +08:00
|
|
|
an instantiation of <code>bitset<N></code> for the exact
|
|
|
|
<code>N</code>
|
2000-04-22 04:33:34 +08:00
|
|
|
that you need at the time. Don't forget to delete the temporary files.
|
2001-09-18 07:24:40 +08:00
|
|
|
(Yes, this <em>can</em> be, and <em>has been</em>, done.)
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
<!-- I wonder if this next paragraph will get me in trouble... -->
|
2001-09-18 07:24:40 +08:00
|
|
|
<p>This would be the approach of either a visionary genius or a raving
|
2000-04-22 04:33:34 +08:00
|
|
|
lunatic, depending on your programming and management style. Probably
|
|
|
|
the latter.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Which of the above techniques you use, if any, are up to you and your
|
2000-04-22 04:33:34 +08:00
|
|
|
intended application. Some time/space profiling is indicated if it
|
|
|
|
really matters (don't just guess). And, if you manage to do anything
|
|
|
|
along the lines of the third category, the author would love to hear
|
|
|
|
from you...
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
2001-09-26 07:51:17 +08:00
|
|
|
<p>Also note that the implementation of bitset used in libstdc++-v3 has
|
|
|
|
<a href="../ext/sgiexts.html#ch23">some extensions</a>.
|
|
|
|
</p>
|
2001-09-18 07:24:40 +08:00
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h2><a name="3">Containers and multithreading</a></h2>
|
|
|
|
<p>This section will mention some of the problems in designing MT
|
2000-04-22 04:33:34 +08:00
|
|
|
programs that use Standard containers. For information on other
|
|
|
|
aspects of multithreading (e.g., the library as a whole), see
|
2001-06-08 11:53:35 +08:00
|
|
|
the Received Wisdom on Chapter 17. This section only applies
|
|
|
|
when gcc and libstdc++-v3 were configured with --enable-threads.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Two excellent pages to read when working with templatized containers
|
2001-05-30 16:30:04 +08:00
|
|
|
and threads are
|
2001-09-18 07:24:40 +08:00
|
|
|
<a href="http://www.sgi.com/tech/stl/thread_safety.html">SGI's
|
|
|
|
http://www.sgi.com/tech/stl/thread_safety.html</a> and
|
|
|
|
<a href="http://www.sgi.com/tech/stl/Allocators.html">SGI's
|
|
|
|
http://www.sgi.com/tech/stl/Allocators.html</a>. The
|
2000-04-22 04:33:34 +08:00
|
|
|
libstdc++-v3 uses the same definition of thread safety
|
|
|
|
when discussing design. A key point that beginners may miss is the
|
2001-05-30 16:30:04 +08:00
|
|
|
fourth major paragraph of the first page mentioned above
|
|
|
|
("For most clients,"...), pointing
|
2000-04-22 04:33:34 +08:00
|
|
|
out that locking must nearly always be done outside the container,
|
|
|
|
by client code (that'd be you, not us *grin*).
|
2001-09-18 07:24:40 +08:00
|
|
|
<em>However, please take caution when considering the discussion
|
2001-05-31 10:45:04 +08:00
|
|
|
about the user-level configuration of the mutex lock
|
|
|
|
implementation inside the STL container-memory allocator on that
|
|
|
|
page. For the sake of this discussion, libstdc++-v3 configures
|
|
|
|
the SGI STL implementation, not you. We attempt to configure
|
|
|
|
the mutex lock as is best for your platform. In particular,
|
|
|
|
past advice was for people using g++ to explicitly define
|
|
|
|
_PTHREADS on the command line to get a thread-safe STL. This
|
2001-06-08 11:53:35 +08:00
|
|
|
is no longer required for your port. It may or may not be
|
2001-05-31 10:45:04 +08:00
|
|
|
a good idea for your port. Extremely big caution: if you
|
|
|
|
compile some of your application code against the STL with one
|
|
|
|
set of threading flags and macros and another portion of the
|
|
|
|
code with different flags and macros that influence the
|
|
|
|
selection of the mutex lock, you may well end up with multiple
|
|
|
|
locking mechanisms in use which don't impact each other in the
|
|
|
|
manner that they should. Everything might link and all code
|
|
|
|
might have been built with a perfectly reasonable thread model
|
|
|
|
but you may have two internal ABIs in play within the
|
|
|
|
application. This might produce races, memory leaks and fatal
|
|
|
|
crashes. In any multithreaded application using STL, this
|
2001-09-18 07:24:40 +08:00
|
|
|
is the first thing to study well before blaming the allocator.</em>
|
|
|
|
</p>
|
|
|
|
<p>You didn't read it, did you? *sigh* I'm serious, go read the
|
2000-04-22 04:33:34 +08:00
|
|
|
SGI page. It's really good and doesn't take long, and makes most
|
|
|
|
of the points that would otherwise have to be made here (and does
|
|
|
|
a better job).
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>That's much better. Now, the issue of MT has been brought up on
|
2000-04-22 04:33:34 +08:00
|
|
|
the libstdc++-v3 mailing list as well as the main GCC mailing list
|
|
|
|
several times. The Chapter 17 HOWTO has some links into the mail
|
|
|
|
archives, so you can see what's been thrown around. The usual
|
|
|
|
container (or pseudo-container, depending on how you look at it)
|
2001-09-18 07:24:40 +08:00
|
|
|
that people have in mind is <code>string</code>, which is one of the
|
2000-04-22 04:33:34 +08:00
|
|
|
points where libstdc++ departs from the SGI STL. As of the
|
|
|
|
2.90.8 snapshot, the libstdc++-v3 string class is safe for
|
|
|
|
certain kinds of multithreaded access.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>For implementing a container which does its own locking, it is
|
2000-04-22 04:33:34 +08:00
|
|
|
trivial to (as SGI suggests) provide a wrapper class which obtains
|
|
|
|
the lock, performs the container operation, then releases the lock.
|
2001-09-18 07:24:40 +08:00
|
|
|
This could be templatized <em>to a certain extent</em>, on the
|
2000-04-22 04:33:34 +08:00
|
|
|
underlying container and/or a locking mechanism. Trying to provide
|
|
|
|
a catch-all general template solution would probably be more trouble
|
|
|
|
than it's worth.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h2><a name="4">"Hinting" during insertion</a></h2>
|
|
|
|
<p>Section [23.1.2], Table 69, of the C++ standard lists this function
|
2001-08-25 04:34:34 +08:00
|
|
|
for all of the associative containers (map, set, etc):
|
|
|
|
<PRE>
|
|
|
|
a.insert(p,t);</PRE>
|
|
|
|
where 'p' is an iterator into the container 'a', and 't' is the item
|
|
|
|
to insert. The standard says that "iterator p is a hint
|
|
|
|
pointing to where the insert should start to search," but
|
|
|
|
specifies nothing more. (LWG Issue #233, currently in review,
|
|
|
|
addresses this topic, but I will ignore it here because it is not yet
|
|
|
|
finalized.)
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Here we'll describe how the hinting works in the libstdc++-v3
|
2001-08-25 04:34:34 +08:00
|
|
|
implementation, and what you need to do in order to take advantage of
|
|
|
|
it. (Insertions can change from logarithmic complexity to amortized
|
|
|
|
constant time, if the hint is properly used.) Also, since the current
|
|
|
|
implementation is based on the SGI STL one, these points may hold true
|
|
|
|
for other library implementations also, since the HP/SGI code is used
|
|
|
|
in a lot of places.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>In the following text, the phrases <em>greater than</em> and <em>less
|
|
|
|
than</em> refer to the results of the strict weak ordering imposed on
|
2001-08-25 04:34:34 +08:00
|
|
|
the container by its comparison object, which defaults to (basically)
|
|
|
|
"<". Using those phrases is semantically sloppy, but I
|
|
|
|
didn't want to get bogged down in syntax. I assume that if you are
|
|
|
|
intelligent enough to use your own comparison objects, you are also
|
|
|
|
intelligent enough to assign "greater" and "lesser"
|
|
|
|
their new meanings in the next paragraph. *grin*
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>If the <code>hint</code> parameter ('p' above) is equivalent to:
|
|
|
|
<ul>
|
|
|
|
<li><code>begin()</code>, then the item being inserted should have a key
|
2001-08-25 04:34:34 +08:00
|
|
|
less than all the other keys in the container. The item will
|
|
|
|
be inserted at the beginning of the container, becoming the new
|
2001-09-18 07:24:40 +08:00
|
|
|
entry at <code>begin()</code>.
|
|
|
|
<li><code>end()</code>, then the item being inserted should have a key
|
2001-08-25 04:34:34 +08:00
|
|
|
greater than all the other keys in the container. The item will
|
|
|
|
be inserted at the end of the container, becoming the new entry
|
2001-09-18 07:24:40 +08:00
|
|
|
at <code>end()</code>.
|
|
|
|
<li>neither <code>begin()</code> nor <code>end()</code>, then: Let <code>h</code>
|
|
|
|
be the entry in the container pointed to by <code>hint</code>, that
|
|
|
|
is, <code>h = *hint</code>. Then the item being inserted should have
|
|
|
|
a key less than that of <code>h</code>, and greater than that of the
|
|
|
|
item preceeding <code>h</code>. The new item will be inserted
|
|
|
|
between <code>h</code> and <code>h</code>'s predecessor.
|
|
|
|
</ul>
|
|
|
|
</p>
|
|
|
|
<p>For <code>multimap</code> and <code>multiset</code>, the restrictions are
|
2001-09-15 08:41:11 +08:00
|
|
|
slightly looser: "greater than" should be replaced by
|
|
|
|
"not less than" and "less than" should be replaced
|
|
|
|
by "not greater than." (Why not replace greater with
|
|
|
|
greater-than-or-equal-to? You probably could in your head, but the
|
|
|
|
mathematicians will tell you that it isn't the same thing.)
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>If the conditions are not met, then the hint is not used, and the
|
|
|
|
insertion proceeds as if you had called <code> a.insert(t) </code>
|
|
|
|
instead. (<strong>Note </strong> that GCC releases prior to 3.0.2
|
|
|
|
had a bug in the case with <code>hint == begin()</code> for the
|
|
|
|
<code>map</code> and <code>set</code> classes. You should not use a hint
|
2001-09-15 08:41:11 +08:00
|
|
|
argument in those releases.)
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>This behavior goes well with other container's <code>insert()</code>
|
2001-08-25 04:34:34 +08:00
|
|
|
functions which take an iterator: if used, the new item will be
|
|
|
|
inserted before the iterator passed as an argument, same as the other
|
|
|
|
containers. The exception
|
2001-09-18 07:24:40 +08:00
|
|
|
(in a sense) is with a hint of <code>end()</code>: the new item will
|
|
|
|
actually be inserted after <code>end()</code>, but it also becomes the
|
|
|
|
new <code>end()</code>.
|
|
|
|
</p>
|
|
|
|
<p><strong>Note </strong> also that the hint in this implementation is a
|
2001-08-25 04:34:34 +08:00
|
|
|
one-shot. The insertion-with-hint routines check the immediately
|
|
|
|
surrounding entries to ensure that the new item would in fact belong
|
|
|
|
there. If the hint does not point to the correct place, then no
|
|
|
|
further local searching is done; the search begins from scratch in
|
|
|
|
logarithmic time. (Further local searching would only increase the
|
|
|
|
time required when the hint is too far off.)
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h2><a name="5">Bitmasks and string arguments</a></h2>
|
|
|
|
<p>Bitmasks do not take char* nor const char* arguments in their
|
2001-09-15 08:41:11 +08:00
|
|
|
constructors. This is something of an accident, but you can read
|
|
|
|
about the problem: follow the library's "Links" from the
|
|
|
|
homepage, and from the C++ information "defect reflector"
|
|
|
|
link, select the library issues list. Issue number 116 describes the
|
|
|
|
problem.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>For now you can simply make a temporary string object using the
|
2001-09-15 08:41:11 +08:00
|
|
|
constructor expression:
|
|
|
|
<PRE>
|
|
|
|
std::bitset<5> b ( std::string("10110") );
|
|
|
|
</PRE>
|
|
|
|
instead of
|
|
|
|
<PRE>
|
|
|
|
std::bitset<5> b ( "10110" ); // invalid
|
|
|
|
</PRE>
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
2001-09-15 08:41:11 +08:00
|
|
|
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<P CLASS="fineprint"><em>
|
2000-04-22 04:33:34 +08:00
|
|
|
Comments and suggestions are welcome, and may be sent to
|
2001-09-18 07:24:40 +08:00
|
|
|
<a href="mailto:libstdc++@gcc.gnu.org">the mailing list</a>.
|
|
|
|
</em></p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
</body>
|
|
|
|
</html>
|