C++学习笔记之模板

函数模板
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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值