C++template运用

为什么用模板

这个问题说傻也不傻,说好笑也不好笑。因为平常我们编程中真的很少用到template。但是我们却经常性用到STL库。例如stack,queue,dequeue,priority_queue等等都是用template写的,这样我们可以调用不同的数据类型的栈,队列等。

模板优缺点

模板的可重用性和可扩展性,极大增加了我们编程的效率。但是由于C++没有二进制实时扩展性,所以模板不能像库那样被广泛使用。模板的数据类型只能在编译时才能被确定。因此,所有用基于模板算法的实现必须包含在整个设计的头文件中。


实现

模板分为两种:类模板和函数模板,STL中基本上都是用类模板写的。
格式如下:
函数模板

template<class Type>
Type 函数名(Type 参数...)

类模板

template<class Type>
class 类名{
	Type 变量名;
	...
}

我们现在已STL中的stack和queue为例,用模板实现。
(代码最好自己copy到本机跑一下,加深影响呢)
首先,我们先写出stack和queue的头文件stacktp.h,代码如下:

//stacktp.h
template <class Type>
class Stack{
	private:
		enum{
			Max = 10
		};
		Type items[Max];
		int index;
	public:
		Stack(){
			index = 0;
		};
		bool isempty(){
			return index == 0;
		}
		bool isfull(){
			return index == Max;
		}
		bool push(const Type &item){
			if(index<Max){
				items[index++] = item;
				return true;
			}
			return false;
		}
		bool top(Type &item){
			if(index>0){
				item = items[index-1];
				return true;
			}
			return false;
		}
		bool pop(){
			if(index>0){
				index--;
				return true;
			}
			return false;
		}
};
template <class Type>
class Queue{
	private:
		enum{
			Max = 10
		};
		Type items[Max];
		int indexfront;
		int indexrear;
	public:
		Queue(){
			indexfront = indexrear = 0;
		}
		bool isempty(){
			return indexfront == indexrear;
		}
		bool isfull(){
			return indexfront == (indexrear+1)%Max;
		}
		bool front(Type &item){
			if(indexfront != indexrear){
				item = items[indexfront];
				return true;
			}
			return false;
		}
		bool pop(){
			if(indexfront != indexrear){
				indexfront = (indexfront+1)%Max;
				return true;
			}
			return false;
		}
		bool push(const Type &item){
			if(!isfull()){
				items[indexrear] = item;
				indexrear = (indexrear + 1 )%Max;
				return true;
			}
			return false;
		}
};

然后我们现在就可以在程序中调用这个自己造的轮子啦。代码如下:(stacktp.cpp)

//stacktp.cpp
#include<iostream>
#include<string>
#include "stacktp.h"
using namespace std;
void Stackinit(){
	Stack<string>str;
	char ch;
	string str1;
	while(cin>>ch && toupper(ch) != 'Q'){
		while(cin.get()!='\n') continue;
		if(!isalpha(ch)){
			cout<<'\a';
			continue;
		}
		switch(ch){
			case 'A':
			case 'a':cout<<"Enter a str1 number to add:";
					 cin >> str1;
					 if(str.isfull()){
					 	cout<<"stack already full"<<endl;
					 } 
					 else{
					 	str.push(str1);
					 	cout<<"insert success"<<endl;
					 }
					 break;
			case 'P':
			case 'p':if(str.isempty())
					 	cout<<"stack already empty"<<endl;
				 	 else{
				 	 	str.top(str1);
				 	 	str.pop();
				 	 	cout<<"str1 : "<<str1<<endl;
					  }	
						break;
		}
	}
}
void Queueinit(){
	Queue<string>str;
	char ch;
	string str1;
	while(cin>>ch && toupper(ch) != 'Q'){
		while(cin.get()!='\n') continue;
		if(!isalpha(ch)){
			cout<<'\a';
			continue;
		}
		switch(ch){
			case 'a':
			case 'A': cout<<"Enter a str1 number to add:";
					  cin>>str1;
					  if(str.isfull()){
					  		cout<<"queue already full"<<endl;
					  }	
					  else{
					  	str.push(str1);
					  	cout<<"insert success"<<endl;
					  }
					  break;
			case 'p':
			case 'P': if(str.isempty()){
						cout<<"queue already empty"<<endl;
					  }
					  else{
					  	str.front(str1);
					  	str.pop();
					  	cout<<"str1 : "<<str1<<endl;
					  }
					  break;
		}
	}
}
int main(){
	//Stackinit();
	Queueinit();
}
//低下是测试数据,自行copy测试
/*
a
abc
a
abcd
a
abcde
a
abcdef
a
abcdefg
a
abcdefgh
a
abcdefghi
p
p
p
a
abcdefghij
a
abcdefghijk
a
abcdefghijkl
a
abcdefghijklm
p
p
p
p
p
p
p
p
p
p
p
p
q
*/

上面的代码还有问题呢,就是栈和队列的容量写死了,没有办法扩展,如果数据量过大,光进不出就会造成栈溢出。我们可以用指针代替。
所以请看下一篇文章

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值