[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数组的展开,其中intArr[i]=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的相关讲解将会在下一篇文章更新

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

相关推荐
念风零壹4 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
智驱力人工智能4 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
猫头虎4 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
YUJIANYUE5 小时前
PHP纹路验证码
开发语言·php
仟濹5 小时前
【Java基础】多态 | 打卡day2
java·开发语言
孞㐑¥5 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
Re.不晚5 小时前
JAVA进阶之路——无奖问答挑战2
java·开发语言
八零后琐话5 小时前
干货:程序员必备性能分析工具——Arthas火焰图
开发语言·python
月挽清风5 小时前
代码随想录第十五天
数据结构·算法·leetcode
3GPP仿真实验室5 小时前
【MATLAB源码】CORDIC-QR :基于Cordic硬件级矩阵QR分解
开发语言·matlab·矩阵