[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的相关讲解将会在下一篇文章更新

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

相关推荐
QuantumStack1 小时前
【C++ 真题】P1104 生日
开发语言·c++·算法
天若有情6731 小时前
01_软件卓越之道:功能性与需求满足
c++·软件工程·软件
whoarethenext1 小时前
使用 C++/OpenCV 和 MFCC 构建双重认证智能门禁系统
开发语言·c++·opencv·mfcc
写个博客1 小时前
暑假算法日记第一天
算法
绿皮的猪猪侠1 小时前
算法笔记上机训练实战指南刷题
笔记·算法·pta·上机·浙大
代码的奴隶(艾伦·耶格尔)2 小时前
后端快捷代码
java·开发语言
hie988942 小时前
MATLAB锂离子电池伪二维(P2D)模型实现
人工智能·算法·matlab
Jay_5152 小时前
C++多态与虚函数详解:从入门到精通
开发语言·c++
路来了2 小时前
Python小工具之PDF合并
开发语言·windows·python
杰克尼2 小时前
BM5 合并k个已排序的链表
数据结构·算法·链表