|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<title> | tait.tech</title>
|
|
|
|
|
<link rel="stylesheet" href="/assets/css/style.css">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="/assets/css/katex.css">
|
|
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<main>
|
|
|
|
|
<div id="wrapper">
|
|
|
|
|
<h1 id="vector-implementation-basics">Vector Implementation Basics</h1>
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>CMPT 225</li>
|
|
|
|
|
<li>Fall 2021</li>
|
|
|
|
|
<li>Lecture 4</li>
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<h2 id="example-using-a-simple-vector-class">Example Using a Simple “Vector” Class</h2>
|
|
|
|
|
|
|
|
|
|
<p>Take a look at the <a href="./simple_vector.cpp">simple_vector.cpp</a> file.</p>
|
|
|
|
|
|
|
|
|
|
<table>
|
|
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th>Name</th>
|
|
|
|
|
<th>Type</th>
|
|
|
|
|
<th>Value</th>
|
|
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><span id="y1">y</span></td>
|
|
|
|
|
<td>float</td>
|
|
|
|
|
<td>(empty)</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td><span id="x1">x</span></td>
|
|
|
|
|
<td>int</td>
|
|
|
|
|
<td>5</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>…</td>
|
|
|
|
|
<td>…</td>
|
|
|
|
|
<td>…</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>p</td>
|
|
|
|
|
<td>int *</td>
|
|
|
|
|
<td><a href="#x1">address</a></td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td>g</td>
|
|
|
|
|
<td>int *</td>
|
|
|
|
|
<td><a href="#y1">p+1</a></td>
|
|
|
|
|
</tr>
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
<p>What is *g?</p>
|
|
|
|
|
|
|
|
|
|
<h2 id="implementation-in-ivectorh">Implementation in <em>IVector.h</em></h2>
|
|
|
|
|
|
|
|
|
|
<p>See <a href="./IVector.h">IVector.h</a></p>
|
|
|
|
|
|
|
|
|
|
<h2 id="templates">Templates</h2>
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
<li>Often, we have algorithms that will work on many data types, with few or no changes.</li>
|
|
|
|
|
<li>In <em>strongly typed</em> languages, we need a way to produce “generic” code–code that can work on different types in different places.</li>
|
|
|
|
|
<li>In C++, <em>templates</em> let us write generic code.</li>
|
|
|
|
|
<li>A template function or class defineition has a <em>placeholder</em> for one or more data types that is instanciated at compile time.</li>
|
|
|
|
|
<li>The instanciation may be different at different places in the same code.</li>
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
<h2 id="test-program">Test Program</h2>
|
|
|
|
|
|
|
|
|
|
<p>See <a href="./test_program.cpp">test_program.cpp</a></p>
|
|
|
|
|
|
|
|
|
|
<p>See <a href="./simple_vector2.cpp">simple_vector2.cpp</a></p>
|
|
|
|
|
|
|
|
|
|
<h2 id="tvector-is-a-templated-version-of-ivector">TVector is a templated version of IVector</h2>
|
|
|
|
|
|
|
|
|
|
<p>See <a href="./TVector.h">TVector.h</a></p>
|
|
|
|
|
|
|
|
|
|
<p>See <a href="./Vector.h">Vector.h</a></p>
|
|
|
|
|
|
|
|
|
|
<p>See <a href="./TestVector.cpp">TestVector.cpp</a></p>
|
|
|
|
|
|
|
|
|
|
<footer>
|
|
|
|
|
</footer>
|
|
|
|
|
</div>
|
|
|
|
|
</main>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|