当前位置:   article > 正文

C++ 基于模板类实现的自定义数组_c++ 模板传入的常量能定义数组吗

c++ 模板传入的常量能定义数组吗
#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;
};



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/874863
推荐阅读
相关标签
  

闽ICP备14008679号