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

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

相关推荐
wjs202410 分钟前
DOM CDATA
开发语言
Tingjct11 分钟前
【初阶数据结构-二叉树】
c语言·开发语言·数据结构·算法
C雨后彩虹12 分钟前
计算疫情扩散时间
java·数据结构·算法·华为·面试
猷咪37 分钟前
C++基础
开发语言·c++
IT·小灰灰39 分钟前
30行PHP,利用硅基流动API,网页客服瞬间上线
开发语言·人工智能·aigc·php
快点好好学习吧40 分钟前
phpize 依赖 php-config 获取 PHP 信息的庖丁解牛
android·开发语言·php
秦老师Q41 分钟前
php入门教程(超详细,一篇就够了!!!)
开发语言·mysql·php·db
烟锁池塘柳041 分钟前
解决Google Scholar “We‘re sorry... but your computer or network may be sending automated queries.”的问题
开发语言
是誰萆微了承諾41 分钟前
php 对接deepseek
android·开发语言·php
CSDN_RTKLIB44 分钟前
WideCharToMultiByte与T2A
c++