赞
踩
#pragma once #include <iostream> using namespace std; template<class T> class Vector { public: Vector(int capacity) { this->m_Capacity = capacity; this->m_Vector = new T[this->m_Capacity]; this->m_Size = 0; } Vector(const Vector& v) { this->m_Capacity = v.m_Capacity; this->m_Size = v.m_Size; this->m_Vector = new T[v.m_Capacity]; if (v.m_Size) { for (int i = 0; i < v.m_Size; ++i) { this->m_Vector[i] = v.m_Vector[i]; } } } Vector& operator = (const Vector& v) { if (this->m_Vector != NULL) { delete[] this->m_Vector; this->m_Vector = NULL; this->m_Capacity = 0; this->m_Size = 0; } this->m_Capacity = v.m_Capacity; this->m_Size = v.m_Size; this->m_Vector = new T[v.m_Capacity]; if (v.m_Size) { for (int i = 0; i < v.m_Size; ++i) { this->m_Vector[i] = v.m_Vector[i]; } } return *this; } T& operator [](int n) { return this->m_Vector[n]; } void Push_back(T n) { if (this->m_Size >= this->m_Capacity) { return; } this->m_Vector[this->m_Size] = n; this->m_Size++; } void Pop_back() { if (this->m_Size <= 0) { return; } this->m_Size--; } int Size() { return this->m_Size; } ~Vector() { if (this->m_Vector != NULL) { delete[] this->m_Vector; this->m_Vector = NULL; this->m_Capacity = 0; this->m_Size = 0; } } private: T* m_Vector; int m_Capacity; int m_Size; };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。