task 4
代码:
Vector.hpp
1 #pragma once 2 #include <iostream> 3 4 using namespace std; 5 6 template < typename T> 7 class Vector { 8 public: 9 Vector(int n); 10 Vector(int n, T value); 11 Vector(const Vector<T>& vi); 12 ~Vector(); 13 14 int get_size() const; 15 T& at(int index) const; 16 T& operator[](int index) const; 17 18 template<typename T> 19 friend void output(const Vector<T>& vi); 20 21 private: 22 int size; 23 T* ptr; 24 }; 25 26 template<typename T> 27 Vector<T>::Vector(int n) { 28 if (n < 0) 29 throw length_error("Vector<T>::Vector(int)大小为负数"); 30 31 ptr = new T[n]; 32 size = n; 33 } 34 35 template<typename T> 36 Vector<T>::Vector(int n, T value) { 37 if (n < 0) 38 throw length_error("Vector<T>::Vector(int, T)大小为负数"); 39 40 ptr = new T[n]; 41 size = n; 42 for (auto i = 0; i < n; i++) { 43 ptr[i] = value; 44 } 45 } 46 47 template<typename T> 48 Vector<T>::Vector(const Vector<T>& vi) { 49 ptr = new T[vi.get_size()]; 50 51 for (auto i = 0; i < vi.get_size(); i++) { 52 ptr[i] = vi[i]; 53 } 54 55 size = vi.get_size(); 56 } 57 58 template<typename T> 59 Vector<T>::~Vector() { 60 delete[] ptr; 61 } 62 63 template<typename T> 64 int Vector<T>::get_size() const { 65 return size; 66 } 67 68 template<typename T> 69 T& Vector<T>::at(int index) const { 70 if (index < 0 || index >= size) 71 throw out_of_range("Vector<T>::at()下标越界"); 72 73 return ptr[index]; 74 } 75 76 template<typename T> 77 T& Vector<T>::operator[](int index) const { 78 if (index < 0 || index >= size) 79 throw out_of_range("Vector<T>::operator[]()下标越界"); 80 return ptr[index]; 81 } 82 83 template<typename T> 84 void output(const Vector<T>& vi) { 85 for (int i = 0; i < vi.get_size(); i++) { 86 cout << vi.at(i) << " "; 87 } 88 cout << "\b \n"; 89 }
task4.cpp
1 #include <iostream> 2 #include "Vector.hpp" 3 4 void test1() { 5 using namespace std; 6 7 int n; 8 cout << "Enter n: "; 9 cin >> n; 10 11 Vector<double> x1(n); 12 for(auto i = 0; i < n; ++i) 13 x1.at(i) = i * 0.7; 14 15 cout << "x1: "; output(x1); 16 17 Vector<int> x2(n, 42); 18 const Vector<int> x3(x2); 19 20 cout << "x2: "; output(x2); 21 cout << "x3: "; output(x3); 22 23 x2.at(0) = 77; 24 x2.at(1) = 777; 25 cout << "x2: "; output(x2); 26 cout << "x3: "; output(x3); 27 } 28 29 void test2() { 30 using namespace std; 31 32 int n, index; 33 while(cout << "Enter n and index: ", cin >> n >> index) { 34 try { 35 Vector<int> v(n, n); 36 v.at(index) = -999; 37 cout << "v: "; output(v); 38 } 39 catch (const exception &e) { 40 cout << e.what() << endl; 41 } 42 } 43 } 44 45 int main() { 46 cout << "测试1: 模板类接口测试\n"; 47 test1(); 48 49 cout << "\n测试2: 模板类异常处理测试\n"; 50 test2(); 51 }
运行结果截图:
task 5
代码:
task5.cpp
1 #include <iostream> 2 #include <vector> 3 #include <fstream> 4 #include <string> 5 #include <algorithm> 6 #include <iomanip> 7 8 using namespace std; 9 10 class Stu { 11 public: 12 int get_score() const { return score; } 13 string get_major() const { return major; } 14 15 friend istream& operator>>(istream& in, Stu& s); 16 friend ostream& operator<<(ostream& out, const Stu& s); 17 18 private: 19 string id; 20 string name; 21 string major; 22 int score; 23 }; 24 25 istream& operator>>(istream& in, Stu& s) { 26 in >> s.id >> s.name >> s.major >> s.score; 27 28 return in; 29 } 30 31 ostream& operator<<(ostream& out, const Stu& s) { 32 out << setiosflags(ios_base::left); 33 out << setw(15) << s.id 34 << setw(15) << s.name 35 << setw(15) << s.major 36 << setw(15) << s.score; 37 38 return out; 39 } 40 41 bool compare_by_solutionInfo(const Stu& s1, const Stu& s2) { 42 if (s1.get_major() < s2.get_major()) 43 return true; 44 45 if (s1.get_major() == s2.get_major()) 46 return s1.get_score() > s2.get_score(); 47 48 return false; 49 } 50 51 void output(ostream& out, const vector<Stu> &s) { 52 for (auto &i : s) { 53 out << i << endl; 54 } 55 } 56 57 void save(const string& filename, vector<Stu> &s) { 58 ofstream out(filename); 59 if (!out.is_open()) { 60 cout << "fail to open file to write\n"; 61 return; 62 } 63 64 output(out, s); 65 out.close(); 66 } 67 68 void load(const string& filename, vector<Stu> &s) { 69 ifstream in(filename); 70 if (!in.is_open()) { 71 cout << "fail to open file to read\n"; 72 return; 73 } 74 75 string title_line; 76 getline(in, title_line); 77 78 Stu t; 79 while (in >> t) { 80 s.push_back(t); 81 } 82 83 in.close(); 84 } 85 86 int main() { 87 vector<Stu> s; 88 89 load("data5.txt", s); 90 sort(s.begin(), s.end(), compare_by_solutionInfo); 91 output(cout, s); 92 save("ans.txt", s); 93 94 }
运行结果截图:
标签:文件,const,index,int,Vector,实验,template,模板,size From: https://www.cnblogs.com/forgiver/p/18623688