#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(); return 0; }
#include <iostream> #include <stdexcept> template <typename T> class Vector { private: T* data; size_t size; public: Vector(size_t n) : size(n) { data = new T[size]; } Vector(size_t n, const T& value) : size(n) { data = new T[size]; for (size_t i = 0; i < size; ++i) { data[i] = value; } } Vector(const Vector& other) : size(other.size) { data = new T[size]; for (size_t i = 0; i < size; ++i) { data[i] = other.data[i]; } } ~Vector() { delete[] data; } size_t get_size() const { return size; } T& at(size_t i) { if (i >= size) { throw std::out_of_range("Index out of range"); } return data[i]; } T& operator[](size_t i) { if (i >= size) { throw std::out_of_range("Index out of range"); } return data[i]; } friend void output(const Vector& v) { for (size_t i = 0; i < v.size; ++i) { std::cout << v.data[i] << " "; } std::cout << std::endl; } };
#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; }
标签:std,Vector,实验,output,include,data,size From: https://www.cnblogs.com/gycgy/p/17910372.html