C++进阶---模板

为什么C++有模板

我们先举一个简单的例子:当一个项目的需求是实现一个折半查找功能,是需要支持int、char等多个类型变量的时候,我们就会用到模板。

1.在函数里面使用模板

我们看看下面这个折半查找的代码:

#include <stdio.h>
//排序
void Sort(int* arr, int nLength)
{
	int i;
	int k;
	for (i = 0; i < nLength - 1; i++)
	{
		for (k = 0; k < nLength - 1 - i; k++)
		{
			if (arr[k] > arr[k + 1])
			{
				int temp = arr[k];
				arr[k] = arr[k + 1];
				arr[k + 1] = temp;
			}
		}
	}
}

//顺序输出
void Print(int* arr, int nLength)
{
	int i;
	for (i = 0; i < nLength; i++)
	{
		printf("%d\n", arr[i]);
	}
}

//折半查找
int Find(int* arr, int nLength, int nElement)
{
	int nBegin = 0, nEnd = nLength - 1, nIndex;
	while (nBegin <= nEnd)
	{
		nIndex = (nBegin + nEnd) / 2;//(nBegin+nEnd)>>1		
		if (nElement > arr[nIndex])
		{
			nBegin = nIndex + 1;
		}
		else if (nElement < arr[nIndex])
		{
			nEnd = nIndex - 1;
		}
		else
		{
			return nIndex;
		}
	}
	return -1;
}
int main(int argc, char* argv[])
{
	int arr[] = { 2,6,1,8,4,3 };
	Sort(arr, 6);
	Print(arr, 6);
	printf("---------------\n");
	int x = Find(arr, 6, 1);
	printf("%x\n", x);
	return 0;
}

查找的变量类型均为int,若是要对多个类型一起进行查找,则需要用到模板

#include <stdio.h>

//排序
template<class T>
void Sort(T arr, int nLength)
{
	int i;
	int k;
	for (i = 0; i < nLength - 1; i++)
	{
		for (k = 0; k < nLength - 1 - i; k++)
		{
			if (arr[k] > arr[k + 1])
			{
				int temp = arr[k];
				arr[k] = arr[k + 1];
				arr[k + 1] = temp;
			}
		}
	}
}

//顺序输出
template<class T>
void Print(T arr, int nLength)
{
	int i;
	for (i = 0; i < nLength; i++)
	{
		printf("%d\n", arr[i]);
	}
}

//折半查找
template<class T, class E>
int Find(T arr, int nLength, E nElement)
{
	int nBegin = 0, nEnd = nLength - 1, nIndex;
	while (nBegin <= nEnd)
	{
		nIndex = (nBegin + nEnd) / 2;//(nBegin+nEnd)>>1		
		if (nElement > arr[nIndex])
		{
			nBegin = nIndex + 1;
		}
		else if (nElement < arr[nIndex])
		{
			nEnd = nIndex - 1;
		}
		else
		{
			return nIndex;
		}
	}
	return -1;
}
int main(int argc, char* argv[])
{
	int arr[] = { 2,6,1,8,4,3 };
	Sort(arr, 6);
	Print(arr, 6);
	printf("---------------\n");
	int index = Find(arr, 6, 1);
	printf("%x\n", index);
	return 0;
}

可以看到,我们如果需要对多种类型的数组进行查找,可以用模板定义一个类型T,这个类型可以对任意类型进行替换。当然,如果我们定义了新的类型就一定要使用,否则会报错。另外,使用模板前后,我们程序的汇编代码是一模一样,没有改变

Tips: 大致如此,main函数的arr数组存的类型暂时只有int,还没添加多个类型。。。。。。

2.在结构体/类中使用模板

#include <stdio.h>

template<class T, class M>
struct Base
{
	T x;
	T y;

	M a;
	M b;

	T Max()
	{
		if (x > y)
		{
			return x;
		}
		else
		{
			return y;
		}
	}
	M Min()
	{
		if (a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
};
int main(int argc, char* argv[])
{
	Base<int, char> base;
	base.x = 10;
	base.y = 20;
	base.a = 1;
	base.b = 2;

	int xy_max = base.Max();
	int ab_min = base.Min();

	printf("%d %d\n", xy_max, ab_min);
	return 0;
}

可以看到,我们对int类型x和y以及a和b分别做比较运算,就可以将x和y的函数以及变量类型定义为T,将a和b的函数以及变量类型定义为M
总结:继承是数据的复制,模板是代码的复制

本文为参考滴水三期的学习笔记,若有错误请指正,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值