task4:
Vector.hpp:
1 #pragma once 2 #include<iostream> 3 #include<stdexcept> 4 using namespace std; 5 6 template<typename T> 7 class Vector 8 { 9 private: 10 int size; 11 T* ptr; 12 public: 13 Vector(int n); 14 Vector(int n, T value); 15 Vector(const Vector<T>& v); 16 ~Vector(); 17 18 int get_size() 19 { 20 return size; 21 } 22 23 T& at(int index)const; 24 T& at(int index); 25 26 T& operator[](int index)const; 27 T& operator[](int index); 28 29 template<typename T1> //模板类中的友元函数,一定要再次声明一个T1,不然就要报错 30 friend void output(const Vector<T1>& v); 31 }; 32 33 template<typename T> 34 Vector<T>::Vector(int n) :size(n) 35 { 36 if (n < 0) 37 { 38 throw length_error("Vector constructor:negative size"); 39 } 40 else 41 { 42 ptr = new T[size]; 43 } 44 } 45 46 47 template<typename T> 48 Vector<T>::Vector(int n,T value):size(n) 49 { 50 51 if (n < 0) 52 { 53 throw length_error("Vector constructor:negative size"); 54 } 55 else 56 { 57 ptr = new T[size]; 58 for (int i = 0; i < size; i++) 59 { 60 ptr[i] = value; 61 62 } 63 } 64 65 66 } 67 68 template<typename T> 69 Vector<T>::Vector(const Vector<T>& v):size(v.size),ptr(new T[size]) 70 { 71 for (int i = 0; i < size; i++) 72 { 73 ptr[i] = v.ptr[i]; 74 } 75 76 } 77 78 template<typename T> 79 Vector<T>::~Vector() 80 { 81 82 delete[]ptr; 83 } 84 85 template<typename T> 86 T& Vector<T>::at(int index)const 87 { 88 if (index < 0 || index >= size) 89 { 90 throw out_of_range("Vector:index out of range"); 91 } 92 else 93 { 94 return ptr[index]; 95 } 96 97 } 98 99 template<typename T> 100 T& Vector<T>::at(int index) 101 { 102 return static_cast<const Vector*>(this)->at(index); 103 104 } 105 106 template<typename T> 107 T& Vector<T>::operator[](int index)const 108 { 109 if (index < 0 || index >= size) 110 { 111 throw out_of_range("Vector:index out of range"); 112 } 113 else 114 { 115 return ptr[index]; 116 } 117 118 } 119 template<typename T> 120 T& Vector<T>::operator[](int index) 121 { 122 return static_cast<const Vector*>(this)->operator[](index); 123 124 } 125 126 template<typename T> 127 void output(const Vector<T>& v) 128 { 129 for (int i = 0; i < v.size; i++) 130 { 131 cout << v.ptr[i] << ", "; 132 } 133 cout <<"\b\b " << endl; 134 135 }View Code
task4:
1 #include <iostream> 2 #include "Vector.hpp" 3 4 void test1() 5 { 6 using namespace std; 7 8 int n; 9 cout << "Enter n: "; 10 cin >> n; 11 12 Vector<double> x1(n); 13 for(auto i = 0; i < n; ++i) 14 x1.at(i) = i * 0.7; 15 16 cout << "x1: "; output(x1); 17 18 Vector<int> x2(n, 42); 19 const Vector<int> x3(x2); 20 21 cout << "x2: "; output(x2); 22 cout << "x3: "; output(x3); 23 24 x2.at(0) = 77; 25 x2.at(1) = 777; 26 cout << "x2: "; output(x2); 27 cout << "x3: "; output(x3); 28 } 29 30 void test2() { 31 using namespace std; 32 33 int n, index; 34 while(cout << "Enter n and index: ", cin >> n >> index) { 35 try { 36 Vector<int> v(n, n); 37 v.at(index) = -999; 38 cout << "v: "; output(v); 39 } 40 catch (const exception &e) { 41 cout << e.what() << endl; 42 } 43 } 44 } 45 46 int main() { 47 cout << "测试1: 模板类接口测试\n"; 48 test1(); 49 50 cout << "\n测试2: 模板类异常处理测试\n"; 51 test2(); 52 }View Code
task5:
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 }View Code
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 }View Code
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 }View Code
实验总结:
通过这次实验,加深了对模板类的使用,了解了部分模板类中的友元函数用模板时,还需要重新定义一个模板参数T1,等,否则就会报错,比如task4中的outpu函数,起初没有用T1,就出现了报错;还学会了报错的简单处理
也加深了对流类库的使用,再task5中,也了解到sort函数,最后一个参数只需要是函数指针即可,不是完整函数
标签:index,const,int,c++,Vector,实验,include,size From: https://www.cnblogs.com/jiangyuhui/p/18613333