函数模板与类模板的相关笔记 entry2
函数模板; 类模板;
类模板的一般定义形式为:
template <类型形参表> class 类名
{
类声明体;
}
建立类模板之后,可以用类型实参定义模板类,并创建实例。模板类是一个实实在在的类,对象是该模板类的势力。其格式如下:
类名 <类型实参表> 对象;
下面看几个例子
例一 :定义数组类模板
#include <iostream>
using namespace std;
template <class T>
class Array
{
private:
T *arr;
public:
Array(int c)
{
arr = new T[c];
}
void init(int i, T x)
{
arr[i] = x;
}
T& operator[](int i)
{
return arr[i];
}
};
void main()
{
Array<int> array(5);
cout << "input every element's value: " << endl;
for (int i = 0; i < 5; i++){
cin >> array[i];
}
cout << "every element's value in Array is: " << endl;
for (i = 0; i < 5; i++){
cout << "No " << i << " : " << array[i] << endl;
}
}
-------------------------------------
运行结果:
input every element's value:
6 21 27 17 40
every element's value in Array is:
No 0 : 6
No 1 : 21
No 2 : 27
No 3 : 17
No 4 : 40
例二 类模板参数是类
类模板的参数不仅可以是标准数据类型,还可以是用户自定义类型,当然包括用户自己定义的类,如下所示:
#include <iostream>
using namespace std;
class A
{
private:
int j;
public:
A(){}
A(int x):j(x){}
A(A* x)
{
j = x->j;
}
void operator!()
{
cout << "j = " << j << endl;
}
};
template<typename T>
class B
{
private:
int i;
T* x;
public:
B(int xa, T* p): i(xa){
x = new T(p);
}
void operator!()
{
cout << "i = " << i << endl;
!*x;
}
};
void main()
{
A a(1);
B<A> b(2, &a);
!b;
}
----------------------------------
运行结果:
i = 2
j = 1
例三
#include <iostream>
using namespace std;
template<class T>
class Stack
{
public:
Stack(int = 10); //default constructor(stack size 10)
~Stack(){
delete[]stackPtr; //destructor
}
bool push(const T&); //push an element onto the stack
bool pop(T&); //pop an element off the stack
private:
int size; //# of elements in the stack
int top; //location of the top element
T* stackPtr; //pointer to the stack
bool isEmpty() const{
return top == -1;
}
bool isFull() const{
return top == size-1;
}
};
//Constructor with default size 10
template<class T>
Stack<T>::Stack(int S)
{
size = S > 0 ? S : 10;
top = -1; //Stack is initially empty
stackPtr = new T[size]; //allocate space for elements
}
//Push an element onto the stack
//return true if successful false otherwise
template <class T>
bool Stack<T>::push(const T& pushValue)
{
if (!isFull()){
stackPtr[++top] = pushValue; //place item in Stafck
return true; //push successful
}
return false;
}
//Pop an element off the stack
template<class T>
bool Stack<T>::pop(T& popValue)
{
if (!isEmpty()){
popValue = stackPtr[top--]; //remove item from Stack
return true; //pop successful
}
return false;
}
void main()
{
Stack<double> doubleStack(5);
double d = 1.1;
cout << "Pushing elements onto doubleStack\n";
while (doubleStack.push(d)){
//success true returned
cout << d << ' ';
d += 1.1;
}
cout<< "\nStack is full. cannot push " << d
<< "\n\nPoping elements from doubleStack\n";
while (doubleStack.pop(d)){ //success true returned
cout << d << ' ';
}
cout << "\nStack is empty.Cannot pop\n";
Stack<int> intStack;
int i = 1;
cout << "\nPushing elements onto intStack\n";
while (intStack.push(i)){
//success true returned
cout << i << ' ';
++i;
}
cout<< "\nStack is full.Cannot push " << i
<< "\n\nPopping elements from intStack\n";
while (intStack.pop(i)){ //success true returned
cout << i << ' ';
}
cout << "\nStack is empty.Cannot pop\n";
}
--------------------------------------------------
运行结果:
Pushing elements onto doubleStack
1.1 2.2 3.3 4.4 5.5
Stack is full. cannot push 6.6
Poping elements from doubleStack
5.5 4.4 3.3 2.2 1.1
Stack is empty.Cannot pop
Pushing elements onto intStack
1 2 3 4 5 6 7 8 9 10
Stack is full.Cannot push 11
Popping elements from intStack
10 9 8 7 6 5 4 3 2 1
Stack is empty.Cannot pop
visitor tracker