[C++入门]简化的艺术---对模版的初步探索

目录

一,函数模版

运行结果​编辑

运行结果

二,类模板

1、基本语法

2、int数组展开

3、string数组展开


在古代,印刷术没有发明出来之前,知识的传播极其不方便,在C语言中也是,对于一个函数,不同的参数类型,需要反复地CV修改,那么在C++中,就出现了极其方便的工具,来简化操作,提高效率。

一,函数模版

首先我们就拿swap函数来举例,当然在后续引入数据库之后,就可以直接使用了,这里我们拿来举例子

cpp 复制代码
void Swap(int& left, int& right)
{
	int temp = left;
	left = right;
	right = temp;
}
void Swap(double& left, double& right)
{
	double temp = left;
	left = right;
	right = temp;
}
void Swap(string& left, string& right)
{
	string temp = left;
	left = right;
	right = temp;
}
int main()
{
	int  a = 1, b = 2;
	double x = 1.1,y = 2.2;
	string m = "abc", n = "qwe";
	swap(a, b);
	swap(x, y);
	swap(m, n);
	cout << a << " " << b << endl << x << " " << y<<endl<<m<<"  "<<n;


}

运行结果

写起来很不方便,换个类型就得再写一遍参数列表,那么怎么解决这个问题呢

答案就是用函数模版,让编译器去替咱们干活

cpp 复制代码
template <class T>
void mySwap(T& a, T& b) {
	T temp = a;
	a = b;
	b = temp;
}

int main() {
	int x = 10, y = 20;
	mySwap(x, y);  
	cout << "x=" << x << ", y=" << y << endl;  

	double m = 1.5, n = 2.5;
	mySwap<double>(m, n);  
	cout << "m=" << m << ", n=" << n;  
	return 0;
}

运行结果

二,类模板

1、基本语法

cpp 复制代码
template <typename T>  // 模板声明
class 类名 {
public:
    // 成员声明(使用T作为类型)
};

这里我们拿一个动态数组进行举例

在这个数组中,我在类内定义了打印,在类外重载了下标访问,实例化了两个数组进行演示

cpp 复制代码
template <typename T, int size>  
class Array {
private:
    T arr[size];
public:
    T& operator[](int index) { 
    }

    void print() {
        for (int i = 0; i < size; ++i) {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
};
template <typename T, int size>
T& Array<T, size>::operator[](int index) {

    if (index < 0 || index >= size) {
        throw out_of_range("Index out of range");
    }
    return arr[index];
}
int main() {
    Array<int, 5> intArr; 
    for (int i = 0; i < 5; ++i) {
        intArr[i] = i * 10;
    }
    intArr.print();

    Array<string, 3> strArr; 
    strArr[1] = "Template";
    strArr[2] = "Magic";
    strArr.print();

    return 0;
}

2、运行结果

在这里对main中的两个实例化对象进行展开

2、int数组展开

cpp 复制代码
class Array_int_5 {
private:
    int arr[5];  // 固定5个int的内存
public:
    int& operator[](int index) { 
    if (index < 0 || index >= size) {
        throw out_of_range("Index out of range");
    }
    return arr[index]; }
    void print() {    for (int i = 0; i < size; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
} }
};

这就是编译器对int数组的展开,其中intArri=i*10展开是

cpp 复制代码
intArr.operator[](i) = i * 10

其中 throw out_of_range是C++中特有的处理问题的一种方式,抛异常,在数组下标范围错误时,就会弹出一下警告

3、string数组展开

cpp 复制代码
class Array<string, 3> {
    string arr[3];
public:
    string& operator[](int index) {
        if (index < 0 || index >= 3)
         throw out_of_range("Index out of range");
        return arr[index];
    }
    void print() {    for (int i = 0; i < size; ++i) {
        cout << arr[i] << " ";
}
};

具体内容和上边类似,string的相关讲解将会在下一篇文章更新

以上就是本篇博客的全部内容啦,欢迎大家在评论区讨论交流!!!

相关推荐
一个王同学9 分钟前
从零到一 | CV转多模态大模型 | week17 | LLM 推理优化 & vLLM 详解
人工智能·深度学习·算法·机器学习·计算机视觉·vllm
jimy117 分钟前
C语言模拟对象、方法:“函数指针+结构体“复用函数指针指向的函数体
c语言·开发语言
天外天-亮1 小时前
HBuilder X 使用 uview-plus 方式
开发语言·javascript·hbuilder x·uview-plus
木木子221 小时前
[特殊字符] 音乐播放器——状态驱动的多媒体控制
android·开发语言·华为·php·harmonyos
旖-旎1 小时前
《LeetCode 53 最大子数组和 || LeetCode 918 环形子数组的最大和》
c++·算法·leetcode·动态规划
变量未定义~2 小时前
单调栈+倍增思想 皇家守卫【算法赛】、单调队列 附近最小
算法
QN1幻化引擎2 小时前
给 AI 做一次「意识体检」——基于 QN1 幻化引擎的灵鉴意识识别框架与 DalinX V5 实测
大数据·数据结构·人工智能·算法·架构
六个九十度2 小时前
用python脚本访问被网关隔离的嵌入式设备
开发语言·python
拂拉氏2 小时前
【知识讲解】 AVL树从基本成员的介绍到核心接口的实现(插入、判断、删除等等)
数据结构·算法·avl树
酷酷的身影2 小时前
Drivers/LedManager.cs
开发语言·php