01 函数模板语法
#include<iostream>
using namespace std;
//函数模板
template <typename T> //声明一个模板, 告诉编辑器后面代码中紧跟着的T不要报错,T是一个数据类型
void swapMy(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
void test()
{
int a = 10;
int b = 200;
//1.方法一:自动类型推导
//swapMy(a, b);
//方法二:显示指定类型
swapMy<int>(a, b);
cout << "a=" << a <<endl;
cout << "b=" << b<<endl;
}
int main()
{
test();
system("pause");
return 0;
}
//总结:
//函数模板利用关键字template
//使用函数模板有两种方式:自动类型推导、显示指定类型
//模板的目的是为了退稿复用性,将类型参数化
#include<iostream>
using namespace std;
//1.自动类型推导,必须推导出一致数据类型T才可以使用
template <class T>
void Swap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
//2.模板必须确定出T的数据类型,才可以使用
template <class T>
void Func()
{
cout << "func调用" << endl;
}
void test1()
{
int a = 23;
int b = 200;
char c = 30;
Swap(a, b);//正确
//Swap(a, c); //错误,类型不一致
cout << "a=" << a << endl;
cout << "b=" << b << endl;
}
void test2()
{
//Func(); //错误,模板不能独立使用,必须确定出T的类型
Func<int>(); //利用显示指定类型,给T一个类型,才可以使用该模板
}
int main()
{
test1();
test2();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
//实现通用, 对数组进行排序的函数
//规则 从大到小
//算法 选择
//测试 char 数组, int数组
//提供打印模板
template <class T>
void Print(T arr[], int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
//交换数组模板
template <class T>
void Swap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
template <class T>
void mySort(T arr[], int len)
{
for (int i = 0; i < len; i++)
{
int max = i; //认定最大值的下标
for (int j = i + 1; j < len; j++)
{
//认定的最大值比遍历出的数值要小,说明j下标的元素才是真正的最大值
if (arr[max] < arr[j])
{
max = j; //更新最大值下标
}
}
if (max != i)
{
//交换max和i元素
Swap(arr[max], arr[i]);
}
}
}
//测试char数组
void test01()
{
char marray[] = "sdanmfdf";
int num = sizeof(marray) / sizeof(marray[0]);
mySort(marray, num);
Print(marray, num);
}
//测试int数组
void test02()
{
int marray[] = {1,3, 5, 4, 45, 6 ,8};
int num = sizeof(marray) / sizeof(marray[0]);
mySort(marray, num);
Print(marray, num);
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
// 1.普通函数调用可以发生隐式类型转换
//2.函数模板 用自动类型推导,不可以发生隐式类型转换
//3.函数模板 用显示指定类型,可以发生隐式类型转换
//普通函数
int add(int a, int b)
{
return a + b;
}
//函数模板
template <class T>
T Tadd(T a, T b)
{
return a + b;
}
void test1()
{
int a = 10;
int b = 23;
char c = 'c'; //a = 97 →c = 99
//cout << add(a, b) << endl;
//自动类型推导
cout << add(a, c) << endl;
//cout << Tadd(a, b) << endl;
//cout << Tadd(a, c) << endl; //报错,不可以发生隐式类型转换
cout << Tadd<int>(a, c) << endl; //显示指定类型正确
}
int main()
{
test1();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
//普通函数与函数模板调用规则
//1.如果函数模板和普通函数可以调用,优先调用普通函数
//2.可以通过空模板参数列表 强制调用 函数模板
//3.函数模板可以发生重载
//4.如果函数模板可以产生更好的匹配,优先调用函数模板
void Func(int a, int b)
{
cout << "普通函数调用" << endl;
}
template <class T>
void Func(T a, T b)
{
cout << "模板调用" << endl;
}
template <class T>
void Func(T a, T b, T c)
{
cout << "模板传三个调用" << endl;
}
void test01()
{
/*int a = 10;
int b = 30;*/
//Func(a, b); //调用普通函数
//Func<>(a, b); //通过空模板参数列表,强制调用函数模板
//Func(a, b, 'c') //如果函数模板产生更好的匹配,优先调用函数模板
char a = 'a';
char b = 'b';
Func(a, b);
}
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
//模板的局限性
//模板并不是万能的,有些特定的数据类型,需要用具体化方式做特殊实现
class Person {
public:
Person( string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
//比较两个函数是否相等
template <class T>
bool Compare(T &a, T &b)
{
if (a == b)
{
return true;
}
else
{
return false;
}
}
//利用具体化Person的版本实现代码,具体优化先调用
template<> bool Compare(Person& p1, Person& p2)
{
if (p1.m_name == p2.m_name && p1.m_age == p2.m_age)
{
return true;
}
else
{
return false;
}
}
void test01()
{
int a = 10;
int b = 30;
bool ret = Compare(a, b);
if (ret == true)
{
cout << "真的" <<endl;
}
else
{
cout << "假的" << endl;
}
}
void test02()
{
Person p1( "张三", 10);
Person p2("李四", 20);
bool ret = Compare(p1, p2);
if (ret)
{
cout << "真的" << endl;
}
else
{
cout << "假的的" << endl;
}
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
评论列表 (0 条评论)