当前位置: 首页 > news >正文

【C++学习】函数模板

模板的概念

模板就是建立通用的模具,大大提高复用性。

模板的特点:

        模板不可以直接使用,它只是一个模型

        模板的通用不是万能的

基本语法

C++中提供两种模板机制:函数模板和类模板

函数模板作用:

        建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟类型来代表 

template<typename T>

template       声明创建模板

typename     表明其后符号是一种数据类型

       T            通用的数据类型

普通代码 

#include<iostream>
#include<string>using namespace std;void swapint(int &a, int &b)
{int temp = a;a = b;b = temp;
}
//交换两个浮点型函数
void swapdouble(double& a, double& b)
{double temp = a;a = b;b = temp;
}
void test01()
{int a = 10;int b = 20;swapint(a, b);cout << a << " " << b << endl;double c = 1.1;double d = 2.2;swapdouble(c, d);cout << c << " " << d << endl;
}
int main()
{test01();system("pause");return 0;
}

 使用模板

#include<iostream>
#include<string>
using namespace std;template<typename T>
//声明一个模板,告诉编译器后面代码中紧跟着的T不要报错,T是一个通用数据类型
void myswap(T& a, T& b)
{T temp = a;a = b;b = t;
}
void test01()
{int a = 10;int b = 20;myswap(a, b);//1.自动类型推导cout << a << " " << b << endl;double c = 1.1;double d = 2.2;myswap<double>(c, d);//2.显示指定类型cout << c << " " << d << endl;
}
int main()
{test01();system("pause");return 0;
}

注意事项

注意:

        自动类型推导,必须推导出一致的数据类型T才可以使用

        模板必须要确定出T的数据类型,才可以使用

 

 

 函数模板案例--数组排序

#include<iostream>
#include<string>
using namespace std;template<class T>
void myswap(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){//交换myswap(arr[max], arr[i]);}}
}
//打印数组
template<class T>
void printarr(T arr[], int len)
{for (int i = 0;i < len;i++){cout << arr[i] << " ";}cout << endl;
}
void test01()
{char chararr[] = "badcfe";int num = sizeof(chararr) / sizeof(char);mysort(chararr, num);printarr(chararr, num);
}
int main()
{test01();system("pause");return 0;
}

普通函数与函数模板区别

普通函数调用时可以发生隐式类型转换

                        用自动类型推导,不可以发生隐式类型转换

函数模板

                        用显示指定类型,可以发生隐式类型转换

 

代码:

#include<iostream>
#include<string>
using namespace std;
int myadd(int a,int b)
{return a + b;
}
template<class T>
int myadd1(T a, T b)
{return a + b;
}
void test01()
{int a = 10;int b = 20;char c = 'c';cout << myadd(a, c) << endl;//ASCII c=99//不报错是因为,编译器隐式的把c类型转换成了intcout << myadd1<int>(a, c) << endl;
}
int main()
{test01();system("pause");return 0;
}

普通函数和函数模板的调用规则

普通函数和函数模板是可以发生函数重载的(函数名一致)

1.如果函数模板和普通函数都可以实现,优先调用普通函数

2.可以通过空模板参数列表来强制调用函数模板

3.函数模板也可以发生重载

4.如果函数模板可以产生更好的匹配,优先匹配函数模板

 1.优先调用普通函数

#include<iostream>
#include<string>
using namespace std;
void myprint(int a, int b)
{cout <<"普通"<< a << " " << b;
}
template<class T>
void myprint(T a, T b)
{cout << "模板" << a << " " << b;
}void test01()
{int a = 10;int b = 20;myprint(a,b);
}
int main()
{test01();system("pause");return 0;
}

 2.通过空模板参数列表来强制调用函数模板

#include<iostream>
#include<string>
using namespace std;
//通过空模板参数列表来强制调用函数模板
void myprint(int a, int b);
/*{cout <<"普通"<< a << " " << b;
}*/
template<class T>
void myprint(T a, T b)
{cout << "模板" << a << " " << b;
}
void test01()
{int a = 10;int b = 20;myprint<>(a,b);
}
int main()
{test01();system("pause");return 0;
}

 3.函数模板也可以发生重载

#include<iostream>
#include<string>
using namespace std;void myprint(int a, int b);
/*{cout <<"普通"<< a << " " << b;
}*/
template<class T>
void myprint(T a, T b)
{cout << "模板" << a << " " << b;
}
template<class T>
void myprint(T a, T b,T c)
{cout << "重载模板" << a << " " << b<<" "<<c;
}
void test01()
{int a = 10;int b = 20;int c = 100;myprint(a,b,c);
}
int main()
{test01();system("pause");return 0;
}

4.如果函数模板可以产生更好的匹配,优先匹配函数模板

#include<iostream>
#include<string>
using namespace std;void myprint(int a, int b)
{cout <<"普通"<< a << " " << b;
}
template<class T>
void myprint(T a, T b)
{cout << "模板" << a << " " << b;
}
template<class T>
void myprint(T a, T b,T c)
{cout << "重载模板" << a << " " << b<<" "<<c;
}
void test01()
{int a = 10;int b = 20;int c = 100;char c1 = 'a';char c2 = 'b';myprint(c1, c2);//普通函数可以隐式类型转换//但编译器认为如果走模板可以直接用,无需隐式类型转换
}
int main()
{test01();system("pause");return 0;
}

如果写了函数模板最好不要再写普通函数了 

 模板的局限性

#include<iostream>
#include<string>
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) cout << "相等" << endl;else cout << "不等" << endl;
}
void test01()
{person a ("ll",10);person b ("xx",90);if (compare(a, b)){cout << "==" << endl;}else cout << "!=" << endl;
}
int main()
{test01();system("pause");return 0;
}

 解决:

#include<iostream>
#include<string>
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) cout << "相等" << endl;else cout << "不等" << endl;
}
//利用具体化person的版本实现
template<> bool compare(person& a, person& b)
{if (a.m_name == b.m_name && a.m_age == b.m_age){return true;}else return false;
}void test01()
{person a ("ll",10);person b ("xx",90);if (compare(a, b)){cout << "==" << endl;}else cout << "!=" << endl;
}
int main()
{test01();system("pause");return 0;
}

 利用具体化的模板,可以解决自定义类型的通用化

学习模板并不是为了写模板,而是在STL能运用系统提供的模板。

http://www.lryc.cn/news/66653.html

相关文章:

  • 1960-2014年各国二氧化碳排放量(人均公吨数)
  • 【java-04】深入浅出多态、内部类、常用API
  • 【逐函数详细讲解ORB_SLAM2算法和C++代码|Viewer|1-26】
  • 【C语言】测试2 C程序设计初步
  • SpringBoot3 integrate SpringDoc
  • 一文解决Xshell无法连接vmware上的centos
  • ATTCK v13版本战术介绍——防御规避(五)
  • 祁宁:社区问答是激荡企业高级智慧的头脑风暴 | 开发者说
  • linux安装
  • 【Go编程语言】 Go语言基础语法
  • 洗稿用什么软件-洗稿软件免费
  • 网络请求发送
  • 微信小程序开发
  • number类型超出16位的问题(前端、后端处理)
  • 【高并发】网络模式
  • springboot+dubbo+zookeeper 项目实战
  • PHP学习笔记第一天
  • 案例研究|萤石网络通过JumpServer解决安全运维难题
  • 即时聊天app开发-即时通讯app开发方案分析
  • js为什么会阻塞渲染, 什么是异步?
  • Nuvoton NK-980IOT开发板 u-boot 编译
  • OpenCL使用CL_MEM_USE_HOST_PTR存储器对象属性与存储器映射
  • 浅谈osgEarth操控器类的createLocalCoordFrame函数如何将局部坐标系的点转为世界坐标系下的Martix(ENU坐标)
  • PHP程序员和Python程序员的职业前景怎么样?我来聊聊自己的体会
  • 【MATLAB图像处理实用案例详解(8)】—— 图像数字水印算法
  • 最全的免费SSL证书申请方式
  • Ceph入门到精通-CrushMap算法概述
  • 如何利用API做好电商,接口如何凋用关键字
  • Give me a logic game idea about economics
  • 测试之路,2023年软件测试市场领域有哪些变化?突破走得更远...