实验任务4
Vector.hpp
#pragma once #include <iostream> #include <stdexcept>
#include <string> using namespace std; template <typename T> class Vector { public: Vector (int x = 0, const T& value = T()); Vector (const Vector& other);int get_size() const; T& at(int e); T& operator[](int e); template<typename T1> friend void output (const Vector<T1>& v); private: T* data; int size; }; template <typename T> Vector<T>::Vector(int x, const T &value) : size(x) { data = new T[size]; for (int i = 0; i < size; ++i) { data[i] = value; } } template <typename T> Vector<T>::Vector(const Vector& other) : size(other.size) { data = new T[size]; for (int i = 0;i < size; ++i) { data[i] = other.data[i]; } } template<typename T> Vector<T>::get_size() const { return size; } template<typename T> T& Vector <T>::at(int e) { if(e >= size) { throw std::out_of_range("Index out of range"); } return data[e]; } template <typename T> T& Vector<T>::operator[](int e) { if(e >= size) { throw out_of_range("Index out of range"); } return data[e]; } template <typename T1> void output(const Vector<T1>& v) { for (int i = 0;i < v.size;++i) { cout << v.data[i] << ","; } cout << "\b \b" <<endl; }
task4.cpp
#include <iostream> #include "Vector.hpp" void test() { using namespace std; int n; cin >> n; Vector<double> x1(n); for(auto i = 0; i < n; ++i) x1.at(i) = i * 0.7; output(x1); Vector<int> x2(n, 42); Vector<int> x3(x2); output(x2); output(x3); x2.at(0) = 77; output(x2); x3[0] = 999; output(x3); } int main() { test(); }
实验任务5
task5.cpp
#include<iostream> #include<iomanip> #include<fstream> using namespace std; void output(std::ostream& output){ output << " "; for (int i = 97; i < 123; i++){ output << setw(2) << char(i); } cout << endl; for (int i = 1; i <= 26; i++){ output << setw(2) << i; int j; for (j=i;j<i+26;j++){ output << setw(2) << char(65+j%26); } cout << endl; } } int main() { ofstream out; output(cout); out.open("cipher_key.txt"); output(out); return 0; }
标签:文件,const,int,Vector,实验,template,output,模板,size From: https://www.cnblogs.com/yzsya/p/17909538.html