任务4:
Vector.hpp:
1 #pragma once 2 #include <iostream> 3 using namespace std; 4 5 template <typename T> 6 class Vector { 7 private: 8 int size_; 9 T* data_; 10 11 public: 12 13 Vector(int n); 14 15 Vector(int n, T value); 16 17 Vector(const Vector<T>& v) : size_(v.size_), data_(new T[v.size_]) { 18 for (int i = 0; i < size_; ++i) { 19 data_[i] = v.data_[i]; 20 } 21 } 22 23 ~Vector() {} 24 25 int size() const { return size_; } 26 27 T& at(int index) { 28 if (index < 0 || index >= size_) { 29 throw out_of_range("Vector: Index out of range"); 30 } 31 return data_[index]; 32 } 33 34 const T& at(int index) const { 35 if (index < 0 || index >= size_) { 36 throw out_of_range("Vector: Index out of range"); 37 } 38 return data_[index]; 39 } 40 41 T& operator[](int index) { return data_[index]; } 42 const T& operator[](int index) const { return data_[index]; } 43 44 template <typename T1> 45 friend void output(const Vector<T1>& v); 46 }; 47 48 49 template <typename T> 50 void output(const Vector<T>& v) { 51 for (int i = 0; i < v.size_; ++i) { 52 cout << v.data_[i]; 53 if (i != v.size_ - 1) cout << ", "; 54 } 55 cout << endl; 56 } 57 template<typename T> 58 Vector<T>::Vector(int n) : size_(n){ 59 if (n < 0) { 60 throw length_error("Vector constructor: negative size"); 61 } 62 else 63 { 64 data_ = new T[size_]; 65 } 66 } 67 template<typename T> 68 Vector<T>::Vector(int n, T value) : size_(n) { 69 if (n < 0) { 70 throw length_error("Vector constructor: negative size"); 71 } 72 else 73 { 74 data_ = new T[size_]; 75 for (int i = 0; i < size_; i++) 76 { 77 data_[i] = value; 78 79 } 80 } 81 }
task.cpp:
1 #include <iostream> 2 #include "Vector.hpp" 3 void test1() { 4 using namespace std; 5 6 int n; 7 cout << "Enter 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 cout << "x1: "; output(x1); 15 16 Vector<int> x2(n, 42); 17 const Vector<int> x3(x2); 18 19 cout << "x2: "; output(x2); 20 cout << "x3: "; output(x3); 21 22 x2.at(0) = 77; 23 x2.at(1) = 777; 24 cout << "x2: "; output(x2); 25 cout << "x3: "; output(x3); 26 } 27 28 void test2() { 29 using namespace std; 30 31 int n, index; 32 while(cout << "Enter n and index: ", cin >> n >> index) { 33 try { 34 Vector<int> v(n, n); 35 v.at(index) = -999; 36 cout << "v: "; output(v); 37 } 38 catch (const exception &e) { 39 cout << e.what() << endl; 40 } 41 } 42 } 43 44 int main() { 45 cout << "测试1: 模板类接口测试\n"; 46 test1(); 47 48 cout << "\n测试2: 模板类异常处理测试\n"; 49 test2(); 50 }
任务5:
people.hpp:
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 6 using namespace std; 7 8 class people 9 { 10 private: 11 int xuehao; 12 string name; 13 string major; 14 int score; 15 public: 16 people() = default; 17 ~people() = default; 18 19 string get_major()const 20 { 21 return major; 22 } 23 int get_score()const 24 { 25 return score; 26 } 27 28 friend ostream& operator<<(ostream& out, people& p); 29 friend istream& operator>>(istream& in, people& p); 30 31 32 }; 33 34 ostream& operator<<(ostream& out, people& p) 35 { 36 out << setiosflags(ios_base::left); 37 out << setw(10) << p.xuehao 38 << setw(10) << p.name 39 << setw(10) << p.major 40 << setw(10) << p.score << endl; 41 return out; 42 43 } 44 45 istream& operator>>(istream& in, people& p) 46 { 47 in >> p.xuehao >> p.name >> p.major >> p.score; 48 return in; 49 50 51 52 }
tools.hpp:
1 #pragma once 2 #include"people.hpp" 3 #include<iostream> 4 #include<string> 5 #include<fstream> 6 #include<vector> 7 8 bool paixu(const people& p1, const people& p2) 9 { 10 if (p1.get_major() < p2.get_major()) 11 { 12 return true; 13 } 14 if (p1.get_major() == p2.get_major()) 15 { 16 return p1.get_score() > p2.get_score(); 17 18 } 19 return false; 20 21 } 22 23 void output(ostream& out, vector<people>& v) 24 { 25 for (auto& i : v) 26 { 27 out << i; 28 } 29 30 } 31 32 void save(const string& filename, vector<people>& v) 33 { 34 ofstream out(filename); 35 if (!out.is_open()) 36 { 37 cout << "文件写入失败" << endl; 38 exit(0); 39 } 40 41 output(out, v); 42 out.close(); 43 44 } 45 46 void load(const string& filename, vector<people>& v) 47 { 48 ifstream in(filename); 49 50 if (!in.is_open()) 51 { 52 cout << "文件读出失败" << endl; 53 exit(0); 54 } 55 56 string firstline; 57 getline(in, firstline); 58 people p; 59 while (in >> p) 60 { 61 v.push_back(p); 62 } 63 64 in.close(); 65 }
task5.cpp:
1 #include"people.hpp" 2 #include"tools.hpp" 3 #include<iostream> 4 #include<string> 5 #include<vector> 6 #include<algorithm> 7 8 int main() 9 { 10 11 vector<people> v; 12 load("data5.txt", v); 13 sort(v.begin(), v.end(), paixu); 14 15 output(cout, v); 16 save("ans.txt", v); 17 18 19 return 0; 20 }
总结:通过本次实验我体验到异常处理的基础用法,异常处理的机制和流程
;也训练了综合应用类的封装、继承、多态特性及现代C++标准库编写正确、高效、安全代码
标签:文件,const,index,int,模版,Vector,实验,include,size From: https://www.cnblogs.com/-transparent/p/18623263