<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>Template 
Array</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is<span lang="en-ca"> first edition of</span> <span lang="en-ca">my little test program to use a template function array. Or in other words, how to</span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>declare a function pointer array that holds some uniformed template functions, say a series of sorting functions.</b></span></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<pre><span lang="en-ca"><b>There is no way to directly declare a function array as template function will never be compiled until it is </b></span></pre>
<pre><span lang="en-ca"><b>used! Or instanciated! So, you cannot put them into array by initializing, instead you have to &quot;make them </b></span></pre>
<pre><span lang="en-ca"><b>instanciate&quot; by passing the parameter to template. You can do this by declare another function pointer to</b></span></pre>
<pre><span lang="en-ca"><b>represent template function.</b></span></pre>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  กก</div>
<p><span lang="en-ca"><font size="2"><b>This is only a small test, if you 
understand it. Then I think we can have some common topic. </b></font></span>
</p>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<pre>#include &lt;iostream&gt;

using namespace std;

//three different sorting function declared in template
template &lt;class Elem&gt;
void bubbleSort(Elem array[], int size);

template &lt;class Elem&gt;
void selectSort(Elem array[], int size);

template &lt;class Elem&gt;
void quickSort(Elem array[], int size);

//here I have to declare function pointer to &quot;instanciate the template
//otherwise compiler won't start compile template until it is in use
void (*fun1)(int array[], int size)=bubbleSort&lt;int&gt;;
void (*fun2)(int array[], int size)=selectSort&lt;int&gt;;
void (*fun3)(int array[], int size)=quickSort&lt;int&gt;;

//this is really what I intend to use---to declare a function array to 
//hold the function pointer to 
void (*sortFun[3])(int array[], int size)={fun1, fun2, fun3};

int main()
{
	int myArray[6]={0,2,6,3,4,1};
	
	for (int i=0; i&lt;3; i++)
	{
		//this will correctly call all template functions.
		(*sortFun[i])(myArray, 6);
	}
	return 0;
}

//the implementation of all sorting algorithms are omitted here.
template &lt;class Elem&gt;
void bubbleSort(Elem array[], int size)
{
	cout&lt;&lt;&quot;this is a function of bubble sorting\n&quot;;
	for (int i=0; i&lt;size; i++)
	{
		cout&lt;&lt;array[i]&lt;&lt;&quot;\t&quot;;
	}
	cout&lt;&lt;endl;
	
}

template &lt;class Elem&gt;
void selectSort(Elem array[], int size)
{
	cout&lt;&lt;&quot;this is a function of selection sorting\n&quot;;
	for (int i=0; i&lt;size; i++)
	{
		cout&lt;&lt;array[i]&lt;&lt;&quot;\t&quot;;
	}
	cout&lt;&lt;endl;
}

template &lt;class Elem&gt;
void quickSort(Elem array[], int size)
{
	cout&lt;&lt;&quot;this is a function of quick sorting\n&quot;;
	for (int i=0; i&lt;size; i++)
	{
		cout&lt;&lt;array[i]&lt;&lt;&quot;\t&quot;;
	}
	cout&lt;&lt;endl;
}



</pre>
<pre>
</pre>

<pre><b><font color="#FF0000"><span lang="en-ca"><a name="result"></a>The following result is to prove the function array does work!</span></font></b></pre>

<pre>this is a function of bubble sorting
0 2 6 3 4 1
this is a function of selection sorting
0 2 6 3 4 1
this is a function of quick sorting
0 2 6 3 4 1
Press any key to continue

</pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>