[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 分钟前
Winform入门
开发语言·机器学习·c#
心静财富之门5 分钟前
退出 for 循环,break和continue 语句
开发语言·python
txinyu的博客7 分钟前
map和unordered_map的性能对比
开发语言·数据结构·c++·算法·哈希算法·散列表
搞笑症患者21 分钟前
压缩感知(Compressed Sensing, CS)
算法·最小二乘法·压缩感知·正交匹配追踪omp·迭代阈值it算法
Mr -老鬼22 分钟前
Rust适合干什么?为什么需要Rust?
开发语言·后端·rust
im_AMBER25 分钟前
Leetcode 101 对链表进行插入排序
数据结构·笔记·学习·算法·leetcode·排序算法
予枫的编程笔记25 分钟前
【Java集合】深入浅出 Java HashMap:从链表到红黑树的“进化”之路
java·开发语言·数据结构·人工智能·链表·哈希算法
ohoy31 分钟前
RedisTemplate 使用之Set
java·开发语言·redis
mjhcsp31 分钟前
C++ 后缀数组(SA):原理、实现与应用全解析
java·开发语言·c++·后缀数组sa
hui函数32 分钟前
如何解决 pip install 编译报错 ‘cl.exe’ not found(缺少 VS C++ 工具集)问题
开发语言·c++·pip