#pragma once #include<iostream> #include<stdexcept> using namespace std; template<typename T> class Vector { private: int size; T* ptr; public: Vector(int n):size{n} { if (n < 0) throw std::length_error("vector constructor:negative size"); ptr = new T[size]; } Vector(int n, T value):size{n} { if (n < 0) throw std::length_error("vector constructor:negative size"); ptr = new T[size]; for (auto i = 0; i < size; ++i) { ptr[i] = value; } } Vector(const Vector<T>& vi):size{vi.size},ptr{new T[size]} { for (auto i = 0; i < size; i++) { ptr[i] = vi.ptr[i]; } } ~Vector() { delete[] ptr; } int get_size() const { return size; } T& at(int index) { if (index < 0 || index >= size) throw std::out_of_range("Vector::at()"); return ptr[index]; } T& at(int index) const { if (index < 0 || index >= size) throw std::out_of_range("Vector::at()"); return ptr[index]; } T& operator[](int index) { return ptr[index]; } friend void output(Vector<T>& v) { for (auto i = 0; i < v.get_size(); i++) cout << v.at(i) << ","; cout << "\b\b\n"; } };
#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(); }
#include<iostream> #include<iomanip> #include<fstream> using namespace std; void output(ostream &out) { for(int i=0;i<=26;i++){ for(int j=0;j<=26;j++) { char c,b; if(i==0&&j==0){ char d = ' '; out<<setw(2)<<d; } else if(j==0&&i!=0){ out<<setw(2)<<i; } else if(i==0&&j!=0){ char c='a'+j-1; out<<setw(2)<<c; } else if(i!=0&&j!=0){ char b=(i+j-1+26)%26+'A'; out<<setw(2)<<b; } } out<<endl; } } int main(){ output(cout); ofstream outFile("cipher_key.txt"); output(outFile); outFile.close(); return 0; }
标签:index,int,Vector,实验,include,ptr,size From: https://www.cnblogs.com/tantivy/p/17909016.html