首页 > 其他分享 >实验6 模板类、文件IO和异常处理

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

时间:2023-12-17 23:14:36浏览次数:30  
标签:index const Vector 实验 IO include data 模板 size

任务4

 1 #include <iostream>
 2 #include "Vector.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     int n;
 8     cin >> n;
 9 
10     Vector<double> x1(n);
11     for (auto i = 0; i < n; ++i)
12         x1.at(i) = i * 0.7;
13 
14     output(x1);
15 
16     Vector<int> x2(n, 42);
17     Vector<int> x3(x2);
18 
19     output(x2);
20     output(x3);
21 
22     x2.at(0) = 77;
23     output(x2);
24 
25     x3[0] = 999;
26     output(x3);
27 }
28 
29 int main() {
30     test();
31 }
task4.cpp
 1 #ifndef VECTOR_HPP
 2 #define VECTOR_HPP
 3 
 4 #include <iostream>
 5 #include <stdexcept>
 6 
 7 template <typename T>
 8 class Vector {
 9 private:
10     T* data;
11     size_t size;
12 
13 public:
14     Vector(size_t sz) : size(sz) {
15         data = new T[size];
16     }
17 
18     Vector(size_t sz, const T& value) : size(sz) {
19         data = new T[size];
20         for (size_t i = 0; i < size; ++i) {
21             data[i] = value;
22         }
23     }
24 
25     Vector(const Vector& other) : size(other.size) {
26         data = new T[size];
27         for (size_t i = 0; i < size; ++i) {
28             data[i] = other.data[i];
29         }
30     }
31 
32     ~Vector() {
33         delete[] data;
34     }
35 
36     size_t get_size() const {
37         return size;
38     }
39 
40     T& at(size_t index) {
41         if (index < size) {
42             return data[index];
43         }
44         else {
45             throw std::out_of_range("Index out of range");
46         }
47     }
48 
49     const T& at(size_t index) const {
50         if (index < size) {
51             return data[index];
52         }
53         else {
54             throw std::out_of_range("Index out of range");
55         }
56     }
57 
58     T& operator[](size_t index) {
59         return data[index];
60     }
61 
62     const T& operator[](size_t index) const {
63         return data[index];
64     }
65 
66     friend void output(const Vector& vec) {
67         for (size_t i = 0; i < vec.size; ++i) {
68             std::cout << vec.data[i] << ' ';
69         }
70         std::cout << std::endl;
71     }
72 };
73 
74 #endif
Vector.hpp

运行结果:

 

任务5

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <fstream>
 4 
 5 void output(std::ostream& out) {
 6     const int numLines = 26;
 7 
 8     for (int i = 0; i < numLines; ++i) {
 9         out << std::setw(2) << std::right << i + 1 << ' ';
10         for (int j = 0; j < 26; ++j) {
11             char ch = 'A' + (i + j) % 26;
12             out << ch << ' ';
13         }
14         out << std::endl;
15     }
16 }
17 
18 int main() {
19     std::cout << "屏幕上打印输出:\n"
20         << "   a b c d e f g h i j k l m n o p q r s t u v w x y z"
21         << std::endl;
22     output(std::cout);
23 
24     std::ofstream outFile("cipher_key.txt");
25     if (outFile.is_open()) {
26         std::cout << "写入文件 cipher_key.txt:" << std::endl;
27         output(outFile);
28         outFile.close();
29     }
30     else {
31         std::cerr << "无法打开文件 cipher_key.txt" << std::endl;
32         return 1;
33     }
34 
35     return 0;
36 }
task5.cpp

运行结果:

 

标签:index,const,Vector,实验,IO,include,data,模板,size
From: https://www.cnblogs.com/8716ydc/p/17910059.html

相关文章

  • 实验6 C语言结构体、枚举应用编程
    1、实验1运行结果2、实验2源代码 1#include<stdio.h>2#include<string.h>3#defineN104#defineM8056typedefstruct{7charname[M];//书名8charauthor[M];//作者9}Book;1011intmain(){12Bookx[N]=......
  • MetaFormer Is Actually What You Need for Vision:通用的ViT架构才是关键
    MetaFormerIsActuallyWhatYouNeedforVision*Authors:[[WeihaoYu]],[[MiLuo]],[[PanZhou]],[[ChenyangSi]],[[YichenZhou]],[[XinchaoWang]],[[JiashiFeng]],[[ShuichengYan]]初读印象comment::(PoolFormer)Transformer的通用架构是其良好性能的保障,而......
  • Educational Codeforces Round 159 (Rated for Div. 2)
    EducationalCodeforcesRound159(RatedforDiv.2)A-BinaryImbalance解题思路:有一对\((0,1)\),那么\(0\)就能无限增长。代码:#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=2e5+10;typedefpair<ll,ll>pii;constllm......
  • 实验6 模板类、文件IO和异常处理
    实验任务4#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public:Vector(intn);Vector(intn,Tvalue);Vector(constVector<T>&vi);~Vector();......
  • 实验六、模板类,文件I/O流,异常处理
    实验四:Vector.hpp://#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector(intsz=0,constT&value=T());V......
  • 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)......
  • Pandas数据分析实战(Pandas in action)第2章 Series 对象
    Pandas数据分析实战第2章SeriesSeries是Pandas的核心数据结构之一,是一个用于同构数据的一维标记数组。Series可以设置索引,没有设置的话,Pandas会设置默认的索引,从0开始的线性索引。创建一个Series对象importpandasaspdpd.Series()Series([],dtype:objec......
  • 实验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......