目录
特化模板函数:即在实例化模板时,对特定类型的实参进行特殊处理,即实例化一个特殊的实例版本,当以特化定义时的形参使用模板时,将调用特化版本
一. 模板函数
函数模板的定义形式:
template<模板参数列表>
类型名 函数名(参数表){
函数体的定义
}
模板参数表由逗号分隔的模板参数构成,包括以下内容:
(1)class(或typename)标识符,指明可以接受一个参数类型。这个类型参数代表的是类型,可尔一世预定义类型或自定义类型
(2)类型说明符 标识符,知名接受一个由“类型说明符”所规定类型的常量作为参数
(3)template<参数表>class标识符,指明可以接收一个类模板作为参数。类型参数可以用来指定函数模板本身的形参类型、返回值类型,以及声明函数中的局部变量
一般模板函数
实现代码:
template<typename T>
int compare(const T& left,const T& right) {
if (left > right) {
return 1;
}
if (left <right) {
return -1;
}
}
/*引用模板函数*/
compare<int>(2,3);
运行结果
特化模板函数:即在实例化模板时,对特定类型的实参进行特殊处理,即实例化一个特殊的实例版本,当以特化定义时的形参使用模板时,将调用特化版本
代码实现:
/*特化函数模板*/
template<>
int compare(const char* left, const char* right) {
std::cout << "特化函数模板:" << std::endl;
return strcmp(left, right); /*strcmp函数返回值只有0,1,-1,相等返回0,left>right返回1,反之返回-1*/
}
const char* left = "abcdd";
const char* right = "abc";
运行结果:
二.类模板(queue and stack)
类模板:是对不同类的公共性质的抽象。使用类模板,使得类中的某些数据成员、成员函数的参数、返回值或局部变量能取不同类型
类模板声明的语法形式:
template<模板参数表>
class 类名{
类成员声明
}
如果需要在类声明以外定义其成员函数,则:
template<模板参数列表>
类型名 类名<模板参数标识符列表>::函数名(参数表)
使用一个模板类来建立对象时,应按如下形式:
模板名<模板参数表> 对象名1,对象名2....对象名n;
队列类
队列只能在一端添加元素(入队),另一端删除元素(出队)
队列类的数据成员包括:队列元素、队头指针和队尾指针
/*类模板queue*/
template<class T, int SIZE = 50>
class Queue {
private:
int front, rear, count; //队头指针、队尾指针、元素个数
T list[SIZE]; //队列元素数组
public:
Queue(); //构造函数,初始化队头指针、队尾指针、元素个数
void insert(const T &item); //新元素入队
T remove(); //元素出队
void clear(); //清空队列
const T &getFront() const; //访问队首元素
//测试队列状态
int getLength() const; //求队列长度
bool isEmpty() const; //判断队列是否为空
bool isFull() const; //判断队列是否满了
};
//构造函数,初始化队头指针、队尾指针、元素个数
template<class T, int SIZE>
Queue<T, SIZE> :: Queue() : front(0), rear(0), count(0) { }
template<class T, int SIZE>
void Queue<T, SIZE> : : insert(const T &item) { //入队
assert(count != SIZE);
count++;
list[rear] = item; //向队尾插入元素
rear = (rear + 1) % SIZE; //队尾指针+1,取余运算实现循环队列
}
template<class T, int SIZE>
T Queue<T, SIZE> : : remove() { //删除队尾元素,并返回该元素的值
assert(count != 0);
int temp = front; //记录原队首指针
count--;
front = (front + 1) % SIZE;
return list[temp]; //返回首元素值
}
template<class T, int SIZE>
const T& Queue<T, SIZE>: : getFront() const { //访问队列首元素
return list[front];
}
template<class T, int SIZE>
int Queue<T, SIZE> : : getLength() const {
return count;
}
template<class T, int SIZE>
bool Queue<T, SIZE> : : isEmpty() const {
return count == 0;
}
template<class T, int SIZE>
bool Queue<T, SIZE> : : isFull() const {
return count = SIZE;
}
template<class T, int SIZE>
void Queue<T, SIZE> : : clear() {
count = 0;
front = 0;
rear = 0;
}
#endif // !QUEUE_H
运行报错
这个错误出乎意料!!! 结果发现只要把“::”之间空格去掉就运行成功了!!?(一整个有被无语住)
运行结果如下:
栈类
栈的基本状况有:一般状态、栈空、栈满。当栈中没有元素时称为栈空;当栈中元素个数达到上限时,称为栈满;栈中有元素,但未达到元素上限时是一般状态。栈类的基本操作:初始化、入栈、出栈、栈清、访问栈顶元素、检测栈的状态。栈顶指针可以指向实际的栈顶元素,也可以指向下一个可用空间。(和队列类差不多,举一反三!!)
/*Stack栈类模板*/
template<class T,int SIZE=50>
class Stack {
private:
T list[SIZE];
int top;
public:
Stack();
void push(const T& item);
T pop();
void clear();
const T& peek() const;
bool isEmpty() const;
bool isFull() const;
};
template<class T,int SIZE>
Stack<T,SIZE>::Stack():top(-1){}
template<class T,int SIZE>
void Stack<T, SIZE>::push(const T& item) {
assert(!isFull());
list[++top] = item;
}
template<class T, int SIZE>
T Stack<T, SIZE>::pop() { //将栈顶元素出栈
assert(!isEmpty());
cout << "出栈" << endl;
return list[top--]; //返回栈顶元素,并将其弹出栈
}
template<class T, int SIZE>
const T& Stack<T, SIZE>::peek() const { //访问栈顶元素
assert(!isEmpty()); //若栈为空则报错
return list[top]; //返回栈顶元素
}
template<class T, int SIZE>
bool Stack<T, SIZE>::isFull() const {
return top == SIZE-1;
}
template<class T, int SIZE>
bool Stack<T, SIZE>::isEmpty() const {
return top == -1;
}
template<class T, int SIZE>
void Stack<T, SIZE>::clear() {
top = -1;
}
#endif // !STACK_H