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

📄 index.html

📁 《Big C++ 》Third Edition电子书和代码全集-Part1
💻 HTML
📖 第 1 页 / 共 2 页
字号:
					<td><font size="+1">

<pre>
template&lt;typename T&gt;
inline T Pair&lt;T&gt;::get_first() const
{
   return first;
}
</pre>
						</font>
					</td>
				</tr>
				<tr>
					<td valign="top"><font size="+1" color="#009999">Purpose:</font></td>
					<td><font size="+1">Supply the implementation of a member function
					for a class template.</font></td>
				</tr>
			</table>
			</font>
		</td>
	</tr>
</table>

</font>

<hr><h2><font color="#009999">22.4 Template Linked List</font></h2>
<font size="+1">

<ul>
	<li>Recall the linked list from chptr. 16</li>
	<li><tt>List</tt> stored strings</li>
	<li>Used the <tt>Node</tt> and <tt>Iterator</tt> classes</li>
	<li>Let's make them into templates</li>
	<li>Node:
		<blockquote>
<pre>template&lt;typename T&gt;
class Node
{
public:
   Node(T s);
private:
   T data;
   Node&lt;T&gt;* previous;
   Node&lt;T&gt;* next;
};</pre>
		</blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.4 Template Linked List (cont.)</font></h2>
<font size="+1">

<ul>
	<li>List:
		<blockquote>
<pre>template&lt;typename T&gt;
class List
{
public:
   List();
   void push_back(T s);
   void insert(Iterator&lt;T&gt; pos, T s);
   Iterator&lt;T&gt; erase(Iterator&lt;T&gt; pos);
   Iterator&lt;T&gt; begin();
   Iterator&lt;T&gt; end();
private:
   Node&lt;T&gt;* first;
   Node&lt;T&gt;* last;
};</pre>
		</blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.4 Template Linked List (cont.)</font></h2>
<font size="+1">

<ul>
	<li>Turn <i>each</i> member function definition into a template:
		<blockquote>
<pre>template&lt;typename T&gt;
Iterator&lt;T&gt; List&lt;T&gt;::begin()
{
   Iterator&lt;T&gt; iter;
   iter.position = first;
   iter.last = last;
   return iter;
}</pre>
		</blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">Quality Tip 22.2</font></h2>
<font size="+1">
<hr color="#00ffff" size="6">

<p><font color="#009999">Use <tt>typedef</tt> to Form an Alias for a 
	Long Name</font></p>

<ul>
	<li><tt>typedef</tt> long types:
		<blockquote><tt>
			typedef pair&lt;int, double&gt; ElementType;
			<br>typedef vector&lt; pair&lt;string, <nobr>string&gt; &gt;</nobr>
				StringMap;
		</tt></blockquote>
	</li>
	<li>Space inside the <tt>&gt; &gt;</tt></li>
	<li>Associates a new name with the given type</li>
	<li>Use as any other type:
		<blockquote><tt>
			ElementType new_value; <font color="#0000cc">
				// new_value is type pair&lt;int, string&gt;</font>
		</tt></blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">Quality Tip 22.2 (cont.)</font></h2>
<font size="+1">
<hr color="#00ffff" size="6">

<table border="1" cellpadding="4" bgcolor="#00cccc">
	<tr>
		<td bgcolor="#ffffff"><font size="+1"><font color="#009999">Syntax
			<tt>typedef</tt> Statement</font>

<pre>typedef <i>declaration</i>;</pre>

			<table border="0" cellpadding="4">
				<tr>
					<td valign="top"><font size="+1" color="#009999">
						Example:</font></td>
					<td><font size="+1">

<pre>typedef pair&lt;int, string&gt; ElementType;</pre>
						</font>
					</td>
				</tr>
				<tr>
					<td valign="top"><font size="+1" color="#009999">Purpose:</font></td>
					<td><font size="+1">Create an alias for a complicated type
						name.</font></td>
				</tr>
			</table>
			</font>
		</td>
	</tr>
</table>

</font>

<hr><h2><font color="#009999">22.5 Nontype Template Arguments</font></h2>
<font size="+1">

<ul>
	<li>May be other things (integer constants)</li>
	<li>Consider our matrix case study
		<ul>
			<li>3x3, at first</li>
			<li>Then managed heap memory to remove this restriction</li>
			<li>Template arguments provide a third alternative</li>
		</ul>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.5 Nontype Template Arguments (cont.)
	</font></h2>
<font size="+1">

<ul>
	<li>Size could be specified by integer template arguments:
		<blockquote>
<pre>template&lt;typename T, int ROWS, int COLUMNS&gt;
class Matrix
{
   . . .
private:
   T data[ROWS][COLUMNS];
};</pre>
		</blockquote>
	</li>
	<li>Note, the integer values must be known at compile time</li>
</ul>

</font>

<hr><h2><font color="#009999">22.5 Nontype Template Arguments (cont.)
	</font></h2>
<font size="+1">

<ul>
	<li>Supply the type and the size:
		<blockquote><tt>
			Matrix&lt;double, 3, 4&gt; a; <font color="#0000cc">
				// A 3 x 4 matrix of double values</font>
			<br>Matrix&lt;string, 2, 2&gt; b;
		</tt></blockquote>
	</li>
	<li>The bounds become part of the type</li>
</ul>

</font>

<hr><h2><font color="#009999">22.5 Nontype Template Arguments (cont.)
	</font></h2>
<font size="+1">

<ul>
	<li>Compatibility checked at compile time
		<blockquote><tt>
			Matrix&lt;int, 3, 4&gt; a;
			<br>Matrix&lt;double, 3, 4&gt; b;
			<br>Matrix&lt;int, 5, 7&gt; c;
			<br>Matrix&lt;int, 3, 4&gt; d;
			<br>b = a; <font color="#cc0000">
				// Error, element types don't match.</font>
			<br>c = a; <font color="#cc0000">
				// Error, sizes dont match, so types differ.</font>
			<br>d = a; <font color="#cc0000">
				// OK. Element types and sizes match.</font>
		</tt></blockquote>
	</li>
	<li>The requirements for matrix multiplication are much more difficult
		to specify</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	</font></h2>
<font size="+1">

<ul>
	<li>Specify behavior (set <i>policy</i>)</li>
	<li>Generalize the <tt>max</tt> function to support types that:
		<ul>
			<li>Don't define <tt>operator&lt;</tt></li>
			<li>Have more than one way of ordering</li>
		</ul>
	</li>
	<li>Make comparison a template argument:
		<blockquote>
<pre>template&lt;typename T, typename CMP&gt;
T max(const T&amp; left, const T&amp; right, CMP cmp)
{
   if (cmp(left, right))
      return right;
   return left;
}</pre>
		</blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	</font></h2>
<font size="+1">

<ul>
	<li><tt>&lt;</tt> has been replaced by a function call</li>
	<li><tt>cmp</tt> may be either
		<ol>
			<li>An actual function</li>
			<li>A function object (chptr. 17)</li>
		</ol>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<ul>
	<li>E.g., use function objects to compare <tt>Employee</tt>s by salary or
		name</li>
	<li>Develop these two classes:
		<blockquote>
<pre>class CompareBySalary
{
public:
   bool operator()(const Employee&amp; a,
      const Employee&amp; b) const;
};

bool CompareBySalary::operator()(const Employee&amp; a,
   const Employee&amp; b) const
{
   return a.get_salary() &lt; b.get_salary();
}</pre>
		</blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<blockquote>
<pre>class CompareByName
{
public:
   bool operator()(const Employee&amp; a,
      const Employee&amp; b) const;
};

bool CompareByName::operator()(const Employee&amp; a,
   const Employee&amp; b) const
{
   return a.get_name() &lt; b.get_name();
}</pre>
</blockquote>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<ul>
	<li><tt>CompareByName</tt> and <tt>CompareBySalary</tt> are types</li>
	<li>Represent two different orderings</li>
	<li>We can use the <tt>max</tt> function in either sense:
		<blockquote><tt>
			Employee alice("Alice Smith", 45000);
			<br>Employee fred("Fred Jones", 38500);
			<br>
			<br>Employee one = max(alice, fred, CompareBySalary());
			<br>Employee two = max(alice, fred, CompareByName());
		</tt></blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<ul>
	<li>Same idea applies to template class arguments</li>
	<li>Maintaining a collection in sorted order
		<ul>
			<li>Specify the rules for comparing two elements</li>
			<li>Do not specify criteria in the container itself</li>
		</ul>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<ul>
	<li>Use template arguments:
		<blockquote>
<pre>template&lt;typename T, typename CMP&gt;
class OrderedCollection
{
public:
   typedef Iterator&lt;T&gt; iterator;
   void add(T value);
   iterator begin();
   iterator end();
private:
   List&lt;T&gt; data;
   CMP comparator;
};</pre>
		</blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<ul>
	<li>The comparator is an attribute</li>
		<blockquote>
<pre>template&lt;typename T, typename CMP&gt;
void OrderedCollection&lt;T, CMP&gt;::insert(T value)
{
   iterator ptr = begin();
   while (ptr != end())
   {
      if (comparator(value, *ptr))
      {
         <font color="#0000cc">// Found place to insert</font>
         data.insert(ptr, value);
         return;
      }
      ++ptr;
   }
   // Insert at end
   data.insert(ptr, value);
}</pre>
		</blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<ul>
	<li>The collection is defined by the type it stores, and by the type
		of its comparator
		<blockquote><tt>
			void f(OrderedCollection&lt;Employee, CompareByName&gt;&amp; c)
			<br>{
			<br>&nbsp;&nbsp;&nbsp;. . .
			<br>}
			<br>
			<br>OrderedCollection&lt;Employee, CompareBySalary&gt; a;
			<br>a.add(. . .);
			<br>OrderedCollection&lt;Employee, CompareByName&gt; b;
			<br>b.add(. . .);
			<br>f(a); <font color="#cc0000">// Errortypes don't match</font>
			<br>f(b); <font color="#0000cc">// OK - type is OK</font>
		</tt></blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<ul>
	<li>Identify a default policy</li>
	<li>Associated with a template parameter</li>
	<li>STL algorithms use the <tt>less&lt;T&gt;</tt>, in
		the <tt>&lt;functional&gt;</tt> header file</li>
		<blockquote>
<pre>template&lt;typename T&gt;
class less
{
   bool operator()(const T&amp; x, const T&amp; y) const;
}

template&lt;typename T&gt;
bool less&lt;T&gt;::operator()(const T&amp; x, const T&amp; y) const
{
   return x &lt; y;
}</pre>
		</blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.6 Setting Behavior Using Template Arguments
	(cont.)</font></h2>
<font size="+1">

<ul>
	<li>The <tt>less</tt> defines a function object</li>
	<li>Invokes <tt>operator&lt;</tt></li>
	<li>Specify <i>trait</i> as the default template argument
		<blockquote><tt>
			template&lt;typename T, typename CMP = less&lt;T&gt; &gt;
			<br>class OrderedCollection
			<br>{
			<br>&nbsp;&nbsp;&nbsp;. . .
			<br>}
		</tt></blockquote>
	</li>
	<li>To use the default:
		<blockquote><tt>
			OrderedCollection&lt;int&gt; data; <font color="#0000cc">
				// Will use the less than operator</font>
		</tt></blockquote>
	</li>
</ul>

</font>

<hr><h2><font color="#009999">22.7 Case Study: Matrices, Cont.</font></h2>
<font size="+1">

<ul>
	<li>Create a template version of the <tt>Matrix</tt> class</li>
	<li>Start with the class in chptr. 19
		<ul>
			<li>Avoids explaining interactions between templates and nested classes
				</li>
			<li>No reason to make the exception classes into templates</li>
		</ul>
	</li>
	<li>Parameterize the element type only</li>
	<li>Not the row and column bounds; simplifies overloading the arithmetic
		operators</li>
	<li>Zero is no longer an appropriate value for all types; remove the
		initializer loop in the c'tor</li>
	<li>Move member function definitions into the header file</li>
</ul>

</font>

<hr><h2><font color="#009999">22.7 Case Study: Matrices (<tt>matrix5.h</tt>)
	</font></h2>
<font size="+1">

<script><!-- 
	iframeWrapCode( "matrix5.h", "90%", "80%" )
//--></script>

</font>

<hr><h2><font color="#009999">22.7 Case Study: Matrices (<tt>matrix5.cpp</tt>)
	</font></h2>
<font size="+1">

<script><!-- 
	iframeWrapCode( "matrix5.cpp", "90%", "70%" )
//--></script>

</font>

<hr><h2><font color="#009999">22.7 Case Study: Matrices
	(<tt>matrixtest5.cpp</tt>)</font></h2>
<font size="+1">

<script><!-- 
	iframeWrapCode( "matrixtest5.cpp", "90%", "80%" )
//--></script>

</font>

<hr><h2><font color="#009999">Chapter Summary</font></h2>
<font size="+1">
<hr color="#00ffff" size="6">

<ol>
	<li>A template allows a function or class to be parameterized by type</li>
	<li>The template abstracts the actions to be performed</li>
	<li>Template arguments are inferred from the values in a function invocation
		</li>
	<li>Template functions can have the same name as nontemplate functions.  The
		nontemplates are searched first for a match</li>
	<li>Template classes allow the creation of containers that work with many
		different types of values</li>
	<li>Template classes must be explicitly instantiated in order to declare
		new variables</li>
	<li>Template arguments can be type names or they can be constants</li>
	<li>A <tt>typedef</tt> statement can be used to form an alias for a type
		name</li>
	<li>Template arguments can be used to set behavior, for example, setting the
		comparison algorithm for sorted containers</li>
</ol>

</font>



</body>
</html>

⌨️ 快捷键说明

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