C++模板是添加到C++的强大函数。它允许您定义通用类和通用函数,从而为通用编程提供支持。泛型编程是一种将泛型类型用作算法中的参数的技术,以便它们可以用于多种数据类型。
可以用两种方式表示模板:
函数模板:
我们可以为函数定义模板。例如,如果我们具有add()函数,则可以创建add函数的版本以添加int,float或double类型的值。
类模板:
我们可以为一个类定义一个模板。例如,可以为数组类创建一个类模板,该模板可以接受各种类型的数组,例如int数组,float数组或double数组。
template < class Ttype> ret_type func_name(parameter_list) { //body of function. }
Ttype - 它是函数使用的数据类型的占位符名称。在函数定义中使用。只是一个占位符,编译器会自动将其替换为实际的数据类型。
class - class关键字用于在模板声明中指定通用类型。
让我们看一个简单的函数模板示例:
#include <iostream> using namespace std; template<class T> T add(T &a,T &b) { T result = a+b; return result; } int main() { int i =2; int j =3; float m = 2.3; float n = 1.2; cout<<"Addition of i and j is :"<<add(i,j); cout<<'\n'; cout<<"Addition of m and n is :"<<add(m,n); return 0; }
输出:
Addition of i and j is :5 Addition of m and n is :3.5
在上面的示例中,我们创建了可以对任何类型执行加法运算的函数模板,可以是整数,浮点数或双精度数。
通过使用逗号分隔列表,我们可以在模板函数中使用多个通用类型。
template<class T1, class T2,.....> return_type function_name (arguments of type T1, T2....) { //body of function. }
在以上语法中,我们已经看到模板函数可以接受任意数量的不同类型的参数。
让我们看一个简单的示例:
#include <iostream> using namespace std; template<class X,class Y> void fun(X a,Y b) { std::cout << "Value of a is : " <<a<< std::endl; std::cout << "Value of b is : " <<b<< std::endl; } int main() { fun(15,12.3); return 0; }
输出:
Value of a is : 15 Value of b is : 12.3
在上面的示例中,我们在模板函数中使用了两种通用类型,即X和Y。
我们可以重载通用函数,这意味着重载的模板函数可以在参数列表中有所不同。
让我们通过一个简单的示例来了解这一点:
#include <iostream> using namespace std; template<class X> void fun(X a) { std::cout << "Value of a is : " <<a<< std::endl; } template<class X,class Y> void fun(X b ,Y c) { std::cout << "Value of b is : " <<b<< std::endl; std::cout << "Value of c is : " <<c<< std::endl; } int main() { fun(10); fun(20,30.5); return 0; }
输出:
Value of a is : 10 Value of b is : 20 Value of c is : 30.5
在上面的示例中,fun()函数的模板已重载。
通用函数对函数的所有版本执行相同的操作,但数据类型不同。让我们看一个重载函数的简单示例,因为这两个函数具有不同的函数,所以不能用通用函数代替。
让我们通过一个简单的示例来了解这一点:
#include <iostream> using namespace std; void fun(double a) { cout<<"value of a is : "<<a<<'\n'; } void fun(int b) { if(b%2==0) { cout<<"Number is even"; } else { cout<<"Number is odd"; } } int main() { fun(4.6); fun(6); return 0; }
输出:
value of a is : 4.6 Number is even
在上面的示例中,我们重载了普通函数。我们不能重载通用函数,因为这两个函数具有不同的函数。第一个显示数值,第二个显示数值是否为偶数。
类模板(Class Template)也可以类似于函数模板来定义。当一个类使用Template的概念时,该类称为通用类。
template<class Ttype> class class_name { . . }
Ttype 是一个占位符名称,将在实例化该类时确定。我们可以使用逗号分隔的列表定义多个通用数据类型。 Ttype可以在类主体内部使用。
现在,我们创建一个类的实例
class_name<type> ob;
class_name - 这是类的名称。
type - 它是类正在处理的数据的类型。
ob - 这是对象的名称。
让我们看一个简单的示例:
#include <iostream> using namespace std; template<class T> class A { public: T num1 = 5; T num2 = 6; void add() { std::cout << "Addition of num1 and num2 : " << num1+num2<<std::endl; } }; int main() { A<int> d; d.add(); return 0; }
输出:
Addition of num1 and num2 : 11
在上面的示例中,我们为类A创建了一个模板。在main()方法内部,我们创建了名为'd'的类A的实例。
我们可以在类模板中使用多个通用数据类型,并且每种通用数据类型都用逗号分隔。
template<class T1, class T2, ......> class class_name { //Body of the class. }
让我们看一个简单的示例,当类模板包含两种通用数据类型时。
#include <iostream> using namespace std; template<class T1, class T2> class A { T1 a; T2 b; public: A(T1 x,T2 y) { a = x; b = y; } void display() { std::cout << "Values of a and b are : " << a<<" ,"<<b<<std::endl; } }; int main() { A<int,float> d(5,6.5); d.display(); return 0; }
输出:
Values of a and b are : 5,6.5
模板可以包含多个参数,我们还可以使用非类型参数,除了T型参数之外,我们还可以使用其他类型的参数,例如字符串,函数名称,常量表达式和内置类型。 让我们看下面的示例:
template<class T, int size> class array { T arr[size]; //自动数组初始化。 };
在上述情况下,非类型模板参数为size,因此,template提供数组的大小作为参数。
在创建类的对象时指定参数:
array<int, 15> t1; //15 个整数的数组。 array<float, 10> t2; //10 个浮点数的数组。 array<char, 4> t3; //4个字符的数组。
让我们看一个简单的非类型模板参数的例子。
#include <iostream> using namespace std; template<class T, int size> class A { public: T arr[size]; void insert() { int i =1; for (int j=0;j<size;j++) { arr[j] = i; i++; } } void display() { for(int i=0;i<size;i++) { std::cout << arr[i] << " "; } } }; int main() { A<int,10> t1; t1.insert(); t1.display(); return 0; }
输出:
1 2 3 4 5 6 7 8 9 10
在上面的示例中,创建了包含非类型模板参数(即大小)的类模板。在创建类" A"的对象时指定。
要记住的要点
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)