本文共 8866 字,大约阅读时间需要 29 分钟。
c++中的模板
Templates in C++ are an abstract version of Generic Programming wherein the code is written in an independent manner i.e. completely independent of the other blocks of code within the same program.
C ++中的模板是通用编程的抽象版本,其中代码以独立的方式编写,即完全独立于同一程序中的其他代码块。
With templates, you can create generic methods and classes, where data types are passed along with the data as arguments to reduce code redundancy.
使用模板,您可以创建通用方法和类,其中数据类型与数据一起作为参数传递,以减少代码冗余。
We can create a single method and/or a class that works with different data types without having to redefine the structure of the function or define a new function.
我们可以创建一个用于不同数据类型的单一方法和/或类 ,而无需重新定义函数的结构或定义新的函数。
Thus, templates enable the programmer to define a method or class without having a prior idea about the data type of the variables it would be dealing with ahead.
因此,模板使程序员可以定义方法或类,而无需事先了解将要处理的变量的数据类型。
Templates in C++ resemble the working logic of macros in system programming.
用C ++模板类似系统编程的宏的工作逻辑。
Thus, in a template, a single function/class handles data with multiple data types within it.
因此, 在模板中,单个函数/类可处理其中具有多种数据类型的数据 。
Templates in C++ can be used to create and define the following:
C ++中的模板可用于创建和定义以下内容:
Classes
: Creates a template class containing any template type or non-template type variable as an argument. Classes
:创建一个包含任何模板类型或非模板类型变量作为参数的模板类。 Functions/Methods
: Creates a Template function containing any template type or non-template type variable as an argument. Functions/Methods
:创建一个包含任何模板类型或非模板类型变量作为参数的模板函数。 Function Templates work and handle different data types within a function in a single flow of the program.
功能模板在程序的单个流程中工作并处理功能内的不同数据类型。
Syntax:
句法:
templatetype fun_name(argument_list) { // body }
type
is just a placeholder/template argument and it represents the data type of the function/class. type
只是一个占位符/模板参数 ,它表示函数/类的数据类型。 class
is basically a keyword to describe the generic function/class type in the template declaration. The keyword class can also be replaced by typename
. class
基本上是描述模板声明中泛型函数/类类型的关键字 。 关键字class也可以用typename
代替。 Example:
例:
#includeusing namespace std; template T subtract(T x,T y) { T res = x-y; return(res); } int main() { cout<<"Subtraction of numbers of integer type:\n"< (3,2); cout<<"\nSubtraction of numbers of float type:\n"< (5.3,3.2); return 0; }
In the above snippet of code, we have created a template function called subtract with two variables – x, y as arguments to it.
在上面的代码片段中,我们创建了一个名为减法的模板函数,带有两个变量– x,y作为其参数。
During the compilation of the program, it assigns the particular data type to the function. i.e. while running the code at dynamic type it checks for the type of the passed arguments to the calling function and then takes the execution ahead.
在程序编译期间,它将特定的数据类型分配给函数。 即在以动态类型运行代码时,它会检查传递给调用函数的参数的类型,然后提前执行。
Output:
输出:
Subtraction of numbers of integer type:1Subtraction of numbers of float type:2.1
Function Templates in C++ can have multiple generic types of arguments in it.
C ++中的函数模板中可以包含多种通用类型的参数 。
Syntax:
句法:
templatetype fun_name (parameters of generic type T1, T2....) { // body of function. }
Here, class T1
and T2
describe the different types of generic functions.
在这里,类T1
和T2
描述了通用函数的不同类型。
Example:
例:
#includeusing namespace std; template void subtract(T1 x,T2 y) { T1 res = x-y; cout<<(res); } int main() { subtract (3.5,2); return 0; }
In the above snippet of code, we have passed a float type value as the data type for T1 and int type value for the data type for T2.
在上面的代码片段中,我们传递了一个float类型值作为T1的数据类型,并将int类型值作为T2的数据类型。
Output:
输出:
1.5
Templates in C++ indicates that the overloaded function would contain different types of arguments.
C ++ 模板的重载表明重载的函数将包含不同类型的参数。
Let’s go through the below example in order to understand Function Overloading in Templates.
让我们看一下下面的示例,以了解模板中的函数重载。
#includeusing namespace std; template void area(T1 a) { T1 res = a*a; cout<<"The area of Square:\n"; cout<<(res); } template void area(T1 l,T2 b) { T1 res = l*b; cout<<"\nThe area of Rectangle:\n"; cout<<(res); } int main() { area(2); area(3.5,1); return 0; }
In the above snippet of code, we have overloaded the ‘area‘ function wherein, we have calculated the area of a Square and a Rectangle by passing different types of arguments to the corresponding function.
在上面的代码片段中,我们重载了 “ area ” 函数,其中,我们通过将不同类型的参数传递给相应的函数来计算Square和Rectangle的面积。
Output:
输出:
The area of Square:4The area of Rectangle:3.5
Class Templates help define different types of classes by passing the data type associated with it.
类模板通过传递与之关联的数据类型来帮助定义不同类型的类。
It can be termed as a generic class
because the data type of the class and its operations is decided at the time of compilation by passing a particular type associated with the functions of the class.
之所以可以将其称为generic class
是因为在编译时通过传递与该类的功能相关联的特定类型来确定generic class
的数据类型及其操作。
Syntax:
句法:
templateclass Class_name { //body }
type
is just a placeholder/template argument and it represents the data type of the class. type
只是一个占位符/模板参数 ,它表示类的数据类型。 In order to create the instance of the class, we need to follow the following statement:
为了创建该类的实例,我们需要遵循以下语句:
Class_nameobject_name;
data_type
: It refers to the data type associated with the particular class. data_type
:它是指与特定类关联的数据类型。 Example:
例:
#includeusing namespace std; template class Compute { public: T subtract(T x,T y) { T res = x-y; return res; } }; int main() { Compute ob; cout<<"Subtraction function for Integer value as argument\n"; cout<<"Result: "< <<"\n" ; return 0; }
In the above code snippet, we use the template T to pass the data
在上面的代码片段中,我们使用模板T传递数据
Output:
输出:
Subtraction function for Integer value as argumentResult: 5
Class Templates can have multiple arguments i.e. more than one generic type in a class.
类模板可以具有多个参数,即一个类中可以有多个通用类型。
Syntax:
句法:
templateclass Class_name { // Body }
Example:
例:
#includeusing namespace std; template class Compute { A x; B y; public: Compute(A i,B j) { x = i; y = j; } void show() { cout < <<","< < ob(5.5,2); ob.show(); return 0; }
In the above snippet of code, we have passed two generic types A and B respectively.
在上面的代码片段中,我们分别传递了两个通用类型A和B。
While creating an instance of the class, we have passed float and int as the data type for generic types A and B and have used a class Constructor to initialize the data member’s values and have displayed the same.
在创建类的实例时,我们将float和int作为通用类型A和B的数据类型传递,并使用类Constructor 初始化数据成员的值并显示出它们。
Output:
输出:
5.5,2
Class templates can contain multiple parameters too. These parameters can be of the generic type or non-type arguments such as constants, method names, string, etc.
类模板也可以包含多个参数。 这些参数可以是通用类型,也可以是非类型参数,例如常量,方法名称,字符串等。
Syntax:
句法:
templateclass Class_name { //Body };
Example:
例:
#includeusing namespace std; template class Compute { public: T solve(T x,T y) { T res = x-y + arg; return res; } }; int main() { Compute ob; cout<<"Result: "< <<"\n" ; return 0; }
Here, we have passed arg of type int as an argument to the Class Template and have passed its value during the function call in the main function.
在这里,我们将int类型的arg作为参数传递给类模板,并在主函数的函数调用期间传递了它的值。
In this article, we have unveiled the working of Templates and have understood how function and class templates lead to the re-usability of the code and better efficiency too.
在本文中,我们揭露了模板的工作原理,并且了解了函数和类模板如何导致代码的可重用性以及更高的效率。
翻译自:
c++中的模板
转载地址:http://agozd.baihongyu.com/