博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++程序笔记——模板
阅读量:2339 次
发布时间:2019-05-10

本文共 5217 字,大约阅读时间需要 17 分钟。

文章目录

函数模板

概述

  • 当有多个函数,他们除了形参列表的数据类型和返回类型不同之外,在函数体中所执行的操作完全相同,可以使用函数模板来对其进行统一的代替。
  • 通过模板函数,减少重复的代码的出现,方便编程

函数模板的底层原理

  • 首先程序在编译时候,在没有调用函数模板代指的函数的时候,并不会的会模板函数产生相关的执行代码。
  • 当程序调用函数模板指代的函数的时候,会将特定的数据类型换入到函数模板中进行替换,将函数模板转成对应的模板函数,并生成相关的执行代码。
  • 不调用,无执行代码;调用,生成特定数据类型的执行代码。

实例

  • 编写程序,功能是查找一维数组a中的第一个值为x的元素的下标。如果不存在该元素就返回-1

  • functionTemplate.h文件

#ifndef _FUNCTIONTEMPLATE_H#define _FUNCTIONTEMPLATE_H#include 
using namespace std;template
T maxElement(T a,T b){
return a>b?a:b;}/* description:search the element x in the array a. return the index where it firstly appear.*/template
int findElement(T a[],T x,int n){
for(int i = 0;i < n;i ++) {
if(a[i] == x) return i; } return -1;}/* description:show different type of array*/template
void show(T a[],int n){
for(int i =0;i < n;i ++) {
cout<
<<" "; } cout<
  • main测试文件
#include 
#include "functionTemplate.h"using namespace std;int main(){
int a[6]; double b[6]; char c[6]; for(int i = 0;i < 6; i++) {
a[i] = i; b[i] = i; c[i] = 'a' + i; } show(a,6); cout<

在这里插入图片描述

分析总结

  • 函数模板是方便编程,减少了重复代码的书写,对于底层的程序运作,程序的执行代码仍旧是那么长
  • 函数模板经过程序调用,传入了特定类型的参数,实例化成对应的模板函数

类模板

概述

  • 对于仅仅只有特定的数据类型不同的类,可以使用模板类去替代所有的类。
  • 比如,C++中的vector就有各种数据类型的vector,整型的,浮点型的等等,但是其功能和函数都是一致的,所以没有必要根据不同数据类型,写大量近似的数据类型。使用模板类来代替
定义

类定义

在这里插入图片描述
类体外的成员函数的定义

在这里插入图片描述

使用
在这里插入图片描述

C++程序运行实例

  • 编写一个模板类Array
    • 成员:为模板类型的一维数组
    • 成员函数:
      • 获取自身的长度getLength()
      • 获取特定索引的元素getElement()
      • 修改所有的元素为一个统一的值setElement()

Array.h文件

#ifndef _ARRAY_H#define _ARRAY_Husing namespace std;template 
class Array{
private: T buffer[length];public: T getElement(int i) {
return buffer[i]; } int getLength() {
return sizeof(buffer)/sizeof(T); } void setElement(T x);};template
void Array
::setElement(T x){
for(int i = 0 ;i < getLength();i ++) {
buffer[i] = x; }}#endif // _ARRAY_H

main函数测试文件

#include 
#include "Array.h"using namespace std;int main(){
Array
a; Array
b; Array
c; cout<<"the length of object a is:"<
<

在这里插入图片描述

注意
  • 需要将类的模板声明和类模板成员函数的定义放在同一个头文件
关于模板类的静态成员
  • 在模板类中定义了静态成员,也不会为其分配内存空间,需要在类体外进行的声明和定义
  • 需要将模板类实例化之后,才能够创建对应的类的静态对象,所有的实例化成员共享要给类的静态成员

模板实例

函数模板实例

概述

设计两个的函数模板,其功能分别是返回两个值中的较大者和返回多个值中的较大者。要求不仅能够处理整型、实型等数值性数据,而且能够省却处理字符串

C++程序实例
  • 描述:设计两个函数模板,其功能分别是返回两个值中的较大者和
    返回多个值中的较大者。要求不仅能够处理整型、实型等数值型数据,而且能够正确处理字符串。
  • functemplate.h文件
#ifndef _FUNCTIONTEMPLATE_H#define _FUNCTIONTEMPLATE_H#include 
using namespace std;template
T maxElement(T a,T b){
return a>b?a:b;}template
T maxElement(T a[],int size){
cout<<"template
T maxElement is being used!"<
< size; i++) {
max = maxElement(max,a[i]); } return max;}#endif
  • functiontemplate.cpp文件
#include "functionTemplate.h"#include 
using namespace std;char *maxElement(char *a,char *b){
cout<<"char *maxElement(char *a,char *b) is being used"<
  • main函数测试文件
#include 
#include
#include "functionTemplate.h"using namespace std;int main(){
int nArr[] = {
3,5,7,16,9,2}; double dArr[] = {
3.5,7.2,4.3,6.8}; char cArr[] = {
'a','b','c','d'}; char *sArr[] = {
"Guolong","chening","wuhaolong"}; cout<
<
  • 运行结果

在这里插入图片描述

分析与总结
  • 在上述的问题中不能,模板函数并不能实现字符串的比较,所以只能通过对函数的重定义进行来实现,在cpp文件中实现函数
  • 使用模板函数确实能够的极大的减少代码的复用率,提高编码效率

类模板程序实例

  • 描述:数组Array的实现

    • 成员:数组的指针 和 数组的实际长度
    • 成员函数:带参数的构造函数(参数为对应的长度)、析构函数、下标符号的重载(返回特定索引的元素引用)、输出当前的元素和增加数组的长度
  • Arraytemplate.h文件

#ifndef _ARRAYTEMPLATE_#include 
using namespace std;template
class Array{
private: T *m_pArray; int m_nSize;public: Array(int nSize); ~Array(); T& operator[](int index); int Size(){
return m_nSize;} bool addNewElement(int nAddSize); void display();};//construct the array without any initializationtemplate
Array
::Array(int nSize){
m_nSize = nSize; m_pArray = new T[nSize + 1];}//destructor of the classtemplate
Array
::~Array(){ delete []m_pArray;}//the overload of the operator[]template
T& Array
::operator[](int index){ if(index >= m_nSize || index < 0) { cout<<"error:out of edge!"<
void Array
::display(){ for(int i = 0;i < m_nSize;i ++) { cout<
<<" "; } cout<
bool Array
::addNewElement(int nAddSize){ if(nAddSize <= 0) { cout<<"the new array you add must over zero!"<
  • main测试文件
#include 
#include "Arraytemplate.h"using namespace std;int main(){
Array
intArray(10); Array
doubleArray(10); Array
charArray(10); for(int i = 0;i < 10; i++) {
intArray[i] = 1 + i; doubleArray[i] = 2.5 + i; charArray[i] = 'a' + i; } intArray.display(); doubleArray.display(); charArray.display(); intArray[10] = 20; intArray.display(); intArray.addNewElement(1); intArray[10] = 20; intArray.display(); return 0;
  • 测试结果

在这里插入图片描述

分析与总结
  • 在模板形参列表中使用typename和class是一样的效果
    在这里插入图片描述
    在这里插入图片描述
  • 主要是形式化的定义,记住这些格式即可

转载地址:http://dwwvb.baihongyu.com/

你可能感兴趣的文章
删除链表中重复的节点——重复节点保留一个
查看>>
实战c++中的vector系列--正确释放vector的内存(clear(), swap(), shrink_to_fit()).md
查看>>
链表排序.md
查看>>
进程与线程的区别与联系、进程与线程的通信方式
查看>>
C++与C的区别
查看>>
产生死锁的必要条件及处理方法
查看>>
TCP和UDP的区别
查看>>
TCP状态中 time_wait 的作用
查看>>
事务具有四个特性
查看>>
树的先序、中序、后序和层次遍历-C++实现
查看>>
static和const关键字的作用
查看>>
Hadoop Hdfs 配置
查看>>
tsung集群测试
查看>>
oracle定时删除表空间的数据并释放表空间
查看>>
servlet文件上传下载
查看>>
解决文件提示: /bin/ksh^M: bad interpreter: bad interpreter:No such file or directory
查看>>
ajaxanywhere jsp 使用
查看>>
jquery的使用
查看>>
如何静态化JSP页面
查看>>
XML 与 Java 技术: 用 Castor 进行数据绑定
查看>>