一,为什么有泛型模板
是两次加载函数 第一次是函数名 第二次是调用函数的名
编辑器 会帮你生成函数名不同函数
是为了方便继承别人的函数的 的框架 , 以及数据结构
例子:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
void myswap(int &a, int &b)
{
int c = 0;
c = a;
a = b;
b = c;
}
void myswap2(char &a, char &b)
{
char c;
c = a;
a = b;
b = c;
}
//1, 泛型的使用
template <typename T>
void myswap3(T &a, T &b)
{
T c;
c = a;
a = b;
b = c;
}
int main(void)
{
//1,
int x = 10;
int y = 20;
myswap(x, y);
printf("%d, y = %d\n", x, y);
//2,
char a = 'a';
char b = 'b';
myswap2(a, b);
printf("a = %c, b = %c\n", a, b);
//3,函数模板的调用自动类型
int xx = 100;
int xy = 20;
myswap3<int>(xx, xy);
printf("xx = %d, xy = %d\n", xx, xy);
//4,char类型
char aa = 'c';
char ab = 'o';
myswap3<char>(aa, ab);
printf("aa = %c, ab = %c\n", aa, ab);
//5
char h = 'h';
char d = 'd';
myswap3(h, d);
printf("h = %c, d = %c\n", h, d);
printf("\n");
system("pause");
return 0;
}
二,泛型的增强
例子:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
//字符数组 int数组进行排序
template <typename T,typename T2>
int mysort(T *array, T2 size)
{
T2 tmp;
if (array == NULL)
{
return -1;
}
T2 i, j;
for ( i = 0; i < size; i++)
{
for ( j = 0; j < size; j++)
{
if (array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
return 0;
}
//泛型模板的 套路
template <typename T, typename T2>
void myPrint(T *array, T2 size)
{
int i;
for (i = 0; i < size; i++)
{
//printf("array[%d] = %T\n", i, array[i]);
cout << "array[" << i << "] =" << array[i] << endl;
}
}
int main(void)
{
//1,int类型的
int array[] = { 11, 222, 444, 44, 33 };
int size = sizeof(array) / sizeof(*array);
mysort<int, int>(array, size);
printf("排序之后\n");
myPrint<int, int>(array, size);
//2,char 类型
char buf[] = "adfdkfjdlchenli";
int len = strlen(buf);
mysort<char, char>(buf, len);
//排序之后
printf("排序之后\n");
myPrint<char, char>(buf, len);
printf("\n");
system("pause");
return 0;
}
三,函数模板和普通函数在一起调用的规则
1,普通函数和模板函数的参数相同时优先执行普通函数
**2,//重点
Max(‘a’, 43); //普通函数 函数模板严格类型一样的模型**
例子:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
//
void Max(int a, int b)
{
cout << "普通函数" << endl;
}
template <typename T>
void Max(T a, T b)
{
cout << "模板函数" << endl;
}
template <typename T>
void Max(T a, T b, T c)
{
cout << "模板函数" << endl;
}
int main(void)
{
int a = 4;
int b = 3;
Max(a, b); // 普通函数和模板函数的参数相同时优先执行普通函数
//如果想用函数模板<>
Max<>(a, b);
Max(4.5, 5.5); //函数模板匹配
Max(4, 5, 5); //函数模板
//重点
Max('a', 43); //普通函数 函数模板严格类型一样的模型
printf("\n");
system("pause");
return 0;
}
四,函数模板遇上函数重载
1,普通函数的调用:可以进行隐士的类型转换
2,调用函数模板(本质:类型参数化将严格遵循类型匹配)
列子:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
template <typename T>
void myswap(T &a, T &b)
{
T c = 0;
c = a;
a = b;
b = c;
cout << "泛型模板" << endl;
}
void myswap(int a, char c)
{
cout << "a:" << a << "c:" << c << endl;
cout << "函数重载" << endl;
}
int main(void)
{
int a = 10;
char c = 'c';
myswap(a, c); //普通函数的调用:可以进行隐士的类型转换
myswap(a, a); //调用函数模板(本质:类型参数化将严格遵循类型匹配)
myswap(c, c);
printf("\n");
system("pause");
return 0;
}
五,单个类模板
**重点::类模板的定义 一定 在main()中 指定类型 “A(int)”
类模板做函数参数**
例子:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
/*
*************************************************
类模板的定义 一定 在main()中 指定类型 "A(int)"
类模板做函数参数
*************************************************
*/
//A模板类
//模板类 类型参数化
template <typename T>
class A
{
public:
A(T a = 0)
{
this->a = a;
}
void printA()
{
cout << "a: " << a << endl;
}
~A()
{
cout << "析构函数" << endl;
}
private:
T a;
};
//类模板的 重点要指定类型 <int>
void UseA(A<int> &a)
{
a.printA();
}
int main(void)
{
//模板类
A<int> al(11), ad(45); //模板类是抽象的 === >需要进行 类型具体的类
al.printA();
UseA(ad);
printf("\n");
system("pause");
return 0;
}
六,继承的类中类模板
**
模板类要派生 ,需要具体化模板类, C++编辑器需要知道 父类数据类型
—– >要知道父类 **
例子:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
template <typename T>
class A
{
public:
A(T a)
{
this->a = a;
}
void print()
{
cout << "a:" << a << endl;
}
~A()
{
}
T a;
};
//模板类要派生 ,需要具体化模板类, C++编辑器需要知道 父类数据类型
//------ >要知道父类
class B : public A<int>
{
public:
B(int a = 10,int b = 20 ) : A<int>(a)
{
this->b = b;
}
void prinfB()
{
cout << "a: " << a << "b: " << b << endl;
}
~B()
{
}
private:
int b;
};
//从模板类派生模板类
template <typename T>
class C : public A<T>
{
public:
C(T c, T a) : A<T>(a)
{
this->c = c;
}
void printC()
{
cout << "c: " << c << endl;
}
protected:
T c;
};
int main(void)
{
B b1(2, 4);
b1.prinfB();
C<int> c(4, 4);
c.printC();
printf("\n");
system("pause");
return 0;
}