函数模板
1.把类型当作未知量
2.语法
template <typename _Ty>
_Ty function(_Ty a,_Ty b)
{
return a>b?a:b;
}
template <class _Ty1,class _Ty2>
void print(_Ty1 a,_Ty2 b)
{
cout<< a << b <<endl;
}
3.调用
(1)隐式调用
(2)显示调用:函数名<类型名>(参数)
#include <iostream>
#include <string>
using namespace std;
template <class _Ty1,class _Ty2>
void print(_Ty1 a,_Ty2 b)
{
cout<< a << b <<endl;
}
int main()
{
print(1,2);//隐式调用
print<string,string>("aa","bb");//显示调用
return 0;
}
4.函数模板的两种形态
(1)普通函数当作函数模板
(2)类的成员函数当作模板,类外声明不可省略
5.函数模板特殊写法
(1)缺省写法,显示调用时,可以少传类型,但不可少参数
(2)存在一个常量类型。不缺省,不能隐式调用
#include <iostream>
#include <string>
using namespace std;
template <class _Ty1,class _Ty2=int>
void print(_Ty1 a, _Ty2 b)
{
cout << a << "\t" << b << endl;
}
class A
{
public:
template <class _Ty>
void print1(_Ty data)
{
cout << data << endl;
}
template <class _Ty>
void print2(_Ty data);
protected:
};
template <class _Ty>
void A::print2(_Ty data)
{
cout << data << endl;
}
template <class _Ty1,size_t size/*unsigned int*/>
void printarr(_Ty1 array)
{
for (int i = 0; i < size;i++)
{
cout << array[i] << "\t";
}
}
int main()
{
A a;
a.print2(123);
print<string>("aaa", 123);
int arr[3] = { 1, 2, 3 };
printarr<int*, 3>(arr);
return 0;
}
类模板
1.声明
template <class _Ty>
class A
{
public:
protected:
};
2.必须要显示调用
3.类模板不是实际的类:类名<未知类型>
#include <iostream>
#include <string>
using namespace std;
template <class _Ty>
class Data
{
public:
Data(){}
Data(string str) :str(str){}
void print();
protected:
string str;
};
template <class _Ty>
void Data<_Ty>::print()
{
cout << str << endl;
}
template <class _Ty>
class data : public Data<_Ty>
{
public:
data(string str) :Data<_Ty>(str){};
protected:
};
template < class _Ty1,class _Ty2>
class AA
{
public:
AA(_Ty1 one, _Ty2 two) :one(one), two(two){}
void Print();
protected:
_Ty1 one;
_Ty2 two;
};
template < class _Ty1, class _Ty2>
void AA<_Ty1, _Ty2>::Print()
{
cout << one << "\t" << two << endl;
}
int main()
{
Data<int> a;
data<int> b("bbb");
b.print();
AA<string, int> aa("aaa", 123);
aa.Print();
return 0;
}
自定义类型当作模板参数
1.基本自定义类型
2.自定义类型也是一个模板
3.重载运算符
#include <iostream>
#include <string>
using namespace std;
class Boy
{
public:
Boy(string name, int age) :name(name), age(age){}
friend ostream& operator<<(ostream& out,Boy& boy)
{
out << boy.name << "\t" << boy.age;
return out;
}
bool operator>(Boy boy)
{
return this->age > boy.age;
}
protected:
string name;
int age;
};
template <class _Ty>
void print(_Ty one)
{
cout << one << endl;
}
template <class _Ty>
_Ty Max(_Ty one,_Ty two)
{
return one > two ? one : two;
}
int main()
{
print(Boy("name0", 0));
Boy boy1("name1", 1);
Boy boy2("name2", 2);
Boy result=Max(boy1, boy2);
cout << result << endl;
return 0;
}
模板的嵌套
#include <iostream>
#include <string>
using namespace std;
template <class _Ty1,class _Ty2>
class Boy
{
public:
Boy(_Ty1 name, _Ty2 age) :name(name), age(age){}
friend ostream& operator<<(ostream& out, Boy& boy)
{
out << boy.name << "\t" << boy.age;
return out;
}
protected:
_Ty1 name;
_Ty2 age;
};
template <class _Ty>
void print(_Ty temp)
{
cout << temp << endl;
}
template <class _Ty1,class _Ty2>
class Data
{
public:
Data(_Ty1 one, _Ty2 two) :one(one), two(two){}
void print()
{
cout << one << "\t" << two << endl;
}
protected:
_Ty1 one;
_Ty2 two;
};
int main()
{
//隐式调用
print(Boy<string, int>("name0", 0));
//显示调用
print<Boy<string,int>>(Boy<string, int>("name1", 1));
//类嵌套
Data<Boy<string, int>, Boy<double, double>>
data(Boy<string, int>("name2", 2), Boy<double, double>(0, 1));
data.print();
return 0;
}
模板重载
1.优先调用普通函数
2.其次调用相似度高的模板函数
#include <iostream>
#include <string>
using namespace std;
void print(string a, int b)
{
cout << "普通函数" << endl;
}
template <class _Ty>
void print(_Ty a, _Ty b)
{
cout << "一个未知类型" << endl;
}
template <class _Ty1,class _Ty2>
void print(_Ty1 a, _Ty2 b)
{
cout << "两个未知类型" << endl;
}
int main()
{
print(string("aa"), 11); //普通函数
print(11, string("aa"));//两个未知类型
print(string("aa"), string("aa"));//一个未知类型
return 0;
}
模板特殊化
//原先模板
class A
{
public:
protected:
};
//局部特殊化处理
template <class _Ty>
class A<_Ty,_Ty>
{
public:
protected:
};
//完全特化
template <>
class A<string,string>
{
public:
protected:
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)