函数模板
template<参数列表>例子1:函数模板例子
template<class ElementType >
void sortArray(ElementType b[], int len) {
for (int pass = 0; pass < len - 1; pass ++ )
for(int i = pass+1; i <= len-1; i++)
if ( b[ pass ] > b[ i ] )
{
ElementType hold;
hold = b[ pass ];
b[ pass ] = b[ i ];
b[ i ] = hold;
}
}
例子2:使用函数模板实现数组的排序和输出
//ex18_1:使用函数模板实现数组的排序和输出
#include <iostream>
using namespace std;
#define SIZE 8
template<class ElementType>
void sortArray(ElementType b[], int len)
{
for (int pass=0; pass < len-1; pass++)
for(int i = pass+1; i <= len-1; i++)
if (b[pass] > b[i]) {
ElementType hold;
hold = b[ pass ];
b[ pass ] = b[ i ];
b[ i ] = hold;
}
}
template<class ElementType >
void displayArray(ElementType b[], int len)
{
for(int index=0; index <= len-1; index++)
if ( index != len -1 )
cout << b[ index ] << "\t";
else
cout << b[ index ] << endl;
}
int main() {
int ai[SIZE] = {18,35,36,61,9,112,77,12};
double af[SIZE] = {12.1, -23.8, 3.7, -16.0,
9.1, 12.12, 7.7, 56.3};
cout << "Before sorting:\n";
cout << "ai: \t";
displayArray(ai, SIZE);
sortArray(ai, SIZE);
cout << "After sorting:\n";
cout << "ai: \t";
displayArray(ai, SIZE);
cout << "Before sorting:\n";
cout << "af: \t";
displayArray(af, SIZE);
sortArray(af, SIZE);
cout << "After sorting:\n";
cout << "af: \t";
displayArray(af, SIZE);
return 0;
}
程序运行结果
Before sorting:
ai: 18 35 36 61 9 112 77 12
After sorting:
ai: 9 12 18 35 36 61 77 112
Before sorting:
af: 12.1 -23.8 3.7 -16 9.1 12.12 7.7 56.3
After sorting:
af: -23.8 -16 3.7 7.7 9.1 12.1 12.12 56.3
类模板
- 类模板:将类定义中的数据类型参数化
- 类模板的实例化:用具体的数据类型替换模板的参数以得到具体的类(模板类)
- 模板类也可以实例化为对象
例子2:用类模板实现栈
// tstack.h: 类模板的定义
template<class ElementType>
class Stack {
public:
Stack( int = 8 ); // 省缺栈元素的树数目为8
~Stack(){ delete [] data; };
int pop(ElementType &num);
int push(ElementType num);
private:
ElementType *data; //栈数据存储
int memNum; //栈元素个数
int size; //栈大小
};
template<class ElementType >
Stack<ElementType>::Stack(int s) {
size = s > 0 ? s : 8;
data = new ElementType[s];
memNum = 0;
}
template<class ElementType >
int Stack<ElementType>::pop(ElementType &num) {
if (memNum==0)
return 0;
num = data[ -- memNum ];
return 1;
}
template<class ElementType >
int Stack<ElementType>::push(ElementType mem)
{
if (memNum == size)
return 0;
data[ memNum ++ ] = mem;
return 1;
}
//ex18_2.cpp:使用栈
#include <iostream>
using namespace std;
#include "tstack.h"
int main() {
Stack<double> doubleStack(6);
double f = 3.14;
cout<<"Pushing elements into doubleStack:\n";
while (doubleStack.push(f)) {
cout << f << ' ';
f += f;
}
cout << "\nStack is full. Cannot push "
<< f << " onto the doubleStack.";
cout<<"\nPopping elements from doubleStack:\n";
while (doubleStack.pop(f))
cout << f << ' ';
cout << "\nStack is empty. Cannot pop.\n";
Stack<int> intStack;
int i = 1;
cout<<"\nPushing elements into intStack:\n";
while (intStack.push(i)) {
cout << i << ' ';
++i;
}
cout<<"\nStack is full.push " <<i<<"failed.";
cout<<"\n\nPopping elements from intStack:\n";
while (intStack.pop(i))
cout << i << ' ';
cout << "\nStack is empty. Cannot pop.\n";
return 0;
}
程序执行结果
Pushing elements onto doubleStack:
3.14 6.28 12.56 25.12 50.24 100.48
Stack is full. push 200.96 failed.
Popping elements from doubleStack:
100.48 50.24 25.12 12.56 6.28 3.14
Stack is empty. Cannot pop.
Pushing elements onto intStack:
1 2 3 4 5 6 7 8
Stack is full. push 9 failed.
Popping elements from intStack:
8 7 6 5 4 3 2 1
Stack is empty. Cannot pop.