1.1 Vector.hpp:
#include <iostream> #include <string> #include <iomanip> using namespace std; template <class T> class Vector { public: Vector(int n) : size{n} { p = new T[size]; } Vector(int n, T value) : size{n} { p = new T[size]; int i; for (i = 0; i < size; i++) p[i] = value; } ~Vector() { delete[] p; } T &operator[](int index) { if (index >= 0 && index < size) return p[index]; } T &at(int index) { if (index >= 0 && index < size) return p[index]; } Vector(const Vector<T> &v1) : size{v1.size} { p = new T[size]; int i; for (i = 0; i < size; i++) p[i] = v1.p[i]; } int get_size() const { return size; } friend void output(Vector<T> &v) { int i; for (i = 0; i < v.size; i++) cout << v.p[i] << " "; cout << endl; } private: int size; T *p; };
1.2 task4.cpp:
#include <iostream> #include "Vector.hpp" void test() { using namespace std; int n; while(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); } cin.clear(); } int main() { test(); }
1.3 运行结果:
2.1 task5.cpp:
#include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; void output(ostream &out) { int n = 26; out << " "; for (auto i = 'a'; i <= 'z'; i++) { out << setw(3) << i; } out << endl; for (int i = 1; i <= 26; i++) { out << setw(3) << i; n++; for (int j = n; j < n + 26; j++) { int ch = j % 26 + 65; out << setw(2) << char(ch); } out << endl; } } int main() { ofstream out; output(cout); out.open("cipher_key.txt"); output(out); out.close(); }
2.2 运行结果:
2.3 cipher_key运行结果:
标签:index,int,Vector,实验,IO,output,include,模板,size From: https://www.cnblogs.com/jyksblog/p/16959601.html