Task4
Vector.hpp:
1 #ifndef VECTOR_HPP 2 #define VECTOR_HPP 3 4 #include <iostream> 5 #include <exception> 6 7 template <typename T> 8 class Vector { 9 private: 10 T* elements; 11 size_t size_; 12 13 public: 14 // 构造函数,动态指定大小,默认初始化元素 15 Vector(size_t n); 16 17 // 构造函数,指定大小并初始化每个元素为指定值 18 Vector(size_t n, const T& value); 19 20 // 拷贝构造函数,实现深复制 21 Vector(const Vector<T>& other); 22 23 // 析构函数,释放内存资源 24 ~Vector(); 25 26 // 获取数组元素个数 27 size_t get_size() const; 28 29 // 通过at函数访问元素,进行越界检查 30 T& at(size_t index); 31 const T& at(size_t index) const; 32 33 // 重载[]运算符,进行越界检查 34 T& operator[](size_t index); 35 const T& operator[](size_t index) const; 36 }; 37 38 // 构造函数,动态指定大小,默认初始化元素 39 template <typename T> 40 Vector<T>::Vector(size_t n) { 41 if (n < 0) { 42 throw std::length_error("Size cannot be negative"); 43 } 44 size_ = n; 45 elements = new T[n](); 46 } 47 48 // 构造函数,指定大小并初始化每个元素为指定值 49 template <typename T> 50 Vector<T>::Vector(size_t n, const T& value) { 51 if (n < 0) { 52 throw std::length_error("Size cannot be negative"); 53 } 54 size_ = n; 55 elements = new T[n]; 56 for (size_t i = 0; i < n; ++i) { 57 elements[i] = value; 58 } 59 } 60 61 // 拷贝构造函数,实现深复制 62 template <typename T> 63 Vector<T>::Vector(const Vector<T>& other) { 64 size_ = other.size_; 65 elements = new T[size_]; 66 for (size_t i = 0; i < size_; ++i) { 67 elements[i] = other.elements[i]; 68 } 69 } 70 71 // 析构函数,释放内存资源 72 template <typename T> 73 Vector<T>::~Vector() { 74 delete[] elements; 75 } 76 77 // 获取数组元素个数 78 template <typename T> 79 size_t Vector<T>::get_size() const { 80 return size_; 81 } 82 83 // 通过at函数访问元素,进行越界检查 84 template <typename T> 85 T& Vector<T>::at(size_t index) { 86 if (index >= size_) { 87 throw std::out_of_range("Index out of range"); 88 } 89 return elements[index]; 90 } 91 92 template <typename T> 93 const T& Vector<T>::at(size_t index) const { 94 if (index >= size_) { 95 throw std::out_of_range("Index out of range"); 96 } 97 return elements[index]; 98 } 99 100 // 重载[]运算符,进行越界检查 101 template <typename T> 102 T& Vector<T>::operator[](size_t index) { 103 if (index >= size_) { 104 throw std::out_of_range("Index out of range"); 105 } 106 return elements[index]; 107 } 108 109 template <typename T> 110 const T& Vector<T>::operator[](size_t index) const { 111 if (index >= size_) { 112 throw std::out_of_range("Index out of range"); 113 } 114 return elements[index]; 115 } 116 117 // 友元函数,输出数组元素 118 template <typename T> 119 std::ostream& output(std::ostream& os, const Vector<T>& v) { 120 for (size_t i = 0; i < v.get_size(); ++i) { 121 os << v[i] << " "; 122 } 123 os << std::endl; 124 return os; 125 } 126 127 // 重载输出流运算符,方便直接输出对象 128 template <typename T> 129 std::ostream& operator<<(std::ostream& os, const Vector<T>& v) { 130 return output(os, v); 131 } 132 133 #endifView Code
task4.cpp:
1 #include <iostream> 2 #include "Vector.hpp" 3 4 void test1() { 5 using namespace std; 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 cout << "x1: "; 14 output(x1); 15 Vector<int> x2(n, 42); 16 const Vector<int> x3(x2); 17 cout << "x2: "; 18 output(x2); 19 cout << "x3: "; 20 output(x3); 21 x2.at(0) = 77; 22 x2.at(1) = 777; 23 cout << "x2: "; 24 output(x2); 25 cout << "x3: "; 26 output(x3); 27 } 28 29 void test2() { 30 using namespace std; 31 int n, index; 32 while (cout << "Enter n and index: ", cin >> n >> index) { // 多组输入,按下Ctrl+Z终止 33 try { 34 Vector<int> v(n, n); 35 v.at(index) = -999; 36 cout << "v: "; 37 output(v); 38 } 39 catch (const std::exception &e) { 40 cout << e.what() << endl; 41 } 42 } 43 } 44 45 int main() { 46 std::cout << "测试1: 模板类接口测试\n"; 47 test1(); 48 std::cout << "\n测试2: 模板类异常处理测试\n"; 49 test2(); 50 return 0; 51 }View Code
Task5
task5.cpp:
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 5 void test() { 6 using namespace std; 7 8 vector<students>v; 9 10 load("data5.txt", v); 11 sort(v.begin(), v.end(), compare); 12 output(cout, v); 13 save("ans.txt", v); 14 15 } 16 17 int main() { 18 test(); 19 }View Code
标签:index,elements,const,Vector,实验,template,size From: https://www.cnblogs.com/g055140/p/18622242