首页 > 其他分享 >实验六、模板类,文件I/O流,异常处理

实验六、模板类,文件I/O流,异常处理

时间:2023-12-17 22:34:10浏览次数:25  
标签:文件 const int Vector 实验 template data 模板 size

实验四:

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;
}
View Code

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();
}
View Code

测试结果:

 

 实验五:

task5:

#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;
}
View Code

结果:

 

标签:文件,const,int,Vector,实验,template,data,模板,size
From: https://www.cnblogs.com/202283230013nuister/p/17897852.html

相关文章

  • 4.1-华三-irf中的bfd mad实验配置
    1.BFDMad概述用途:核心层的irf,最好做MAD检测,来确保网络的稳定性。BFD:BidirectionalForwardingDetection(双向转发检测)。1.是一种网络协议,用于快速检测和报告两个网络节点之间的连接状态。主要目标是提供低延迟、高可靠性的链路故障检测,以便网络设备可以快速做出响应并进行故障恢复......
  • 实验6
    实验41#include<stdio.h>2#include<string.h>3#defineN104typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;//销售册数10}......
  • 实验六 模板类,文件io和异常处理
    实验任务4#pragmaonce#include<iostream>#include<stdexcept>usingstd::cout;usingstd::endl;template<typenameT>classVector{public://构造函数,默认大小为0Vector(intn=0):size(n),data(newT[n]){if(n<0)......
  • 实验6
    1.实验任务41#include<iostream>2#include<stdexcept>34template<typenameT>5classVector{6private:7T*data;8size_tsize;9public:10Vector(size_tn):size(n){11data=newT[size];12......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp#pragmaonce#include<iostream>#include<stdexcept>#include<string>usingnamespacestd;template<typenameT>classVector{public:Vector(intx=0,constT&value=T());Vector(constVecto......
  • Vscode解决中文乱码和多文件操作
    一、解决多文件操作2.然后创建一个main文件、一个func.h文件、一个func.c文件分别写上代码这是main.c这是func.c文件这是func.h文件二、配置launch.json和tasks.json文件 将下面这个json文件复制拷贝到launch.json去:launch.json{//使用IntelliSense了解相关......
  • 调整archlinux分区及ext4文件系统大小
    参照https://wiki.archlinuxcn.org/wiki/Parted1.防止数据丢失有重要数据的话先备份,防止系统崩了数据没了可以的话先在虚拟机练习一下2.注意点要扩展分区及其文件系统,(1)先扩展分区(2)再扩展文件系统要收缩分区及其文件系统,(1)先收缩文件系统(2)再收缩分区这样做是因......
  • 实验6 模板类、文件I/O和异常处理
    Task4: vector.hpp:#include<iostream>#include<string>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector():data(nullptr),size(0){}Vector......
  • 实验7
    实验任务4:\#include<stdio.h>intmain(){inti=0;chars;FILE*fp;fp=fopen("data4.txt","r");while(1){s=fgetc(fp);if(s==EOF){break;}elseif(s=='�......
  • 实验六
    实验代码实验任务4.hpp#include<iostream>#include<stdexcept>#include<cassert>usingnamespacestd;template<typenameT>classVector{private:T*ptr;intsize;public:Vector(intsize){if(size<0)throwstd:......