4.hpp
#include<iostream> #include<stdexcept> template<typename T> class Vector { private: int size; T* p; public: Vector<T>(int s); Vector<T>(int s, T value); Vector<T>(Vector<T>& x); ~Vector<T>(); int get_size()const; T& at(int i); T & operator[](int i); template < typename T1>; friend void output(const Vector<T1> t); }; template<typename T> Vector<T>::Vector<T>(int s) { size = s; p = new T[s]; } template<typename T> Vector<T>::Vector<T>(int s, T value) { size = s; p = new T[s]; for (int i = 0;i < s;i++) { p[i] = value; } } template<typename T> Vector<T>::Vector(Vector<T>& x) { size = x.size; p = new T[x.size]; for (int i = 0;i < x.size;i++) { p[i] = x.p[i]; } } template<typename T> Vector<T>::~Vector() { delete[] p; } template<typename T> int Vector<T>:: get_size()const { return size; } template<typename T> T& Vector<T>::at(int i) { if (i<size && i>size) throw out_of_range("越界"); else return p[i]; }template<typename T> T& Vector<T>::operator[](int i) { if (i<size && i>size) throw out_of_range("越界"); else return p[i]; } template<typename T1> void output(const Vector<T1>& t) { for (int i = 0;i < t.get_size;i++) cout << v.p[i] << endl; }
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.cpp
#include<iostream> #include<fstream> #include<iomanip> void output(std::ostream& out) { int i, j; for (i = 0;i <= 26;i++) { out.width(2); if (i == 0) out << ' '; else out << i << std::endl; for (j = 0;j <= 26;j++) { if (i == 0) { out << 'a' + j % 26; } else { out << 'A' + (i + j) % 26; } } } } int main() { output(std::cout); std::ofstream outFile("cipher_key.txt"); if (outFile.is_open()) { output(outFile); outFile.close(); } return 0; }
标签:int,Vector,实验,template,output,include,size From: https://www.cnblogs.com/32re/p/17909655.html