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

📄 functiontemplate.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


  
  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">


  
  
  <link rel="stylesheet" href="css/print.css" type="text/css">


  
  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">


  
  
  <title>函式范本(Function template)</title>
</head>


<body>


<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>


<h1><a href="CppGossip.html">C++
Gossip: 函式范本(Function template)</a></h1>


来看看气泡排序法:<br>

<div style="margin-left: 40px; font-weight: bold; font-family: Courier New,Courier,monospace;">// 整数气泡排序法<br>

</div>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void bubbleSort(int number[], int length) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; int i, j, k, flag = 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; 
int tmp; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; for(i = 0; i &lt; 
length-1 &amp;&amp; flag == 1; i++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; flag = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; for(j = 
0; j &lt; length-i-1; j++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(number[j+1] &lt; number[j]) { 
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmp = number[j]; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number[j] = 
number[j+1]; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number[j+1] = tmp; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = 
1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; } </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; } </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">
// 浮点数气泡排序</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void bubbleSort(float 
number[], int length) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; int i, j, k, flag = 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; float tmp; 
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; for(i = 0; i &lt; length-1 
&amp;&amp; flag == 1; i++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; flag = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; for(j = 0; j &lt; 
length-i-1; j++) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(number[j+1] &lt; number[j]) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; tmp = number[j]; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number[j] = number[j+1]; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number[j+1] = tmp; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = 1; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } 
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; &nbsp; &nbsp; } </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">&nbsp; &nbsp; } </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

函式范本(Function template)又称“通用函式”(Generic 
function),它可以适用于不同资料型态的参数列,但实作内容相同的函式,以上面的气泡排序法为例,除了参数列资料型态不同之外,其实排序的程式码几乎相同,实在没必要为了资料型态的不同而重覆撰写相同的程式码。 
<br>

<br>

建立函式范本的方法之一是使用"template"关键字,并使用class来宣告取代用的资料型态宣告字,使用下面这个程式作个示范:<br>


<br>


<pre>#include &lt;iostream&gt; <br>using namespace std; <br><br>template &lt;class X&gt; <br>void bubbleSort(X[], int); <br><br>int main() { <br>    int iarr[] = {5, 3, 11, 6, 8, 9, 20, 11}; <br>    float farr[] =  {5.1, 3.4, 11.3, 6.6, 8.9, 9.11, 20.2, 11.4}; <br><br>    bubbleSort(iarr, 8); <br>    for(int i = 0; i &lt; 8; i++) <br>        cout &lt;&lt; " " &lt;&lt; iarr[i]; <br>    cout &lt;&lt; endl; <br><br>    bubbleSort(farr, 8); <br>    for(int i = 0; i &lt; 8; i++) <br>        cout &lt;&lt; " " &lt;&lt; farr[i]; <br>    cout &lt;&lt; endl; <br>    <br>    return 0; <br>} <br><br>template &lt;class X&gt; <br>void bubbleSort(X number[], int length) { <br>    int i, j, k, flag = 1; <br>    X tmp; <br><br>    for(i = 0; i &lt; length-1 &amp;&amp; flag == 1; i++) { <br>        flag = 0; <br>        for(j = 0; j &lt; length-i-1; j++) { <br>            if(number[j+1] &lt; number[j]) { <br>                tmp = number[j]; <br>                number[j] = number[j+1]; <br>                number[j+1] = tmp; <br>                flag = 1; <br>            } <br>        } <br>    } <br>}</pre>



<span class="postbody"><br>

执行结果:</span><br>


<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">


  <tbody>


    <tr>


      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">&nbsp; 3 5 6 8 9 11 11 20<br>

&nbsp;3.4 5.1 6.6 8.9 9.11 11.3 11.4 20.2</span></small><span style="color: rgb(255, 255, 255);"><br>


      </span></td>


    </tr>


  
  
  </tbody>
</table>


<br>

在这个程式中,编译器会自动产生两个版本的bubbleSort()函式,一个是整数阵列的参数,一个是浮点数阵列的参数,也可以使用"typename"来取代class关键字,例如: <br>

<div style="margin-left: 40px; font-family: Courier New,Courier,monospace;"><span style="font-weight: bold;">template &lt;typename X&gt; </span><br style="font-weight: bold;">

<span style="font-weight: bold;">void bubbleSort(X number[], int length) { </span><br style="font-weight: bold;">

<span style="font-weight: bold;">&nbsp; &nbsp; .... </span><br style="font-weight: bold;">

<span style="font-weight: bold;">}</span><br>

</div>

<br>

"typename"是后来才加入至C++中的,早期的撰写风格主要都是使用"class"。<br>

<br>

在宣告了函式范本之后,您还是可以自行重载函式,例如: <br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">template &lt;class X&gt; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void bubbleSort(X number[], int length) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">.... 
</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">void bubbleSort(int number[], int length, int descend) { </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">// 
以descend决定要由大至小或由小至大排序 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">.... </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br>

</div>

<br>

在这个程式片段中,虽然您宣告了样版函式bubbSort(),但您仍自行重载了一个函式以自行定义特定的函式功能。 <br>

<br>


</body>
</html>

⌨️ 快捷键说明

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