当前位置: 首页 > news >正文

C++ 泛型和特化

泛型 (Generics)

泛型编程通过模板实现,它允许我们编写可以处理多种数据类型的代码,而不需要为每种类型重写代码

例子

函数模板示例

#include <iostream>
#include <string>
using namespace std;// 泛型函数模板 - 可以比较任何可比较的类型
template <typename T>
T getMax(T a, T b) {return (a > b) ? a : b;
}int main() {// 使用泛型函数比较整数int intMax = getMax(10, 25);cout << "Max integer: " << intMax << endl;// 使用泛型函数比较浮点数double doubleMax = getMax(3.14, 2.71);cout << "Max double: " << doubleMax << endl;// 使用泛型函数比较字符串string strMax = getMax(string("apple"), string("banana"));cout << "Max string: " << strMax << endl;return 0;
}

类模板

#include <iostream>
using namespace std;// 泛型类模板 - 可以存储任何类型的值
template <typename T>
class Container {
private:T value;
public:Container(T val) : value(val) {}T getValue() {return value;}void setValue(T val) {value = val;}void display() {cout << "Value: " << value << endl;}
};int main() {// 使用泛型类存储整数Container<int> intContainer(42);intContainer.display();// 使用泛型类存储字符串Container<string> strContainer("Hello, Templates!");strContainer.display();// 使用泛型类存储浮点数Container<double> doubleContainer(3.14159);doubleContainer.display();return 0;
}

特化(Specialization)

特化允许我们为模板的特定类型提供定制实现,当通用模板不能满足某些类型的特殊需求时非常有用

例子

函数模板

#include <iostream>
#include <cstring>
using namespace std;// 通用模板函数
template <typename T>
void printTypeInfo(T value) {cout << "Generic type: " << value << " (size: " << sizeof(T) << " bytes)" << endl;
}// 特化版本 - 针对const char*类型
template <>
void printTypeInfo<const char*>(const char* value) {cout << "C-string: " << value << " (length: " << strlen(value) << " characters)" << endl;
}// 特化版本 - 针对double类型
template <>
void printTypeInfo<double>(double value) {cout << "Double precision: " << value << " (size: " << sizeof(double) << " bytes)" << endl;
}int main() {int i = 42;double d = 3.14159;const char* str = "Hello, Specialization!";printTypeInfo(i);    // 使用通用模板printTypeInfo(d);    // 使用double特化printTypeInfo(str);  // 使用const char*特化return 0;
}

类模板

#include <iostream>
#include <vector>
using namespace std;// 通用类模板
template <typename T>
class DataProcessor {
public:void process(T data) {cout << "Processing generic data: " << data << endl;}
};// 特化版本 - 针对vector<int>类型
template <>
class DataProcessor<vector<int>> {
public:void process(vector<int> data) {cout << "Processing vector of integers (size: " << data.size() << "):" << endl;for (int val : data) {cout << val << " ";}cout << endl;}
};// 特化版本 - 针对const char*类型
template <>
class DataProcessor<const char*> {
public:void process(const char* data) {cout << "Processing C-string: " << data << endl;cout << "String length: " << strlen(data) << " characters" << endl;}
};int main() {DataProcessor<int> intProcessor;intProcessor.process(42);DataProcessor<const char*> strProcessor;strProcessor.process("Hello, World!");DataProcessor<vector<int>> vecProcessor;vecProcessor.process({1, 2, 3, 4, 5});return 0;
}

总结

泛型: 通过模板实现,允许编写与类型无关的通用代码,提高代码复用性

特化: 为特定类型提供定制实现,处理通用模板无法满足的特殊情况

http://www.dtcms.com/a/435175.html

相关文章:

  • 【Linux篇】Linux指令进阶:从入门到熟练的实操指南
  • Orleans流系统时序图
  • 专业网站建设价格分析企业建设网站好吗
  • 活动日志系统集成指南
  • 弹幕网站是怎么做的软件开发工程师级别
  • 贵阳市建设局信息管理网站中国建设会计学网站
  • 函数简单传入参数的汇编分析
  • 怎样做已有网站的编辑维护大沥九江网站制作
  • 自己网站首页如何设置网站托管维护方案
  • 安卓基础组件024-底部导航栏
  • 【ROS2学习笔记】话题通信篇:话题通信项目实践——系统状态监测与可视化工具
  • 苏州门户网站平台推广员
  • ICT 数字测试原理 4 --电源监控电路(PMC)
  • 网站内页要不要加上关键词和描述广告型网站怎么做的
  • 深圳市建设局工程交易中心网站贵州 网站建设
  • 【C/C++】 函数形参—指针传递
  • 门限签名与多方安全计算(MPC)
  • 东莞公司高端网站建设高大上网站
  • 基于websocket的多用户网页五子棋(一)
  • PCA 主成分分析:数据世界的 “旅行清单整理师”—— 从 30 维杂乱到 2 维清晰的诗意降维
  • wordpress兼容mipseo加盟代理
  • 台州网站排名优化费用网站建设设计风格描述
  • 利用 ZoneABC 免费域名 零成本接入 Cloudflare 企业版 CDN
  • 企业网站建设费用属于什么科目dw软件网站建设教程
  • Streamlit:基础入门——零基础搭建第一个 Web 应用
  • Netty粘包和半包问题产生的原因和解决方案
  • 【小沐学GIS】基于C++绘制地形DEM(OpenGL、Terrain、TIFF、hgt)第十二期
  • 怎么搭建本地网站外贸营销工具
  • MySQL常用命令全攻略
  • 郑州市网站和公众号建设长沙公积金网站怎么做异动