一、如何进行泛型编程
C/C++是一种静态编程语言,必须需要把代码翻译成可执行的二进制可执行程序然后再运行,一旦编译好之后就不能再变了(数据类型也就必须确定下无法更改,因此要为每一种数据类型编写一份算法,工程量巨大)。
C语言中的快速排序:
void qsort(void *base, size_t nmemb, size_t size,int(*compar)(const void *, const void *));
C++提供了模板的编程方法来解决泛型编程的问题,它的解决思路是,程序员先编写好一份"套路"代码,然后在调用时编译器根据调用时的参数再为这种数据类型生成一份属于的它代码。
二、模板的语法
template <typename T,typename M>
T max(T num1,M num1)
{
return num1>num2?num1:num2;
}
练习:使用模板实现出一冒泡排序,能够被所有的数据类型所调用。
T被称作模板的类型参数,可以叫任何名字,只是俗成约定叫作T。
它指的是函数调用时的任何类型的参数。
虽然模板的类型可以是任意的,但是必须要支持模板函数中所使用到的运算符,因此模板不是成能的,虽然能带来很多好处,但也带来了很多问题。
三、函数模板的使用
1、模板的实例化
编译器不会把函数模板编译成一个实例,而是根据调用时的参数,再进行实例化(进一步生成二进制指令)。
2、使用模板时才实例化。
模板只有在调用时才会实例化,因此模板编译正确并不代码没有问题,很多错误会产生于调用时。
3、二次编译
第一次是检查模板的语法,第二次编译是根据调用参数把模板实例化出来然后再检查运算符是否支持这种类型。
四、函数模板的隐式推断
a、使用函数模板时可以根据参数的类型来推断模板的参数
b、当函数模板不能通过函数调用时的参数来推断模板参数时,可以使用<类型,类型...>来明确指定。
c、函数模板参数是可以有默认值的
1、默认值什么放在右边
2、C++标准才支持:-std=c++0x
五、函数模板与普通可以重载(特化)
1、同一种格式的函数和函数模板是可以共生,但优先调用普通函数,但可以有函数名后添加一个空的<>指定调用函数模板,模板参数根据调用时的参数推断。
2、普通函数在调用时可以进行类型提升,但是这种提升的结果要低于模板的实例化。
3、函数模板也可以进行类型提升,但如果有一个普通函数也可以进行类型提升调用,那么则优先调用普通函数。
六、类模板
a、类模板的语法
类模板的参数可以在类中当作类型使用,可以定义成员、返回值、参数等。
template<class T,class A,class B,...>
class className
{
C c;
public:
T func(A a);
};
注意:typename也可以继续使用,但大多用class以示区别。
b、类模板的使用
类模板必须要经过实例化才能使用,也是需要经过两次编译,第一次是把类模板编译成一个"套路",这个过程是为了检查语法,第二次是根据实例化参数,生成一个类,然后才能使用这个类创建对象。
使用类模板实例化一个类:
className<type1,type2,...> a;
练习:实现栈类模板,成员函数有入栈、出栈、栈空、栈满。
c、类模板参数不支持隐式推断,必须显示实例化。
d、静态成员的定义的语法
template<class T> int MyStack<T>::num = 10;
静态成员必须在类模板实例化之后才被真正定义出来,每个实例化的类都有和份静态成员,这个实例化类创建出的对象共用一个静态成员。
e、递归实例化
MyStack<MyStack<int>> stack;
尽量不要轻易使用。
七、类模板的特化(重载)
特化:指的是当类模板有特殊的类型无法处理时,可以为这种特殊类型单独实例化出一个类,这种单独的实现叫作模板的特化。
全类的第特化:按照类的格式把类完整再实现一遍(重写一遍)。
template<> class className <char*>
{
...
};
成员特化:给指定的类型提供一个特殊的的成员函数。
template<> 返回值 className<char*>::max(void)
{
...
}
局部特化:可以让用户根据实例化时的参数来指定使用的类模板
template<class A,class B> class N
{public:N(void){cout <<"1"<<endl;}};
template<class A> class N <A,A>
{public:N(void){cout <<"2"<<endl;}};
template<class A> class N <A,short>
{public:N(void){cout <<"3"<<endl;}};
template<class A> class N <A*,A*>
{public:N(void){cout <<"4"<<endl;}};
template<class A,class B> class N <A*,B*>
{public:N(void){cout <<"5"<<endl;}};
注意:同等程序的特化会引起二义性。
八、类模板的参数
1、类模板的参数可以有默认值
注意:使用类模板默认值时,<>不能省略,可以空着,但不能不写。
2、类模板后面的参数可以调用前面的参数。
3、普通数值可以对类模板进行实例化,它必须以类似变量的形式存在。
注意:只能是常量才能进行实例化。
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class Qu
{
T* arr;
int head;
int tail;
int size;
int cnt;
public:
Qu(int len)
{
arr=new T[len];
size=len;
head=0;
tail=0;
cnt=0;
}
bool empty(void)
{
return !cnt;
}
void push_head(T data)
{
if(cnt>=size)
{
arr=(T*)realloc((void*)arr,2*size*sizeof(T));
size=2*size;
}
if(head<0)
{
head=size;
}
arr[head--]=data;
cnt++;
}
void resizequ(T data,int num)
{
if(num>=cnt)
{
for(int i=0;i<num-cnt;i++)
{
push_tail(data);
}
}
else
{
for(int i=0;i<cnt-num;i++)
{
pop_head();
}
}
cnt=num;
}
void clear_qu(void)
{
delete []arr;
}
T& head_qu(void)
{
return arr[head-1];
}
T& tail_qu(void)
{
return arr[tail-1];
}
void swap_qu(Qu& q)
{
int count=cnt;
while(q.cnt>0)
{
push_tail(q.head());
q.pop_head();
}
while(count>0)
{
q.push_tail(head());
pop_head();
}
}
void push_tail(T data)
{
if(cnt>=size)
{
arr=(T*)realloc((void*)arr,2*size*sizeof(T));
size=2*size;
}
if(tail>=size)
{
tail=0;
}
arr[tail++]=data;
cnt++;
}
T& at_qu(int n)
{
int count=head;
for(int i=1;i<n;i++)
{
count++;
if(count>size)
{
count=0;
}
}
return arr[count];
}
T& operator[](int n)
{
int count=head;
for(int i=1;i<n;i++)
{
count++;
if(count>size)
{
count=0;
}
}
return arr[count];
}
int size_qu(void)
{
return cnt;
}
int max_size_qu(void)
{
return size;
}
void pop_head(void)
{
head++;
if(head>=size)
{
head=0;
}
cnt--;
}
void pop_tail(void)
{
tail--;
if(tail<0)
{
tail=size-1;
}
cnt--;
}
};
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class T>
class sstack
{
T* arr;
int len;
int top;
public:
sstack(int len)
{
arr=new T[len];
this->len=len;
top=0;
}
void spush(T data)
{
if(top>=len)
{
arr=(T*)realloc((void*)arr,2*len*sizeof(T));
len=2*len;
}
arr[top++]=data;
}
bool operator!=(sstack& m)
{
int c1=top-1;
int c2=m.top-1;
if(c1!=c2) return true;
for(int i=0;i<top;i++)
{
if(arr[c1--]!=m.arr[c2--])
{
return true;
}
}
return false;
}
bool operator==(sstack& m)
{
int c1=top-1;
int c2=m.top-1;
if(c1!=c2) return false;
for(int i=0;i<top;i++)
{
if(arr[c1--]!=m.arr[c2--])
{
return false;
}
}
return true;
}
bool spop(void)
{
if(sis_empty()) return false;
top--;
return true;
}
friend ostream& operator<<(ostream& out,const sstack& m)
{
int count=m.top-1;
for(int i=0;i<m.top;i++)
{
cout<<m.arr[count--]<<" ";
}
cout<<endl;
return out;
}
int ssize(void)
{
return top;
}
T top_stack(void)
{
return arr[top-1];
}
bool sis_empty(void)
{
return !top;
}
~sstack(void)
{
delete []arr;
}
};
#include<iostream>
using namespace std;
template<class T>
class List
{
struct Node
{
T data;
Node* next;
};
Node* head;
public:
List()
{
head=new Node;
head->next=NULL;
}
List(List& that)
{
head=new Node;
Node* node=that.head;
while(node->next!=NULL)
{
push_back(node->data);
node=node->next;
}
}
void operator=(List& that)
{
if(this==&that) return *this;
List head(that);
list_swap(head);
}
friend ostream& operator<<(ostream& out,const List& that)
{
Node* node=that.head->next;
while(node!=NULL)
{
cout<<node->data<<" ";
node=node->next;
}
cout<<endl;
return out;
}
~List(void)
{
list_clear();
//delete head;
}
void list_clear(void)
{
Node* node=head;
while(node->next!=NULL)
{
Node* tempnode=node;
node=node->next;
delete tempnode;
}
delete node;
}
void remove_val(T& data)
{
Node* prevnode=head;
Node* node=head->next;
while(node!=NULL)
{
if(node->data==data)
{
prevnode->next=node->next;
Node* tempnode=node;
node=node->next;
delete tempnode;
continue;
}
node=node->next;
prevnode=prevnode->next;
}
}
void list_swap(List& list)
{
Node* node=head->next;
head->next=list.head->next;
list.head->next=node;
}
void list_swapnode(Node*& node1,Node*&node2)
{
T temp;
temp=node1->data;
node1->data=node2->data;
node2->data=temp;
}
void list_reverse(void)
{
Node* prev=head->next;
Node* node=prev->next;
while(node!=NULL)
{
Node* nextnode=node->next;
node->next=prev;
prev=node;
node=nextnode;
}
head->next->next=NULL;
head->next=prev;
}
void sort(void)
{
Node* node=head->next;
while(node->next!=NULL)
{
Node* node2=node->next;
while(node2!=NULL)
{
if(node->data>node2->data)
{
list_swapnode(node,node2);
node2=node2->next;
continue;
}
node2=node2->next;
}
node=node->next;
}
}
void list_push_back(const T& data)
{
Node* node=head;
while(node->next!=NULL)
{
node=node->next;
}
Node* tempnode=new Node;
tempnode->data=data;
node->next=tempnode;
}
void list_push_front(const T& data)
{
Node* node=new Node;
node->data=data;
node->next=head->next;
head->next=node;
}
void list_pop_front(void)
{
Node* node=head->next;
head->next=head->next->next;
delete node;
}
void list_pop_back(void)
{
Node* node=head;
while(node->next!=NULL)
{
node=node->next;
}
delete node;
}
bool list_empty(void)
{
return head->next==NULL;
}
Node*& list_front(void)
{
if(this->is_empty()) return head;
return head->next;
}
int list_size(void)
{
if(head->next==NULL) return 0;
Node* node=head;
int count=0;
while(node->next!=NULL)
{
count++;
node=node->next;
}
return count;
}
Node*& list_back(void)
{
Node* node=head;
while(node->next!=NULL)
{
node=node->next;
}
return node;
}
};
还有vector容器的要比双向队列的简单的多,代码我就不贴出来了,但是要想让容器里的类型万能,就比较复杂了,需要对类型进行特例话,其实c++中的模板虽然很好用,实际上模板的封装十分复杂,需要很严谨的编程,才能封装出好的模板,在公司中,能封装模板的,必定是能力过硬,且编程习惯较好的人,所以模板重点是会使用。