为什么用模板
这个问题说傻也不傻,说好笑也不好笑。因为平常我们编程中真的很少用到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
*/
上面的代码还有问题呢,就是栈和队列的容量写死了,没有办法扩展,如果数据量过大,光进不出就会造成栈溢出。我们可以用指针代替。
所以请看下一篇文章