#include <iostream> #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(const Vector& other) : size(other.size) { data = new T[size]; for (int i = 0; i < size; i++) { data[i] = other.data[i]; } } Vector(int n, T t):size{n} { data= new T[n]; for (int i = 0; i < n; i++) data[i] = t; } ~Vector() { delete[] data; } int get_size() const { return size; } 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; } };View Code
task4.cpp
#include <iostream> #include "Vector.hpp" void test() { using namespace std; int n; cout << "Enter the size of the vector: "; cin >> n; Vector<double> x1(n); for (auto i = 0; i < n; ++i) x1[i] = i * 0.618; x1.output(); Vector<int> x2(n, 777); Vector<int> x3(x2); x2.output(); x3.output(); x2[0] = 42; x2.output(); x3[0] = 666; x3.output(); } int main() { test(); return 0; }View Code
实验五
task5
#include <iostream> #include <fstream> void output(std::ostream &out) { for (int i = 0; i <= 26; i++) { out.width(2); if (i == 0) out << ' '; else out << i; for (int j = 0; j <= 26; j++) { out << ' '; if (i == 0) out << static_cast<char>('a' + (i + j) % 26); else out << static_cast<char>('A' + (i + j) % 26); } out << '\n'; } } void output() { output(std::cout); std::ofstream ofile("cipher_key.txt"); if (ofile.is_open()) { output(ofile); ofile.close(); } else { std::cerr << "Unable to open file." << std::endl; } } int main() { output(); return 0; }View Code
标签:int,Vector,实验,output,include,data,size From: https://www.cnblogs.com/grcvafg/p/17910411.html