实验6 模板类、文件IO和异常处理

发布时间 2023-12-17 18:43:50作者: 夏添添

四、实验结论

1.实验任务4

Vector.hpp

#pragma once
#include <iostream>
#include <stdexcept>

using namespace std;

template <typename T>
class Vector {
private:
    T* data;      
    int size;    

public:
    Vector(int sz = 0, const T& value = T());
    Vector(const Vector& other);
    ~Vector();

    int get_size() const;
    
    T& at(int index);
    
    T& operator[](int index);

    template <typename T1>
    friend void output(const Vector<T1>& v);
};

template <typename T>
Vector<T>::Vector(int sz, const T& value) : size(sz) {
    data = new T[size];
    for (int i = 0; i < size; ++i) {
        data[i] = value;
    }
}

template <typename T>
Vector<T>::Vector(const Vector& other) : size(other.size) {
    data = new T[size];
    for (int i = 0; i < size; ++i) {
        data[i] = other.data[i];
    }
}

template <typename T>
Vector<T>::~Vector() {
    delete[] data;
}

template <typename T>
int Vector<T>::get_size() const {
    return size;
}

template <typename T>
T& Vector<T>::at(int index) {
    if (index >= size) {
        throw std::out_of_range("Index out of range");
    }
    return data[index];
}

template <typename T>
T& Vector<T>::operator[](int index) {
    if (index >= size) {
        throw out_of_range("Index out of range");
    }
    return data[index];
}

template <typename T1>
void output(const Vector<T1>& v) {
    for (int i = 0; i < v.size; ++i) {
        cout << v.data[i] << ",";
    }

    cout << "\b \b" << endl;
}

 

task4.cpp

#include <iostream>
#include "Vector.hpp"

void test() {
    using namespace std;

    int n;
    cin >> n;

    Vector<double> x1(n);
    for (auto i = 0; i < n; ++i)
        x1.at(i) = i * 0.7;

    output(x1);

    Vector<int> x2(n, 42);
    Vector<int> x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;
    output(x2);

    x3[0] = 999;
    output(x3);
}

int main() {
    test();
}

 

运行测试截图

 

 

2.实验任务5

task5.cpp

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
void output(ostream &out) {
    for (int i = 0; i <=26; ++i) {
        if (i == 0) { out<<"   ";
            for (char ch = 'a'; ch <= 'z'; ++ch) {
                out << ch << ' ';
            }
            out << endl;
            continue;
        }
        
        out << setw(2) << i << ' ';
        
        for (int j = 0; j < 26; ++j) {
            out << char('A' + (j + i ) % 26) << ' ';
        }
        out << endl; 
    }
}

int main() {
    output(cout);

    ofstream out("cipher_key.txt");
    if (out) {
        output(out);
    } else if(!out.is_open()){
        cout << "Fail to open the file." << endl;
    }

    out.close();

    return 0;
}

 

运行测试截图

 

 

五、实验总结

使用模板类的优点:

  1. 代码重用和泛化: 模板类允许创建通用的数据结构和算法,使得代码更具有通用性,可以适用于不同的数据类型而无需重复编写代码。

  2. 类型安全: 使用模板可以提高类型安全性,因为编译器在编译时能够检查和捕获类型不匹配的错误。这有助于减少在运行时发生的类型错误。

  3. 高性能: 模板类在编译时生成特定类型的代码,因此可以产生比手动实现更高效的代码。模板的内联特性还有助于减少函数调用的开销。

  4. 灵活性: 模板允许在编译时进行参数化,使得程序员可以以一种灵活的方式处理不同类型的数据结构,而不必针对每种类型编写特定的代码。

  5. 标准库支持: C++标准库中广泛使用了模板,其中包含了许多通用的数据结构和算法,为开发人员提供了丰富的工具集。

使用文件IO的优点:

  1. 数据持久性: 文件IO允许程序将数据保存到文件中,从而实现数据的持久性。这对于需要长期存储信息的应用程序来说是至关重要的。

  2. 数据交换: 文件是不同程序之间进行数据交换的通用方式。通过使用文件IO,可以轻松地将数据从一个应用程序传递到另一个应用程序。

  3. 配置和设置: 文件经常用于存储配置信息和设置,这样用户可以在不修改源代码的情况下修改应用程序的行为。

  4. 日志记录: 文件IO对于记录应用程序的运行时信息、错误日志和调试信息非常有用,有助于后续的故障排除和性能优化。

  5. 数据备份: 文件IO使得数据可以轻松备份到外部存储设备,从而提供对数据的额外保护。

使用异常处理的优点:

  1. 错误处理: 异常处理提供了一种结构化的方式来处理运行时错误。通过抛出和捕获异常,让程序员可以更容易地定位、记录和处理错误。

  2. 代码清晰度: 异常处理可以将正常代码与错误处理代码分离开来,使得主要逻辑更加清晰和易读。不同层次的错误处理可以在不同的异常处理块中完成。

  3. 程序稳定性: 异常处理有助于提高程序的稳定性,因为它可以防止未处理的异常导致程序崩溃。程序员可以选择在适当的位置捕获异常并采取适当的措施。

  4. 资源管理: 异常处理对于确保资源(如文件、内存等)被正确释放具有重要意义。在发生异常的情况下,可以使用异常处理机制来确保资源得到释放。

  5. 可维护性: 使用异常处理可以使代码更具可维护性,因为错误处理逻辑被集中在一起,而不是分散在代码的各个部分。这样有助于快速识别和修复错误。