C++函数模板和类模板结合使用,能极大提升代码的灵活性和复用性。简单来说,就是用模板类来存储数据,然后用模板函数来操作这些数据,类型可以根据需要自动推导。
解决方案
下面通过一个例子来说明如何结合使用 C++ 函数模板和类模板。假设我们需要一个通用的数组类,可以存储各种类型的数据,并且需要一个函数来查找数组中的最大值。
#include <iostream> #include <vector> #include <algorithm> // 类模板:通用数组类 template <typename T> class GenericArray { private: std::vector<T> data; public: GenericArray(int size) : data(size) {} T& operator[](int index) { return data[index]; } int getSize() const { return data.size(); } }; // 函数模板:查找数组最大值 template <typename T> T findMax(const GenericArray<T>& arr) { if (arr.getSize() == 0) { throw std::runtime_error("Array is empty"); } T maxVal = arr[0]; for (int i = 1; i < arr.getSize(); ++i) { if (arr[i] > maxVal) { maxVal = arr[i]; } } return maxVal; } int main() { // 创建一个存储 int 类型的数组 GenericArray<int> intArray(5); intArray[0] = 10; intArray[1] = 5; intArray[2] = 20; intArray[3] = 15; intArray[4] = 8; // 使用函数模板查找 int 数组的最大值 int maxInt = findMax(intArray); std::cout << "Max int value: " << maxInt << std::endl; // 创建一个存储 double 类型的数组 GenericArray<double> doubleArray(3); doubleArray[0] = 3.14; doubleArray[1] = 2.71; doubleArray[2] = 1.618; // 使用函数模板查找 double 数组的最大值 double maxDouble = findMax(doubleArray); std::cout << "Max double value: " << maxDouble << std::endl; return 0; }
类模板
GenericArray:
- 定义了一个可以存储任意类型
T
的数组。 - 使用
std::vector
作为底层存储。 - 提供了
operator[]
用于访问数组元素。 - 提供了
getSize()
用于获取数组大小。
函数模板
findMax:
- 接收一个
GenericArray<T>
类型的数组作为参数。 - 遍历数组,找到最大值。
- 返回最大值。
main函数:
- 演示了如何创建
int
和double
类型的GenericArray
实例。 - 演示了如何使用
findMax
函数模板查找不同类型数组的最大值。
在类模板的成员函数中使用函数模板,其实就是把函数模板的定义放在类模板的内部。这样做可以进一步提升代码的模块化程度,让类模板的功能更加强大。
#include <iostream> #include <vector> #include <algorithm> template <typename T> class GenericArray { private: std::vector<T> data; public: GenericArray(int size) : data(size) {} T& operator[](int index) { return data[index]; } int getSize() const { return data.size(); } // 在类模板内部定义函数模板 template <typename U> U findMax() const { if (data.empty()) { throw std::runtime_error("Array is empty"); } U maxVal = static_cast<U>(data[0]); // 显式转换,确保类型匹配 for (size_t i = 1; i < data.size(); ++i) { if (static_cast<U>(data[i]) > maxVal) { // 显式转换 maxVal = static_cast<U>(data[i]); } } return maxVal; } }; int main() { GenericArray<int> intArray(5); intArray[0] = 10; intArray[1] = 5; intArray[2] = 20; intArray[3] = 15; intArray[4] = 8; // 显式指定模板参数 double maxInt = intArray.findMax<double>(); std::cout << "Max int value: " << maxInt << std::endl; // 输出 double 类型 GenericArray<double> doubleArray(3); doubleArray[0] = 3.14; doubleArray[1] = 2.71; doubleArray[2] = 1.618; // 显式指定模板参数 int maxDouble = doubleArray.findMax<int>(); std::cout << "Max double value: " << maxDouble << std::endl; // 输出 int 类型 return 0; }
在这个例子中,
findMax函数模板被定义为
GenericArray类模板的成员函数。 关键在于,你可以在调用
findMax的时候,显式指定返回值的类型。 模板函数与模板类结合使用时,如何处理类型转换问题?
类型转换是使用模板时经常遇到的问题。如果模板函数和模板类处理的数据类型不一致,就需要进行类型转换。
例如,在上面的例子中,
findMax函数模板返回值的类型
U可以和数组元素的类型
T不同。 为了保证比较的正确性,需要使用
static_cast将数组元素转换为
U类型。
template <typename T> class GenericArray { // ... template <typename U> U findMax() const { // ... U maxVal = static_cast<U>(data[0]); // ... if (static_cast<U>(data[i]) > maxVal) { // ... } // ... } };
static_cast是一种编译时类型转换,它比 C 风格的类型转换更加安全。 但是,使用
static_cast也需要小心,因为它可能会导致数据丢失或精度损失。 例如,将
double类型转换为
int类型会丢失小数部分。 如何在模板函数中访问模板类的私有成员?
在模板函数中直接访问模板类的私有成员是不允许的,因为私有成员只能在类的内部访问。 但是,可以通过以下几种方式来间接访问私有成员:
-
友元函数: 可以将模板函数声明为模板类的友元函数。这样,模板函数就可以访问模板类的所有成员,包括私有成员。
template <typename T> class GenericArray { private: std::vector<T> data; // 声明模板函数为友元函数 template <typename U> friend U findMax(const GenericArray<T>& arr); public: GenericArray(int size) : data(size) {} T& operator[](int index) { return data[index]; } int getSize() const { return data.size(); } }; template <typename T> T findMax(const GenericArray<T>& arr) { // 现在可以访问 arr.data if (arr.data.empty()) { throw std::runtime_error("Array is empty"); } T maxVal = arr.data[0]; for (size_t i = 1; i < arr.data.size(); ++i) { if (arr.data[i] > maxVal) { maxVal = arr.data[i]; } } return maxVal; }
-
提供公共接口: 可以在模板类中提供公共的成员函数,用于访问或修改私有成员。 这样,模板函数就可以通过调用这些公共接口来间接访问私有成员。 这是更推荐的做法,因为它更符合面向对象的设计原则。
template <typename T> class GenericArray { private: std::vector<T> data; public: GenericArray(int size) : data(size) {} T& operator[](int index) { return data[index]; } int getSize() const { return data.size(); } // 提供公共接口访问私有成员 const std::vector<T>& getData() const { return data; } }; template <typename T> T findMax(const GenericArray<T>& arr) { // 通过公共接口访问 data const std::vector<T>& data = arr.getData(); if (data.empty()) { throw std::runtime_error("Array is empty"); } T maxVal = data[0]; for (size_t i = 1; i < data.size(); ++i) { if (data[i] > maxVal) { maxVal = data[i]; } } return maxVal; }
函数对象(Functor)是一个行为类似函数的对象。 它可以是一个类的实例,该类重载了
operator()运算符。 使用函数对象可以实现更加灵活和可定制的操作。
#include <iostream> #include <vector> #include <algorithm> // 函数对象:自定义比较函数 template <typename T> class GreaterThan { private: T threshold; public: GreaterThan(T threshold) : threshold(threshold) {} bool operator()(const T& value) const { return value > threshold; } }; template <typename T> class GenericArray { private: std::vector<T> data; public: GenericArray(int size) : data(size) {} T& operator[](int index) { return data[index]; } int getSize() const { return data.size(); } // 使用函数对象进行过滤 std::vector<T> filter(const GreaterThan<T>& predicate) const { std::vector<T> result; for (const T& value : data) { if (predicate(value)) { result.push_back(value); } } return result; } }; int main() { GenericArray<int> intArray(5); intArray[0] = 10; intArray[1] = 5; intArray[2] = 20; intArray[3] = 15; intArray[4] = 8; // 创建一个函数对象 GreaterThan<int> gt10(10); // 使用函数对象过滤数组 std::vector<int> filteredArray = intArray.filter(gt10); std::cout << "Filtered array: "; for (int value : filteredArray) { std::cout << value << " "; } std::cout << std::endl; return 0; }
在这个例子中,
GreaterThan是一个函数对象,它用于比较一个值是否大于给定的阈值。
GenericArray类的
filter函数接收一个
GreaterThan对象作为参数,并使用该对象来过滤数组中的元素。 这样,就可以根据不同的比较规则来过滤数组。
总的来说,C++函数模板与类模板结合使用,可以编写出高度灵活和可复用的代码。理解类型转换、友元函数、函数对象等概念,可以更好地利用模板的优势。
以上就是C++函数模板与类模板结合使用实例的详细内容,更多请关注知识资源分享宝库其它相关文章!
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。