⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sidebar.htm

📁 ARM编辑、编译软件
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>Sidebars</TITLE></HEAD>
<BODY>
<A HREF="ug.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the user guide home page.</STRONG></P>
<P>&copy;Copyright 1996 Rogue Wave Software</P>
<H2>Sidebars</H2>
<BR>
<A NAME="sidebar1"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Iterators</STRONG>
<P><I>Iterators</I> are pointer-like objects, used to cycle through the elements stored in a container.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar2"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Range</STRONG>
<P>A <I>range</I> is a sequence of values held in a container.  The range is described by a pair of iterators, which define the beginning and end of the sequence.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar3"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Iterator Ranges</STRONG>
<P>When iterators are used to describe a range of values in a container, it is assumed (but not verified) that the second iterator is reachable from the first.  Errors will occur if this is not true.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar4"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Ordinary Pointers as Iterators</STRONG>
<P>Because ordinary pointers have the same functionality as random access iterators, most of the generic algorithms in the standard library can be used with conventional C++ arrays, as well as with the containers provided by the standard library.</P>

<BR><BR><BR><BR><BR><A NAME="sidebar5"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Parallel Sequences</STRONG>
<P>A number of the generic algorithms manipulate two parallel sequences.  Frequently the second sequence is described using only a beginning iterator, rather than an iterator pair.  It is assumed, but not checked, that the second sequence has at least as many elements as the first.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar6"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> randomInteger()</STRONG>
<P>The function <SAMP>randomInteger</SAMP> described here is used in a number of the example programs presented in later sections.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar7"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Stream Iterators</STRONG>
<P>An input stream iterator permits an input stream to be read using iterator operations.  An output stream iterator similarly writes to an output stream when iterator operations are executed.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar8"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Location of the Class Definitions</STRONG>
<P>The class definitions for unary_function and binary_function can be incorporated by #including <SAMP>functional</SAMP>.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar9"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Using Function Objects to Store References</STRONG>
<P>A more complex illustration of the use of a function object occurs in the radix sorting example program given as an illustration of the use of the list data type in <a href="exa_6226.htm">Chapter 7</a>.  In this program references are initialized in the function object, so that during the sequence of invocations the function object can access and modify local values in the calling program.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar10"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> A Hot Idea</STRONG>
<P>The idea described here by the term binder is in other contexts often described by the term <I>curry</I>.  This is not, as some people think, because it is a hot idea.  Instead, it is named after the computer scientist Haskell P. Curry, who used the concept extensively in an influential book on the theory of computation in the 1930's.  Curry himself attributed the idea to Moses Sch_nfinkel, leaving one to wonder why we don't instead refer to binders as "Sch_nfinkels."</P>
<BR><BR><BR><BR><BR><A NAME="sidebar11"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Requirements of an Element Type</STRONG>
<P>Elements that are held by a vector must define a default constructor (constructor with no arguments), as well as a copy constructor.  Although not used by functions in the vector class, some of the generic algorithms also require vector elements to recognize either the equivalence operator (operator <SAMP>==</SAMP>) or the relational less-than operator (operator <SAMP>&#60;)</SAMP>.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar12"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Constructors and Iterators</STRONG>
<P>Because it requires the ability to define a method with a template argument different from the class template, some compilers may not yet support the initialization of containers using iterators.  In the mean time, while compiler technology catches up with the standard library definition, the Rogue Wave version of the standard library will support conventional pointers and vector iterators in this manner.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar13"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Memory Management</STRONG>
<P>A vector stores values in a single large block of memory.  A deque, on the other hand, employs a number of smaller blocks.  This difference may be important on machines that limit the size of any single block of memory, because in such cases a deque will be able to hold much larger collections than are possible with a vector.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar14"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Costly Insertions</STRONG>
<P>Even adding a single element to a vector can, in the worst case, require time proportional to the number of elements in the vector, as each element is moved to a new location.  If insertions are a prominent feature of your current problem, then you should explore the possibility of using containers, such as lists or sets, which are optimized for insert operations.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar15"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Iterator Invalidation</STRONG>
<P>Once more, it is important to remember that should reallocation occur as a result of an insertion, all references, pointers, and iterators that denoted a location in the now-deleted memory block that held the values before reallocation become invalid.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar16"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Initializing Count</STRONG>
<P>Note that count() returns its result through an argument that is passed by reference.  It is important that this value be properly initialized before invoking this function.</P>

<BR><BR><BR><BR><BR><A NAME="sidebar17"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Source</STRONG>
<P>Source for this program is found in the file <SAMP>sieve.cpp</SAMP>.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar18"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Memory Management</STRONG>
<P>Note that if you declare a container as holding pointers, you are responsible for managing the memory for the objects pointed to.  The container classes will not, for example, automatically free memory for these objects when an item is erased from the container.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar19"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Iteration Invalidation</STRONG>
<P>Unlike a <B><I>vector</I></B> or <B><I>deque</I></B>, insertions or removals from the middle of a <B><I>list</I></B> will not invalidate references or pointers to other elements in the container. This property can be important if two or more iterators are being used to refer to the same container.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar20"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Verify Search Results</STRONG>
<P>The searching algorithms in the standard library will always return the end of range iterator if no element matching the search condition is found.  Unless the result is guaranteed to be valid, it is a good idea to check for the end of range condition.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar21"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Sample Program</STRONG>
<P>The executable version of the widget works program is contained in file <SAMP>widwork.cpp</SAMP> on the distribution disk.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar22"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Sample Program</STRONG>
<P>The complete radix sort program is found in the file <SAMP>radix.cpp</SAMP> in the tutorial distribution disk.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar23"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Sets, Ordered and Not</STRONG>
<P>Although the abstract concept of a set does not necessarily imply an ordered collection, the <A HREF="../stdref/set_1649.htm"><B><I>set</I></B></A> data type is always ordered.  If necessary, a collection of values that cannot be ordered can be maintained in, for example, a <A HREF="../stdref/lis_3222.htm"><B><I>list</I></B></A>.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar24"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Sets and Bags</STRONG>
<P>In other programming languages, a multiset is sometimes referred to as a <I>bag</I>.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar25"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Initializing Sets with Iterators</STRONG>
<P>As we noted in the earlier discussion on vectors and lists, the initialization of containers using a pair of iterators requires a mechanism that is still not widely supported by compilers.  If not provided, the equivalent effect can be produced by declaring an empty set and then using the <SAMP>copy()</SAMP> generic algorithm to copy values into the set. </P>
<BR><BR><BR><BR><BR><A NAME="sidebar26"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> The Pair Data Type</STRONG>
<P>If you want to use the <B>pair</B> data type without using maps, you should include the header file named <SAMP>utility</SAMP>.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar27"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> No Iterator Invalidation</STRONG>
<P>Unlike a vector or deque, the insertion or removal of values from a set does not invalidate iterators or references to other elements in the collection.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar28"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Sample Program</STRONG>
<P>This program can be found in the file <SAMP>spell.cpp</SAMP> in the tutorial distribution.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar29"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Other Names for Maps</STRONG>
<P>In other programming languages, a map-like data structure is sometimes referred to as a <I>dictionary</I>, a <I>table</I>, or an <I>associative</I> <I>array</I>.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar30"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Pairs</STRONG>
<P>See the discussion of insertion in <a href="set_3455.htm">Chapter 8</a> for a description of the pair data type.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar31"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> No Iterator Invalidation</STRONG>
<P>Unlike a vector or deque, the insertion or removal of elements from a map does not invalidate iterators which may be referencing other portions of the container.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar32"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Sample Program</STRONG>
<P>The complete example program is included in the file tutorial <SAMP>tele.cpp</SAMP> in the distribution.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar33"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Sample Program</STRONG>
<P>The executable version of this program is found in the file <SAMP>graph.cpp</SAMP> on the tutorial distribution disk.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar34"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Sample Program</STRONG>
<P>An executable version of the concordance program is found on the tutorial distribution file under the name <SAMP>concord.cpp</SAMP>.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar35"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> LIFO and FIFO</STRONG>
<P>A stack is sometimes referred to as a LIFO structure, and a queue is called a FIFO structure.  The abbreviation LIFO stands for Last In, First Out.  This means the first entry removed from a stack is the last entry that was inserted.  The term FIFO, on the other hand, is short for First In, First Out.  This means the first element removed from a queue is the first element that was inserted into the queue.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar36"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Right Angle Brackets</STRONG>
<P>Note that on most compilers it is important to leave a space between the two right angle brackets in the declaration of a stack; otherwise they are interpreted by the compiler as a right shift operator.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar37"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Sample Program</STRONG>
<P>This program is found in the file <SAMP>calc.cpp</SAMP> in the distribution package.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar38"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Defensive Programming</STRONG>
<P>A more robust program would check to see if the stack was empty before attempting to perform the <SAMP>pop()</SAMP> operation.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar39"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Obtaining the Sample Program</STRONG>
<P>The complete version of the bank teller simulation program is found in file <SAMP>teller.cpp</SAMP> on the distribution disk.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar40"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> A Queue That is Not a Queue</STRONG>
<P>The term priority <I>queue</I> is a misnomer, in that the data structure is not a queue, in the sense that we used the term in <a href="sta_2474.htm">Chapter 10</a>, since it does not return elements in a strict first-in, first-out sequence.  Nevertheless, the name is now firmly associated with this particular data type.</P>
<BR><BR><BR><BR><BR><A NAME="sidebar41"><IMG SRC="images/sail_bar.gif"></A> <BR><IMG SRC="images/note.gif"><STRONG> Initializing Queues from other containers</STRONG>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -