Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
C++函数模板与类模板结合使用实例_创想鸟

C++函数模板与类模板结合使用实例

通过类模板定义通用数据结构,结合函数模板实现灵活操作,支持类型自动推导与转换,利用友元函数或公共接口访问私有成员,并可通过函数对象实现自定义逻辑,提升代码复用性与扩展性。

c++函数模板与类模板结合使用实例

C++函数模板和类模板结合使用,能极大提升代码的灵活性和复用性。简单来说,就是用模板类来存储数据,然后用模板函数来操作这些数据,类型可以根据需要自动推导。

解决方案

下面通过一个例子来说明如何结合使用 C++ 函数模板和类模板。假设我们需要一个通用的数组类,可以存储各种类型的数据,并且需要一个函数来查找数组中的最大值。

#include #include #include // 类模板:通用数组类template class GenericArray {private:    std::vector data;public:    GenericArray(int size) : data(size) {}    T& operator[](int index) {        return data[index];    }    int getSize() const {        return data.size();    }};// 函数模板:查找数组最大值template T findMax(const GenericArray& arr) {    if (arr.getSize() == 0) {        throw std::runtime_error("Array is empty");    }    T maxVal = arr[0];    for (int i = 1; i  maxVal) {            maxVal = arr[i];        }    }    return maxVal;}int main() {    // 创建一个存储 int 类型的数组    GenericArray 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 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

:

立即学习“C++免费学习笔记(深入)”;

定义了一个可以存储任意类型

T

的数组。使用

std::vector

作为底层存储。提供了

operator[]

用于访问数组元素。提供了

getSize()

用于获取数组大小。

函数模板

findMax

:

接收一个

GenericArray

类型的数组作为参数。遍历数组,找到最大值。返回最大值。

main

函数:

演示了如何创建

int

double

类型的

GenericArray

实例。演示了如何使用

findMax

函数模板查找不同类型数组的最大值。

如何在类模板的成员函数中使用函数模板?

在类模板的成员函数中使用函数模板,其实就是把函数模板的定义放在类模板的内部。这样做可以进一步提升代码的模块化程度,让类模板的功能更加强大。

#include #include #include template class GenericArray {private:    std::vector data;public:    GenericArray(int size) : data(size) {}    T& operator[](int index) {        return data[index];    }    int getSize() const {        return data.size();    }    // 在类模板内部定义函数模板    template     U findMax() const {        if (data.empty()) {            throw std::runtime_error("Array is empty");        }        U maxVal = static_cast(data[0]); // 显式转换,确保类型匹配        for (size_t i = 1; i < data.size(); ++i) {            if (static_cast(data[i]) > maxVal) { // 显式转换                maxVal = static_cast(data[i]);            }        }        return maxVal;    }};int main() {    GenericArray intArray(5);    intArray[0] = 10;    intArray[1] = 5;    intArray[2] = 20;    intArray[3] = 15;    intArray[4] = 8;    // 显式指定模板参数    double maxInt = intArray.findMax();    std::cout << "Max int value: " << maxInt << std::endl; // 输出 double 类型    GenericArray doubleArray(3);    doubleArray[0] = 3.14;    doubleArray[1] = 2.71;    doubleArray[2] = 1.618;    // 显式指定模板参数    int maxDouble = doubleArray.findMax();    std::cout << "Max double value: " << maxDouble << std::endl; // 输出 int 类型    return 0;}

在这个例子中,

findMax

函数模板被定义为

GenericArray

类模板的成员函数。 关键在于,你可以在调用

findMax

的时候,显式指定返回值的类型。

模板函数与模板类结合使用时,如何处理类型转换问题?

类型转换是使用模板时经常遇到的问题。如果模板函数和模板类处理的数据类型不一致,就需要进行类型转换。

例如,在上面的例子中,

findMax

函数模板返回值的类型

U

可以和数组元素的类型

T

不同。 为了保证比较的正确性,需要使用

static_cast

将数组元素转换为

U

类型。

template class GenericArray {    // ...    template     U findMax() const {        // ...        U maxVal = static_cast(data[0]);        // ...        if (static_cast(data[i]) > maxVal) {            // ...        }        // ...    }};
static_cast

是一种编译时类型转换,它比 C 风格的类型转换更加安全。 但是,使用

static_cast

也需要小心,因为它可能会导致数据丢失或精度损失。 例如,将

double

类型转换为

int

类型会丢失小数部分。

如何在模板函数中访问模板类的私有成员?

在模板函数中直接访问模板类的私有成员是不允许的,因为私有成员只能在类的内部访问。 但是,可以通过以下几种方式来间接访问私有成员:

友元函数: 可以将模板函数声明为模板类的友元函数。这样,模板函数就可以访问模板类的所有成员,包括私有成员。

template class GenericArray {private:    std::vector data;    // 声明模板函数为友元函数    template     friend U findMax(const GenericArray& arr);public:    GenericArray(int size) : data(size) {}    T& operator[](int index) {        return data[index];    }    int getSize() const {        return data.size();    }};template T findMax(const GenericArray& 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  maxVal) {            maxVal = arr.data[i];        }    }    return maxVal;}

提供公共接口: 可以在模板类中提供公共的成员函数,用于访问或修改私有成员。 这样,模板函数就可以通过调用这些公共接口来间接访问私有成员。 这是更推荐的做法,因为它更符合面向对象的设计原则。

template class GenericArray {private:    std::vector data;public:    GenericArray(int size) : data(size) {}    T& operator[](int index) {        return data[index];    }    int getSize() const {        return data.size();    }    // 提供公共接口访问私有成员    const std::vector& getData() const {        return data;    }};template T findMax(const GenericArray& arr) {    // 通过公共接口访问 data    const std::vector& data = arr.getData();    if (data.empty()) {        throw std::runtime_error("Array is empty");    }    T maxVal = data[0];    for (size_t i = 1; i  maxVal) {            maxVal = data[i];        }    }    return maxVal;}

如何在模板类中使用函数对象(Functor)进行自定义操作?

函数对象(Functor)是一个行为类似函数的对象。 它可以是一个类的实例,该类重载了

operator()

运算符。 使用函数对象可以实现更加灵活和可定制的操作。

#include #include #include // 函数对象:自定义比较函数template class GreaterThan {private:    T threshold;public:    GreaterThan(T threshold) : threshold(threshold) {}    bool operator()(const T& value) const {        return value > threshold;    }};template class GenericArray {private:    std::vector data;public:    GenericArray(int size) : data(size) {}    T& operator[](int index) {        return data[index];    }    int getSize() const {        return data.size();    }    // 使用函数对象进行过滤    std::vector filter(const GreaterThan& predicate) const {        std::vector result;        for (const T& value : data) {            if (predicate(value)) {                result.push_back(value);            }        }        return result;    }};int main() {    GenericArray intArray(5);    intArray[0] = 10;    intArray[1] = 5;    intArray[2] = 20;    intArray[3] = 15;    intArray[4] = 8;    // 创建一个函数对象    GreaterThan gt10(10);    // 使用函数对象过滤数组    std::vector 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++函数模板与类模板结合使用实例的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1474175.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
C++如何使用std::variant实现多类型安全存储
上一篇 2025年12月18日 21:14:13
C++在Linux系统下环境搭建常见坑及解决方案
下一篇 2025年12月18日 21:14:27

相关推荐

发表回复

登录后才能评论
关注微信