C++模板
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<class Ttype>
class class_name
{
.
.
}
Ttype 是一个占位符名称,它将在实例化该类时确定。我们可以使用逗号分隔的列表定义多个通用数据类型。 Ttype可以在类主体内部使用。
现在,我们创建一个类的实例
其中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]; // automatic array initialization.
};
在上述情况下,非类型模板参数为size,因此,模板将数组的大小作为参数提供。
在创建类的对象时指定了参数:
array<int, 15> t1; // array of 15 integers.
array<float, 10> t2; // array of 10 floats.
array<char, 4> t3; // array of 4 chars.
让我们看一个简单的非类型模板参数示例。
#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;
}
输出:
在上面的示例中,创建了包含非类型模板参数(即大小)的类模板。在创建类" A"的对象时指定。
要记住的要点
C++支持称为模板的强大功能,以实现通用编程的概念。
模板使我们能够创建一类类或一组函数来处理不同的数据类型。
模板类和函数消除了不同数据类型的代码重复,从而使开发更加轻松快捷。
在类和函数模板中都可以使用多个参数。
模板函数也可以重载。
我们还可以将非类型参数(例如内置或派生数据类型)用作模板参数。