C++ 模板多种类型
本文介绍了 C++ 中多类型模板的使用。
C++ 模板多种类型
C++ 中的模板可以定义为创建通用函数和类的公式蓝图。 模板是 C++ 中简单但功能强大的工具,其思想是将数据类型传递给参数,这样我们就不会为不同的数据类型再次编写相同的代码。
模板在编译时像宏一样展开。 唯一的区别是编译器会在扩展模板之前检查数据类型。
模板可以定义为源代码仅包含类或函数,但编译器代码将包含这些类或函数的多个副本。
现在,多类型模板可以定义为类的成员模板,该类也是模板,并且在其类模板定义之外定义; 此类定义应使用类模板的模板参数指定,后跟成员模板参数。
C++ 中有两种类型的模板。
函数模板
函数模板可以同时处理多种数据类型,而标准函数则不能。 函数模板的语法如下。
template<class DataType>Returntype FunctionName(parameter list) {
// function body
}
让我们尝试一个简单的函数模板示例。
#include <iostream>
using namespace std;
// This template function will work for all data types/ it will return the minimum value from given parameters
template <typename Delftstack> Delftstack MinValue(Delftstack a, Delftstack b) {
return (a < b) ? a : b;
}
int main() {
cout << MinValue<int>(78, 354) << endl; // Call the template function for int
cout << MinValue<double>(336.23, 4673.23)
<< endl; // Call the template function for double
cout << MinValue<char>('s', 'y')
<< endl; // Call the template function for char
return 0;
}
上面包含一个模板函数 MinValue,它将适用于所有数据类型并返回给定值的最小值。 查看输出:
78
336.23
s
类模板
类模板还知道通用模板的工作方式与函数模板类似。 类模板定义了 C++ 中的一系列类; 类模板的语法如下。
template<class Ttype>
class ClassName
{
//class body;
}
现在让我们尝试使用类模板的示例。
#include <iostream>
using namespace std;
template <typename Delftstack> class DemoArray {
private:
Delftstack* ptr;
int arraysize;
public:
DemoArray(Delftstack arr[], int number);
void print();
};
template <typename Delftstack> DemoArray<Delftstack>::DemoArray(Delftstack arr[], int number) {
ptr = new Delftstack[number];
arraysize = number;
for (int x = 0; x < arraysize; x++)
ptr[x] = arr[x];
}
template <typename Delftstack> void DemoArray<Delftstack>::print() {
for (int x = 0; x < arraysize; x++)
cout << " " << *(ptr + x);
cout << endl;
}
int main() {
int arr[7] = { 23, 12, 43, 74, 55, 36, 67 };
DemoArray<int> demo(arr, 7);
demo.print();
return 0;
}
上面的代码创建了一个模板数组类和一个模板方法来打印数组的成员; 我们在模板类之外定义了成员模板。 查看输出:
23 12 43 74 55 36 67
具有多种类型的类模板
具有多种类型的类模板意味着我们必须定义具有多个参数的类模板,这在 C++ 中是可能的。 定义具有多种类型的类模板的语法如下。
template <class Delft1, class Delft2, class Delft3 = char> class DemoClassTemplate {
//class body
}
让我们跳到示例来了解具有多种类型的类模板。
#include <iostream>
using namespace std;
// Class template containing multiple type parameters
template <class Delft1, class Delft2, class Delft3 = char> class DemoClassTemplate {
private:
Delft1 value1;
Delft2 value2;
Delft3 value3;
public:
DemoClassTemplate(Delft1 v1, Delft2 v2, Delft3 v3) : value1(v1), value2(v2), value3(v3) {} // constructor
void printVar() {
cout << "The Value 1 = " << value1 << endl;
cout << "The Value 2 = " << value2 << endl;
cout << "The Value 3 = " << value3 << endl;
}
};
int main() {
// object with int, double and char types using the template class
DemoClassTemplate<int, double> DemoObject1(57, 34.7, 'Delftstack');
cout << "DemoObject 1 Values: " << endl;
DemoObject1.printVar();
// object with int, double and boolean types using the template class
DemoClassTemplate<double, char, bool> DemoObject2(789.8, 'Delft', true);
cout << "\nDemoObject 2 values: " << endl;
DemoObject2.printVar();
return 0;
}
正如我们所看到的,上面的代码创建了一个模板类,其中包含多个类型参数,其中包含默认参数。 现在,我们可以创建该模板类的对象并使用我们想要的任何数据类型。
输出:
DemoObject 1 Values:
The Value 1 = 57
The Value 2 = 34.7
The Value 3 = k
DemoObject 2 values:
The Value 1 = 789.8
The Value 2 = t
The Value 3 = 1
相关文章
Arduino 复位
发布时间:2024/03/13 浏览次数:315 分类:C++
-
可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。
Arduino 的字符转换为整型
发布时间:2024/03/13 浏览次数:181 分类:C++
-
可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。
Arduino 串口打印多个变量
发布时间:2024/03/13 浏览次数:381 分类:C++
-
可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。