Task4:
vector.hpp:
#include<iostream> #include<string> #include<stdexcept> using namespace std; template <typename T> class Vector { private: T* data; int size; public: Vector() : data(nullptr), size(0) {} Vector(int n) : size(n) { data = new T[n]; for (int i = 0; i < n; i++) { data[i] = T(); } } Vector(int n, T t):size{n} { data= new T[n]; for (int i = 0; i < n; i++) data[i] = t; } Vector(const Vector& other) : size(other.size) { data = new T[size]; for (int i = 0; i < size; i++) { data[i] = other.data[i]; } } int get_size() const { return size; } ~Vector() { delete[] data; } T& at(int i) { if (i < 0 || i >= size) { throw out_of_range("Index out of range"); } return data[i]; } T& operator[](int i) { return at(i); } friend void output(const Vector& v) { for (int i = 0; i < v.size; i++) { cout << v.data[i] << " "; } cout << 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(); }
Task5:
main:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
void output(std::ostream& output) {
output << " ";
for(int i=97;i<123;i++) {
output << setw(2) << char(i);
}
output << endl;
for(int i=1;i<=26;i++) {
output << setw(2) << i;
for(int j=i;j<i+26;j++) {
output << setw(2) << char(65 + j % 26);
}
output << endl;
}
}
int main() {
ofstream out;
output(cout);
out.open("cipher_key.txt");
output(out);
return 0;
}
标签:文件,int,Vector,实验,output,include,data,模板,size From: https://www.cnblogs.com/yanmao/p/17909732.html