【小白学习C++ 教程】十五、C++ 中的template模板和泛型

举报
毛利 发表于 2021/07/30 22:59:26 2021/07/30
【摘要】 @Author:Runsen template模板在 C++ 中一个简单但非常强大的工具。简单的想法是将数据类型作为参数传递,这样我们就不需要为不同的数据类型编写相同的代码。 C++ 添加了两个新关键字来支持模板:template和typename。第二个关键字typename可以替换为关键字“class”。 模板可以以两种不同的方式使用。 函数模板类模板 ...

@Author:Runsen

template模板在 C++ 中一个简单但非常强大的工具。简单的想法是将数据类型作为参数传递,这样我们就不需要为不同的数据类型编写相同的代码。

C++ 添加了两个新关键字来支持模板:templatetypename。第二个关键字typename可以替换为关键字“class”。

模板可以以两种不同的方式使用。

  1. 函数模板
  2. 类模板

泛型

在学习template模板之前,了解一下泛型。在Java中学习过泛型,其实都是差不多的概念。泛型是允许类型(整数、字符串等)作为方法、类和接口的参数的想法。

例如,像数组等这样的对象可以非常有效地使用泛型。

实现了泛型编程的方法来提高代码的效率。通用编程使程序员能够编写适用于所有数据类型的通用算法。如果数据类型是整数、字符串或字符,则无需创建不同的算法。

泛型编程的优点是

  • 代码可重用性
  • 避免函数重载
  • 一旦编写泛型,它可以用于多个cpp

泛型一般和模板结合使用。模板是一个简单但非常强大的 C++ 工具。简单的想法是将数据类型作为参数传递,这样我们就不需要为不同的数据类型编写相同的代码。

#include <iostream>
using namespace std;
 
// 函数模板
template <typename T> T myMax(T x, T y)
{ return (x > y) ? x : y;
} int main()
{ //  int cout << myMax<int>(3, 7) << endl; // double cout << myMax<double>(3.0, 7.0) << endl; //  char cout << myMax<char>('g', 'e') << endl; return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

输出:

7
7
G

  
 
  • 1
  • 2
  • 3

函数模板

函数模板用于创建一组将相同算法应用于不同数据类型的函数。

#include<iostream>
using namespace std;
template <class A> A addition(A x, A y)
{ return ( x + y );
}
int main()
{ cout<<"Addition1 : "<<addition(4,4)<<endl; cout<<"Addition2 : "<<addition(4.2,3.6)<<endl; cout<<"Addition3 : "<<addition(4.0,4.7)<<endl; return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出:

Addition1 : 8
Addition2 : 7.8
Addition3 : 8.7

  
 
  • 1
  • 2
  • 3

如果执行cout<<"Addition3 : "<<addition(4,4.7)<<endl;,将会报错。

4 & 4.7,它将是不同的整数和浮点数;而泛型函数声明两个参数的数据类型相同。

类模板

与函数模板一样,可以为通用类操作创建类模板。

有时,需要一个对都相同的类实现,只是使用的数据类型不同。

类模板可以轻松地为所有数据类型重用相同的代码。

以下是类模板的语法:

template <class T>
class className
{ ... .. ...
public: T var; T someOperation(T arg); ... .. ...
};

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在上面的声明中,T是模板参数,它是所用数据类型的占位符。

在类体内部,一个成员变量 var和成员函数someOperation()都是类型T。

创建类模板对象,需要在< >内部定义数据类型。

类名<数据类型> 类对象;

  
 
  • 1

下面使用类模板对两个数字进行加减乘除的程序

#include <iostream>
using namespace std;

template <class T>
class Calculator
{
private:
	T num1, num2;
	
public:
	Calculator(T n1, T n2)
	{
		num1 = n1;
		num2 = n2;
	} void displayResult()
	{
		cout << "Numbers are: " << num1 << " and " << num2 << "." << endl;
		cout << "Addition is: " << add() << endl;
		cout << "Subtraction is: " << subtract() << endl;
		cout << "Product is: " << multiply() << endl;
		cout << "Division is: " << divide() << endl;
	} T add() { return num1 + num2; } T subtract() { return num1 - num2; } T multiply() { return num1 * num2; } T divide() { return num1 / num2; }
};

int main()
{
	Calculator<int> intCalc(2, 1);
	Calculator<float> floatCalc(2.4, 1.2); cout << "Int results:" << endl;
	intCalc.displayResult(); cout << endl << "Float results:" << endl;
	floatCalc.displayResult(); return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

输出如下:

Int results:
Numbers are: 2 and 1.
Addition is: 3
Subtraction is: 1
Product is: 2
Division is: 2

Float results:
Numbers are: 2.4 and 1.2.
Addition is: 3.6
Subtraction is: 1.2
Product is: 2.88
Division is: 2

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Code:使用函数模板显示求数组中的最小数。

#include <iostream>
using namespace std;

template <class T, int max>
int arrMin(T arr[], int n)
{ int m = max; for (int i = 0; i < n; i++) if (arr[i] < m) m = arr[i]; return m;
} int main()
{ int arr1[]  = {10, 20, 15, 12}; int n1 = sizeof(arr1)/sizeof(arr1[0]); char arr2[] = {1, 2, 3}; int n2 = sizeof(arr2)/sizeof(arr2[0]); cout << arrMin<int, 10000>(arr1, n1) << endl; cout << arrMin<char, 256>(arr2, n2); return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

文章来源: maoli.blog.csdn.net,作者:刘润森!,版权归原作者所有,如需转载,请联系作者。

原文链接:maoli.blog.csdn.net/article/details/119223074

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。