模板
作用:书写与类型无关的程序
把类型当成参数来传递
模板是泛型编程的基础,泛型编程即以一种独立于任何特定类型的方式编写代码。
模板是创建泛型类或函数的蓝图或公式。库容器,比如迭代器和算法,都是泛型编程的
例子,它们都使用了模板的概念。
函数模板
template <class T, class T1 ....> 返回值 函数名 (参数 )
1.函数模板的模板参数可以有默认参数
2.函数模板的函数参数可以有非模板参数的参数,参数也可以是默认参数
代码示例
/*===============================================
* 文件名称:template.cpp
* 创 建 者:
* 创建日期:2023年09月06日
* 描 述:
================================================*/
#include <iostream>
using namespace std;
template <class T , class T1 > T add( T a , T1 b , int c = 1)
{
cout<<c<<endl;
return a+b;
}
int main()
{
int a = 100 ;
double b = 1.0201;
char c = 'A';
cout<<"C=1000 "<<add(a,b,1000)<<endl;
cout<<"C=1 "<<add(a,b)<<endl;
return 0;
}
类模板
一般形式
template <class T....>
class A{
T a;
}
特点:类模板创建对象时必须,显式调用
类模板的友元函数,必须是一个函数模板
代码示例
/*===============================================
* 文件名称:模板类数组.cpp
* 创 建 者:
* 创建日期:2023年09月07日
* 描 述:
================================================*/
#include <iostream>
using namespace std;
const int SIZE = 256 ;
template <class T>
class Arr
{
private:
int len ;
int size;
T *addr ;
static int count;
public:
//普通构造🐶
Arr( const int len = 512)
{
this->size = len+SIZE;
this->len = 0;
this->addr = new T[size];
count++;
}
// 类似于拷贝构造🐣
Arr(T * addr , int len )
{
this->size = len + SIZE;
this->len = len ;
this->addr = new T[this->size];
for( int i = 0 ; i < len ;i++ )
{
this->addr[i] = addr[i];
}
count++;
}
//拷贝构造🏇
Arr(const Arr <T> & obj)
{
this->size = obj.size;
this->len = 0 ;
this->addr = new T[this->size];
for( int i = 0 ; i < this->len ;i++ )
{
this->addr[i] = obj.addr[i];
}
count++;
}
//移动构造🦆
Arr(Arr <T> && obj)
{
this->size = obj.size;
this->len = obj.len;
this->addr = obj.addr;
obj.addr = nullptr;
count++;
}
//析构
~Arr()
{
if(addr != nullptr )
{
delete []addr;
}
count--;
}
//重载=🐮
Arr <T> & operator =(const Arr <T> &obj)
{
if( obj.len > this->size)
{
Arr <T> temp(obj);
this->size = temp.size;
this->len = temp.len;
delete []this->addr;
this->addr = temp.addr;
temp.addr = nullptr;
}
else
{
for( int i = 0 ; i < obj.len ; i++ )
{
this->addr[i] = obj.addr[i];
}
this->len = obj.len;
}
return *this;
}
//重载+🐏
Arr <T> operator +(Arr <T> & obj)
{
Arr <T> temp(this->len+obj.len);
temp.len = this->len+obj.len;
for( int i = 0 ; i < this->len ; i++ )
{
temp.addr[i] = this->addr[i];
}
for( int i = 0 ; i < obj.len ; i++ )
{
temp.addr[i+this->len] = obj.addr[i];
}
return temp;
}
//重载[]🐺
T & operator [](int index)
{
return addr[index] ;
}
//重载*🙈
T * data()
{
return addr;
}
//🐲
static int get_count()
{
return count;
}
//
int get_len()
{
return this->len;
}
//重载++🐷
Arr <T> & operator ++()
{
for( int i = 0 ; i < this->len ; i++ )
{
(this->addr[i])++;
}
return *this;
}
//重载--🐼
Arr <T> & operator --()
{
for( int i = 0 ; i < this->len ; i++ )
{
(this->addr[i])--;
}
return *this;
}
template <class G>
friend ostream & operator <<(ostream & os ,const Arr <G> & obj);
template <class G>
friend istream & operator >>(istream & is , Arr <G> & obj);
};
template <class G>
ostream & operator <<(ostream & os ,const Arr <G> & obj)
{
for( int i = 0 ; i < obj.len ; i++ )
{
os<<obj.addr[i]<<" ";
}
os<<endl;
return os;
}
template <class G>
istream & operator >>(istream & is , Arr <G> & obj)
{
for( int i=0 ; i < obj.len ; i++ )
{
is>>obj.addr[i];
}
return is;
}
template <class T>
int Arr<T>::count = 0;
int main()
{
int arr[]={1,2,3,4,5};
Arr<int> a(arr,5);
cout<<"a:"<<a;
++a;
cout<<"a++;a:"<<a<<endl;
--a;
cout<<"a--;a:"<<a<<endl;
Arr<int> b = a;
cout<<"Arr <int> b = a; b:"<<b<<endl;
Arr <int> c = b;
c = a+b ;
cout<<"Arr <int> c = a+b ;c:"<<c<<endl;
cout<<"c len: "<<c.get_len()<<" c data:"<<c.data()<<endl;
int *p = c.data();
*p = 9999;
cout<<"*p=9999,c:"<<c;
c[0] = 8989;
cout<<"c[0] = 8989; c:"<<c<<endl;
cout<<"count:"<<Arr<int> :: get_count()<<endl;
cin>>c;
cout<<"cin>>c;c:"<<c<<endl;
return 0;
}
结果