类模板定义
templete <class T> // 模板的参数列表 <class T>
class Array
{
public:
Array(int size);
void display();
private:
T* m_pArr;
int m_iSize;
int m_iLength;
};
// 成员函数定义
templete <typename T>
Array<T>::Array(int size)
{
...
}
templete <class T>
Array<T>::display()
{
...
}
类模板实例化为模板类
Array<int> arr(10);
Array<double>* arr1 = new Array<double>(10);
多参数类模板定义
templete <class T, int size>
class Array
{
public:
Array();
void display();
private:
T* m_pArr;
int m_iSize;
int m_iLength;
};
templete <typename T, int size>
Array<T,size>::Array()
{
m_iSize = size;
...
}
templete <class T, int size>
Array<T,size>::display()
{
...
}
例程
#include <iostream>
#include <stdlib.h>
template <class T>
class Array
{
public:
Array(int size);
~Array();
bool push(T data); // 向数组中保存元素
void print();
private:
T *m_pArr; // 指向分配的内存
int m_iSize; // 数组大小
int m_iLen; // 数组元素个数
};
template <class T>
Array<T>::Array(int size)
{
m_iSize = size; // 数组大小
m_iLen = 0; // 数组元素个数
m_pArr = new T[m_iSize]; // 分配内存大小
}
template <class T>
Array<T>::~Array()
{
delete m_pArr;
}
template <class T>
bool Array<T>::push(T data)
{
if (m_iLen >= m_iSize)
return false;
m_pArr[m_iLen] = data;
m_iLen++;
return true;
}
template <class T>
void Array<T>::print()
{
for (int i = 0; i < m_iLen; i++)
std::cout << m_pArr[i] << std::endl;
}
int main()
{
Array<int> arr(10);
for (int i = 0; i < 10; i++)
arr.push(i);
arr.push(10);
arr.print();
Array<double> *arr1 = new Array<double>(10);
for (int i = 0; i < 10; i++)
arr1->push(i*2.1);
arr1->push(10);
arr1->print();
delete arr1;
system("pause");
return 0;
}
运行结果:
0
1
2
3
4
5
6
7
8
9
0
2.1
4.2
6.3
8.4
10.5
12.6
14.7
16.8
18.9
例程:对上面例程参数进行修改,运行结果一致
#include <iostream>
#include <stdlib.h>
template <class T, int size>
class Array
{
public:
Array();
~Array();
bool push(T data); // 向数组中保存元素
void print();
private:
T *m_pArr; // 指向分配的内存
int m_iSize; // 数组大小
int m_iLen; // 数组元素个数
};
template <class T, int size>
Array<T,size>::Array()
{
m_iSize = size; // 数组大小
m_iLen = 0; // 数组元素个数
m_pArr = new T[m_iSize]; // 分配内存大小
}
template <class T, int size>
Array<T, size>::~Array()
{
delete m_pArr;
}
template <class T, int size>
bool Array<T, size>::push(T data)
{
if (m_iLen >= m_iSize)
return false;
m_pArr[m_iLen] = data;
m_iLen++;
return true;
}
template <class T, int size>
void Array<T, size>::print()
{
for (int i = 0; i < m_iLen; i++)
std::cout << m_pArr[i] << std::endl;
}
int main()
{
Array<int,10> arr;
for (int i = 0; i < 10; i++)
arr.push(i);
arr.push(10);
arr.print();
Array<double,10> *arr1 = new Array<double,10>();
for (int i = 0; i < 10; i++)
arr1->push(i*2.1);
arr1->push(10);
arr1->print();
delete arr1;
system("pause");
return 0;
}
例程:参数是自定义类型
#include <iostream>
#include <stdlib.h>
#include <ostream>
template <class T, int size>
class Array
{
public:
Array();
~Array();
bool push(T data); // 向数组中保存元素
void print();
private:
T *m_pArr; // 指向分配的内存
int m_iSize; // 数组大小
int m_iLen; // 数组元素个数
};
template <class T, int size>
Array<T,size>::Array()
{
m_iSize = size; // 数组大小
m_iLen = 0; // 数组元素个数
m_pArr = new T[m_iSize]; // 分配内存大小
}
template <class T, int size>
Array<T, size>::~Array()
{
delete m_pArr;
}
template <class T, int size>
bool Array<T, size>::push(T data)
{
if (m_iLen >= m_iSize)
return false;
m_pArr[m_iLen] = data;
m_iLen++;
return true;
}
template <class T, int size>
void Array<T, size>::print()
{
for (int i = 0; i < m_iLen; i++)
std::cout << m_pArr[i] << std::endl;
}
class Coor
{
// 重载 <<
friend std::ostream &operator<<(std::ostream &out, Coor &coor);
public:
Coor(int x = 0, int y = 0);
private:
int m_iX;
int m_iY;
};
Coor::Coor(int x, int y)
{
m_iX = x;
m_iY = y;
}
// 有元函数不需要加 Coor:: 标识
std::ostream &operator<<(std::ostream &out, Coor &coor)
{
out << coor.m_iX << "," << coor.m_iY;
return out;
}
int main()
{
Array<Coor,10> arr;
Coor coor1(1, 2);
Coor coor2(3, 4);
arr.push(coor1);
arr.push(coor2);
arr.print();
system("pause");
return 0;
}
运行结果:
1,2
3,4