任务三:
任务四:
#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(); }
#pragma once #include<iostream> #include<vector> using namespace std; template<typename T> class Vector { public: Vector(int n0); Vector(int n0, T value0); Vector(const Vector<T>& v1); ~Vector(); int get_size()const { return n; } T &at(int i) { return p[i]; } T &operator[](int i) {return p[i]; } template<typename T> friend void output(Vector<T>&x); T value; private: int n; T *p; }; template<typename T> Vector<T>::Vector(int n0) { n = n0; p = new T[n]; } template<typename T> Vector<T>::Vector(int n0,T value0){ n = n0; value = value0; p = new T[n]; for (auto i = 0; i < n; i++) p[i] = value; } template<typename T> Vector<T>::Vector(const Vector<T>& v1) { n = v1.n; p = new T[n]; for (int i = 0; i < n; i++) { p[i] = v1.p[i]; } } template<typename T> Vector<T>::~Vector(){ delete p; } template<typename T> void output(Vector<T>& x) { for (int i = 0; i < x.n; i++) cout << x.at(i) << ' '; cout << endl; }
标签:int,x2,Vector,实验,template,n0,output From: https://www.cnblogs.com/hjr123/p/16942127.html