目录
泛型,是一种将类型参数化以达到代码复用的技术,C++中使用模板来实现泛型
模板的使用格式如下
template <typename\class T>
typename和class是等价的
模板没有被使用时,是不会被实例化出来的
模板的声明和实现如果分离到.h和.cpp中,会导致链接错误
一般将模板的声明和实现统一放到一个.hpp文件中
函数模板
template <typename T>
void swapValues(T& v1, T& v2){
T temp = v1;
v1 = v2;
v2 = temp;
}
int main(){
int a = 10;
int b = 20;
swapValues<int>(a,b);
}
多参数模板
template <class T1, class T2>
void display(const T1 &v1, const T2 &v2){
cout << v1 << endl;
cout << v2 << endl;
}
display(20, 1.7);
类模板之动态数组
#pragma once
#include <iostream>
using namespace std;
template <typename Item>
class Array {
// 重载的<<运算符 友元函数支持泛型要加上<>
friend ostream &operator<<<>(ostream &, const Array<Item> &);
// 用于指向首元素
Item *m_data;
// 元素个数
int m_size;
// 容量
int m_capacity;
void checkIndex(int index);
public:
Array(int capacity = 0);
~Array();
void add(Item value);
void remove(int index);
void insert(int index, Item value);
Item get(int index);
int size();
Item operator[](int index);
};
template <typename Item>
Array<Item>::Array(int capacity) {
m_capacity = (capacity > 0) ? capacity : 10;
// 申请堆空间
m_data = new Item[m_capacity];
}
template <typename Item>
Array<Item>::~Array() {
if (m_data == NULL) return;
delete[] m_data;
}
template <typename Item>
void Array<Item>::checkIndex(int index) {
if (index < 0 || index >= m_size) {
// 报错:抛异常
throw "数组下标越界";
}
}
template <typename Item>
void Array<Item>::add(Item value) {
if (m_size == m_capacity) {
// 扩容
/*
1.申请一块更大的新空间
2.将旧空间的数据拷贝到新空间
3.释放旧空间
*/
cout << "空间不够" << endl;
return;
}
m_data[m_size++] = value;
}
template <typename Item>
void Array<Item>::remove(int index) {
checkIndex(index);
}
template <typename Item>
void Array<Item>::insert(int index, Item value) {
}
template <typename Item>
Item Array<Item>::get(int index) {
checkIndex(index);
return m_data[index];
}
template <typename Item>
int Array<Item>::size() {
return m_size;
}
template <typename Item>
Item Array<Item>::operator[](int index) {
return get(index);
}
template <typename Item>
ostream &operator<<<>(ostream &cout, const Array<Item> &array) {
cout << "[";
for (int i = 0; i < array.m_size; i++) {
if (i != 0) {
cout << ", ";
}
cout << array.m_data[i];
}
return cout << "]";
}