Day34.C++08
001.函数模板的基本使用
模板概论:
C++提供了函数模板,所谓函数模板,实际上就是建立一个通用函数,其函数类型和形参类型不具体制定,用一个虚拟的类型来代表。这个通用函数就成为函数模板。
凡是函数体相同的函数都可以用这个模板代替,不必定义多个函数,只需在模板中定义一次即可。在调用函数时系统会根据实参的类型来取代模板中的虚拟类型,从而实现不同函数的功能。
代码示例:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
void mySwapInt(int& a, int& b)
{
int tmp = a;
a = b;
b = tmp;
}
void mySwapDouble(double& a, double& b)
{
double tmp = a;
a = b;
b = tmp;
}
template<class T>
void mySwap(T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
template<typename T>
void mySwap2()
{
}
void test01()
{
int a = 10;
int b = 20;
mySwap(a, b);
mySwap<int>(a, b);
mySwap2<double>();
cout << "a = " << a << " b = " << b << endl;
double c = 3.14;
double d = 1.5;
mySwap(c, d);
cout << "c = " << c << " d = " << d << endl;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
002.练习-利用模板对int类型和char类型的数组进行排序
代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
template<class T>
void mySwap(T& a, T& b)
{
T tmp = a;
a = b;
b = tmp;
}
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)
{
if (arr[max] < arr[j])
{
max = j;
}
if (max != i)
{
mySwap(arr[max], arr[i]);
}
}
}
}
template<class T>
void myPrintArray(T arr[],int len)
{
for (int i = 0; i < len; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
}
void test01()
{
int intArr[] = { 5,96,8,9,7,5,1,2,3,5654,4154,125315,2246 };
int len1 = sizeof(intArr) / sizeof(intArr[0]);
mySort<int>(intArr, len1);
myPrintArray<int>(intArr, len1);
char charArray[] = "helloworld";
int len2 = sizeof(charArray) / sizeof(char);
mySort(charArray, len2);
myPrintArray(charArray, len2);
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
003.函数模板与普通函数的区别以及调用规则
结论:
- 普通函数与函数模板的区别
函数模板不可以进行隐式类型转换
普通函数可以进行隐式类型转换
- 普通函数和函数模板的调用规则
1. 如果出现重载,优先使用普通函数调用(如果没有实现,出现错误)
2. 如果想强制调用模板,那么可以使用空参数列表
3. 函数模板可以发生重载
4. 如果函数模板可以产生更好的匹配,那么优先调用函数模板
代码示例:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
template<class T>
T myPlus1(T a, T b)
{
return a + b;
}
int myPlus2(int a, int b)
{
return a + b;
}
void test01()
{
int a = 10;
int b = 20;
char c = 'c';
cout << myPlus2(a, c) << endl;
}
template<class T>
void myPrint(T a, T b)
{
cout << "模板函数调用的myPrint" << endl;
}
void myPrint(int a, int b)
{
cout << "普通函数调用的myPrint" << endl;
}
template<class T>
void myPrint(T a, T b,T c)
{
cout << "模板函数调用的myPrint(a,b,c)" << endl;
}
void test02()
{
int a = 10;
int b = 20;
myPrint(a, b);
myPrint<>(a, b);
int c = 30;
myPrint(a, b, c);
char d = 'd';
char e = 'e';
myPrint(d, e);
}
int main(void)
{
test02();
system("pause");
return EXIT_SUCCESS;
}
004.函数模板的局限性以及解决
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person(string name,int age)
{
this->m_Age = age;
this->m_Name = name;
}
string m_Name;
int m_Age;
};
template<class T>
bool myCompare(T& a, T& b)
{
if (a == b)
{
return true;
}
return false;
}
template<>bool myCompare(Person& a, Person& b)
{
if (a.m_Age == b.m_Age)
{
return true;
}
return false;
}
void test01()
{
int a = 10;
int b = 20;
int ret1 = myCompare(a, b);
cout << "ret1 = " << ret1 << endl;
Person p1 = { "Tom",10 };
Person p2 = { "Jerry",20 };
int ret2 = myCompare(p1, p2);
cout << "ret2 = " << ret2 << endl;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
005.类模板的基本使用
总结:
写法:template<class T1,class T2···>
与函数模板的区别,可以有默认类型参数
函数模板可以进行自动类型推导,而类模板不可以
成员函数 一开始不会创建出来,而是在运行的时候被创建出来
代码示例:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
template<class NameType,class AgeType>
class Person
{
public:
Person(NameType name, AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << this->m_Name << "的年龄是" << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void test01()
{
Person<string, int>p("孙悟空", 500);
p.showPerson();
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
006.类模板做函数的参数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
template<class NameType, class AgeType>
class Person
{
public:
Person(NameType name, AgeType age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << this->m_Name << "的年龄是" << this->m_Age << endl;
}
NameType m_Name;
AgeType m_Age;
};
void doWork(Person<string, int>& p)
{
p.showPerson();
}
void test01()
{
Person<string, int>p("MT", 10);
doWork(p);
}
template<class T1, class T2>
void doWork2(Person<T1,T2>& p)
{
cout << "T1 的类型:" << typeid(T1).name() << endl;
cout << "T2 的类型:" << typeid(T2).name() << endl;
p.showPerson();
}
void test02()
{
Person<string, int>p("狗贼", 10);
doWork2(p);
}
template<class T>
void doWork3(T& p)
{
p.showPerson();
}
void test03()
{
Person<string, int>p("戴比", 20);
doWork3(p);
}
int main(void)
{
test01();
test02();
test03();
system("pause");
return EXIT_SUCCESS;
}
007.类模板碰到继承问题
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
template<class T>
class Base
{
public:
T m_A;
};
class Child :public Base<int>
{
};
template<class T1,class T2>
class Child2 :public Base<T2>
{
public:
Child2()
{
cout << "T1的类型: " << typeid(T1).name() << endl;
cout << "T2的类型: " << typeid(T2).name() << endl;
}
public:
T1 m_B;
};
void test01()
{
Child2<int,double>child;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
008.类模板的类外实现成员函数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
template<class T1,class T2>
class Person
{
public:
Person(T1 name, T2 age);
void showPerson()
{
cout << "姓名:" << this->m_Name << " " << "年龄:" << this->m_Age << endl;
}
T1 m_Name;
T2 m_Age;
};
template<class T1,class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
void test01()
{
Person<string, int>p("孙悟空", 100);
p.showPerson();
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
009.类模板的分文件编写问题以及解决
- .h 和 .cpp 分别写声明和实现
- 但是由于 类模板的成员函数运行阶段才去创建,导致包含.h头文件,不会创建函数的实现,无法解析外部命令
- 建议 模板不要做分文件编写,写到一个类中即可,类内进行声明和实现,最后把后缀名改为 .hpp
010.类模板碰到友元函数-类内实现
friend void printPerson(Person<string,int>& p)
011.类模板碰到友元函数-类外实现
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
template<class T1, class T2>class Person;
template<class T1, class T2>void printPerson(Person<T1, T2>& p);
template<class T1, class T2>
class Person
{
friend void printPerson<>(Person<T1, T2>& p);
public:
Person(T1 name, T2 age)
{
this->m_Name = name;
this->m_Age = age;
}
private:
T1 m_Name;
T2 m_Age;
};
template<class T1, class T2>
void printPerson(Person<T1, T2>& p)
{
cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
}
void test01()
{
Person<string, int>p("者行孙", 1500);
printPerson(p);
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
012.类模板的应用-数组类封装
MyArray.hpp
#pragma once
#include<iostream>
using namespace std;
template<class T>
class MyArray
{
public:
explicit MyArray(int capacity)
{
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[m_Capacity];
}
MyArray(const MyArray& array)
{
this->m_Capacity = array.m_Capacity;
this->m_Size = array.m_Size;
this->pAddress = new T[array->m_Capacity];
for (int i = 0; i < this->m_Size; ++i)
{
this->pAddress[i] = array.pAddress[i];
}
}
~MyArray()
{
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
}
}
MyArray& operator=(const MyArray& array)
{
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
}
this->m_Capacity = array.m_Capacity;
this->m_Size = array.m_Size;
this->pAddress = new T[array->m_Capacity];
for (int i = 0; i < this->m_Size; ++i)
{
this->pAddress[i] = array.pAddress[i];
}
}
T& operator[](int index)
{
return this->pAddress[index];
}
void push_Back(T val)
{
this->pAddress[this->m_Size] = val;
this->m_Size++;
}
int getSize()
{
return this->m_Size;
}
int getCapacity()
{
return this->m_Capacity;
}
private:
T* pAddress;
int m_Capacity;
int m_Size;
};
类模板的应用-数组类的封装.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"MyArray.hpp"
void printIntArray(MyArray<int>& array)
{
for (int i = 0; i < array.getSize(); ++i)
{
cout << array[i] << endl;
}
}
int main(void)
{
MyArray<int> arr(10);
for (int i = 0; i < 10; ++i)
{
arr.push_Back(i * i);
}
printIntArray(arr);
system("pause");
return EXIT_SUCCESS;
}
|