📄 index.html
字号:
<script><!--
image( "fig4.png" )
//--></script>
</font>
<hr><h2><font color="#009999">23.2.2 Lists (cont.)</font></h2>
<font size="+1">
<p>Operations unique to <tt>list</tt>:</p>
<table border="1" width="90%" align="center">
<tr bgcolor="#00ffff">
<th>Operation</th>
<th>Description</th>
</tr>
<tr align="center">
<td><tt>merge(<i>list</i>)</tt></td>
<td>Merge the current (sorted) list with the (sorted) argument list</td>
</tr>
<tr align="center">
<td><tt>remove(<i>value</i>)</tt></td>
<td>Remove all instances of the given value</td>
</tr>
<tr align="center">
<td><tt>remove(<i>pred</i>)</tt></td>
<td>Remove values for which <i>pred</i> returns true</td>
</tr>
<tr align="center">
<td><tt>reverse()</tt></td>
<td>Reverse order of elements</td>
</tr>
<tr align="center">
<td><tt>sort()</tt></td>
<td>Place the values into sorted order</td>
</tr>
<tr align="center">
<td><tt>sort(<i>comp</i>)</tt></td>
<td>Place values in order using <tt>comp</tt></td>
</tr>
<tr align="center">
<td><tt>splice(<i>iter</i>, <i>list</i>)</tt></td>
<td>Splices argument list into the list at given location</td>
</tr>
<tr align="center">
<td><tt>unique()</tt></td>
<td>Remove duplicate copies of values from <u>sorted</u> list</td>
</tr>
<tr align="center">
<td><tt>unique(<i>pred</i>)</tt></td>
<td>Remove duplicate values from list sorted w/<i>pred</i></td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">23.2.2 Lists - Example</font></h2>
<font size="+1">
<p>Simple inventory mgt. system; products have unique IDs. Tasks:
<br>- Process an order for a product #
<br>- Process the shipment of a given product.</p>
</font>
<hr><h2><font color="#009999">23.2.2 Lists - Example</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "inventory.cpp", "90%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">23.2.3 Deque</font></h2>
<font size="+1">
<ul>
<li><i>double-ended queue</i></li>
<li>Indexed, like vector</li>
<li>Provides O(1) insertions at front or back</li>
<li>O(<i>n</i>) insertion at middle</li>
<li>W/out link fields, takes up less room than a list</li>
</ul>
</font>
<hr><h2><font color="#009999">23.2.3 Deque - Example</font></h2>
<font size="+1">
<p>Example: time-driven simulation of the line in front of a tellers'
counter at a bank.
<ul>
<li>Each minute there's a .9 probability that a new customer arrives
<ul>
<li>Add to the end of customer deque</li>
</ul>
</li>
<li>Each transaction takes from 2-8 minutes (random)</li>
<li>Calculate the avg. time a customer stands in line</li>
<li>Each minute, examine and update the status of every teller in the
teller deque, working off the front of the customer deque</li>
</ul>
</font>
<hr><h2><font color="#009999">23.2.3 Deque - Example</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "bank.cpp", "95%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">Advanced Topic 23.3</font></h2>
<font size="+1">
<hr color="#00ffff" size="6">
<p><font color="#009999">Implementation of Deque</font></p>
<ul>
<li>Not specified by the standard</li>
<li><i>circular buffer</i> commonly used</li>
<li>Like vector, but also store the start index</li>
<li>Can insert/remove from the front w/out moving all of the other
elements</li>
</ul>
</font>
<hr><h2><font color="#009999">23.3 The Stack and Queue Adapters</font></h2>
<font size="+1">
<ul>
<li>Linear data structures that support LIFO or FIFO access (sect. 16.3)</li>
<li>Not actually containers, but adapters (wrappers) built on containers</li>
<li>Found in <tt><stack></tt> and <tt><queue></tt></li>
<li>Adapter: a class that uses another container to maintain elements,
but changes the interface</li>
<li>Operations for the adapter are ultimately passed to the underlying
class</li>
<li>Slightly simplified implementation of <tt>stack</tt> (next slide)</li>
</ul>
</font>
<hr><h2><font color="#009999">23.3 (cont.) - <tt>stack</tt> implementation
</font></h2>
<font size="+1">
<blockquote>
<pre>template <class T, class C = deque<T> >
class stack
{
public:
int size() const;
void push(const T& x);
T& top();
void pop();
protected:
C values;
};</pre>
</blockquote>
</font>
<hr><h2><font color="#009999">23.3 (cont.) - <tt>stack</tt> implementation
</font></h2>
<font size="+1">
<blockquote>
<pre>template <typename T, typename C>
int stack<T, C>::size() const
{
return values.size();
}
template <typename T, typename C>
void stack<T, C>::push(const T& x)
{
values.push_back(x);
}</pre>
</blockquote>
</font>
<hr><h2><font color="#009999">23.3 <tt>stack</tt> implementation (cont.)
</font></h2>
<font size="+1">
<blockquote>
<pre>template <typename T, typename C>
T& stack<T, C>::top()
{
return values.back();
}
template <typename T, typename C>
void stack<T, C>::pop()
{
values.pop_back();
}</pre>
</blockquote>
</font>
<hr><h2><font color="#009999">23.3 <tt>stack</tt> implementation (cont.)
</font></h2>
<font size="+1">
<ul>
<li><tt>values</tt> attribute is the actual container</li>
<li><tt>stack</tt> takes two type arguments</li>
<li>The 2nd argument is the type of the underlying container</li>
<li>The default value depends on the first argument</li>
<li>The default container for both <tt>stack</tt> and <tt>queue</tt> is a
<tt>deque</tt></li>
<li>For <tt>stack</tt> it can also be <tt>vector</tt></li>
<li>A <tt>queue</tt> can also be built on a <tt>list</tt></li>
</ul>
</font>
<hr><h2><font color="#009999">23.3 (cont.) - <tt>stack</tt> Example</font></h2>
<font size="+1">
<p><i>Reverse Polish Notation</i> (RPN, or <i>postfix</i>) calculator</p>
<ul>
<li>Arguments written before operators
<ul>
<li><tt>3 + 4 * 7</tt> would be <tt>3 4 7 * +</tt></li>
</ul>
</li>
<li>Calculation is easy:</li>
<ul>
<li>Each argument read is pushed onto a stack</li>
<li>If a (binary) operator is read:
<ul>
<li>Pop top two values</li>
<li>Perform appropriate operation</li>
<li>Push result back onto stack</li>
</ul>
<li>Add 2 commands:
<ol>
<li><tt>p</tt> - print the top of the stack</li>
<li><tt>q</tt> - quit the program</li>
</ol>
</ul>
</li>
</ul>
</font>
<hr><h2><font color="#009999">23.3 (cont.) - <tt>stack</tt> Example</font></h2>
<font size="+1">
<script><!--
iframeWrapCode( "calc.cpp", "90%", "80%" )
//--></script>
</font>
<hr><h2><font color="#009999">23.4 Sets</font></h2>
<font size="+1">
<ul>
<li>optimized for fast insertion, removal, and testing</li>
<li>Not linear. Often a balanced BST</li>
<li>Finding an item takes O(log <i>n</i>) time</li>
<li>Elements of a set are unique
<ul>
<li><tt>multiset</tt> removes this restriction</li>
</ul>
</li>
<li>Declared in the <tt><set></tt> header file</li>
</ul>
</font>
<hr><h2><font color="#009999">23.4 Set Operations</font></h2>
<font size="+1">
<table border="1" width="90%" align="center">
<tr bgcolor="#00ffff">
<th>Operation</th>
<th>Description</th>
</tr>
<tr align="center">
<td><tt>set()</tt></td>
<td>Construct an empty set</td>
</tr>
<tr align="center">
<td><tt>set(<i>iterator</i>, <i>iterator</i>)</tt></td>
<td>Construct a set from the given range of values</td>
</tr>
<tr align="center">
<td><tt>begin()</tt></td>
<td>Return iterator for set</td>
</tr>
<tr align="center">
<td><tt>count(<i>value</i>)</tt></td>
<td>Return number of instances of <i>value</i></td>
</tr>
<tr align="center">
<td><tt>empty()</tt></td>
<td>Return <tt>true</tt> if set is empty</td>
</tr>
<tr align="center">
<td><tt>end()</tt></td>
<td>Return ending iterator for set
</tr>
<tr align="center">
<td><tt>erase(<i>iterator</i>)</tt></td>
<td>Erase position from set</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">23.4 Set Operations (cont.)</font></h2>
<font size="+1">
<table border="1" width="90%" align="center">
<tr bgcolor="#00ffff">
<th>Operation</th>
<th>Description</th>
</tr>
<tr align="center">
<td><tt>erase(<i>value</i>)</tt></td>
<td>Erase value from set</td>
</tr>
<tr align="center">
<td><tt>find()</tt></td>
<td>Return iterator for value</td>
</tr>
<tr align="center">
<td><tt>insert(<i>value</i>)</tt></td>
<td>Insert value into set</td>
</tr>
<tr align="center">
<td><tt>rbegin()</tt></td>
<td>Return iterator for values in reverse order</td>
</tr>
<tr align="center">
<td><tt>rend()</tt></td>
<td>Return ending iterator for values in reverse order</td>
</tr>
<tr align="center">
<td><tt>size()</tt></td>
<td>Return number of elements in set</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">23.4 Sets (cont.)</font></h2>
<font size="+1">
<ul>
<li>Elements are maintained in sequence (sorted order)</li>
<li>Fast lookups</li>
<li>Need to compare elements, unlike the fundamental containers</li>
<li>Template parameters:
<blockquote>
<pre>template <typename T, typename CMP = less<T> >
class set
{
. . .
};</pre>
</blockquote>
</li>
<li>Second argument is the trait that compares two elements</li>
<li>The default value is the standard <tt>less</tt> function object
(<tt>operator<</tt>)</li>
</ul>
</font>
<hr><h2><font color="#009999">23.4 Sets (cont.)</font></h2>
<font size="+1">
<p>2 ways to form sets of a new object type. Using the <tt>Employee</tt>
class for our example.</p>
<ol>
<li>Override <tt>operator<</tt>, allowing the normal declaration:
<blockquote>
<pre>
bool operator< (const Employee& a, const Employee& b)
{
return a.salary() < b.salary();
}
set<Employee> workers; <font color="#0000cc">// Will be sorted by salary</font></pre>
</blockquote>
</li>
</ol>
</font>
<hr><h2><font color="#009999">23.4 Sets (cont.)</font></h2>
<font size="+1">
<ol start="2">
<li>Provide an explicit function object; name it as the second template
argument
<blockquote>
<pre>class SortByName
{
public:
bool operator() (const Employee& a, const Employee& b) const;
};
bool SortByName::operator(const Employee& a, const Employee& b)
{
return a.get_name() < b.get_name();
}
set<Employee, SortByName> workers; <font color="#0000cc">// Now sorted by name</font></pre>
</blockquote>
</li>
</ol>
</font>
<hr><h2><font color="#009999">23.4 Sets - Dictionary Example</font></h2>
<font size="+1">
<p>Program to check for misspelled words. A dictionary is read
into a set, then checks input words against the set.</p>
<blockquote>
<pre>void spellCheck(istream& dictionary, istream& text)
{
set<string> words;
string word;
// First put all words from dictionary into set.
while (dictionary >> word)
words.insert(word);
// Then read words from text
while (text >> word)
if (words.count(word) == 0)
cout << "Misspelled word " << word << "\n";
}</pre>
</blockquote>
</font>
<hr><h2><font color="#009999">23.5 Maps</font></h2>
<font size="+1">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -