使用函数模板,一个未知类型和多个未知类型。
#include<iostream>
using namespace std;
#include <string>
//模板:数据类型当成参数
//template<class T>
//template<typename T>
template<typename _TY>//一个未知类型
_TY max(_TY a, _TY b)
{
return a > b ? a : b;
}
template<class _TY1, class _TY2, class _TY3>//多个未知类型
void print(_TY1 one, _TY2 two, _TY3 three)
{
cout<< one <<endl;
cout << two << endl;
cout << three << endl;
}
int main()
{
//隐式调用
cout << max(1, 2) << endl;
cout << max("abc", "bcd") << endl;
//显式调用
cout << max<double>(1.1, 1.3) << endl;
print(1, "string", 1.1);
print<string,string,string>("string1", "string2", "stirng3");
while (1);
return 0;
} #学习路径#
查看13道真题和解析