C++ 实现迭代器和TArray容器

迭代器和TArray容器

容器遍历和迭代器遍历

int main()
{

	//迭代器
	struct FTestA
	{
		FTestA() {}
		FTestA(int InA)
			:ID(InA)
		{

		}

		bool operator != (const FTestA& A)
		{
			return A.ID != ID;
		}

		inline int GetID() { return ID; }

	protected:
		int ID;
	};

	TArray<FTestA> ArrayA;
	FTestA A1(1);
	ArrayA.Add(A1);
	ArrayA.Add(FTestA(100));
	ArrayA.Add(FTestA(1002));
	ArrayA.Add(FTestA(1001));
	ArrayA.Add(FTestA(1002));
	ArrayA.Add(FTestA(10303));
	ArrayA.Add(FTestA(12004));
	ArrayA.Add(FTestA(10404));
	ArrayA.Add(FTestA(10204));
	ArrayA.Add(FTestA(10014));
	ArrayA.Add(FTestA(102204));
	ArrayA.Add(FTestA(10704));

	ArrayA.RemoveAt(1);

	for (int i = 0; i < ArrayA.Num(); i++)
	{
		cout << ArrayA[i]->GetID() << endl;
	}

	for (TArray<FTestA>::TIterator It = ArrayA.Begin();It != ArrayA.End(); It++)
	{
		cout << (*It).GetID() << endl;
	}

	return 0;

}

template<class ContainerType,typename ElementTye>
class TIndexedContainerIterator
{
public:
	typedef TIndexedContainerIterator<ContainerType, ElementType> TIterator;

	//构造
	TIndexedContainerIterator(ContainerType& InContainer, int InIndex = 0)
		:Container(InContainer)
		, Index(InIndex)
	{}

	//深拷贝
	TIndexedContainerIterator(const TIterator& InIterator)
		:Container(InContainer)
		, Index(InIterator.Index)
	{}

	
	bool operator !=(const TIterator& InIterator)
	{
		return Container[Index] != InIterator.Container[InIterator.Index];
	}

	TIterator& operator++(int)
	{
		++index;
		return *this;
	}

	TIterator& operator--(int)
	{
		--index;
		return *this;
	}

	TIterator &operator == (const TIterator& InIterator)
	{
		Index = InIterator.Index;
		Container = InIterator.Container;

		return *this;
	}

	ElementTye& operator*()
	{
		return *Container[Index];
	}

protected:
	ContainerType& Container;
	int Index;

};

template<typename ElementType>
class TArray
{
public:
	typedef TIndexedContainerIterator<TArray<ElementType>, ElementType> TIterator;

	TArray()
		:Data(nullptr)
		,Size(0)
		,Allocation(10)
	{
		Data = (ElementType**)malloc(sizeof(int) * Allocation); //分配下标
		memset(Data, 0, sizeof(int) * Allocation);
		for (int i = 0; i < Allocation; i++)
		{
			Data[i] = (ElementType**)malloc(sizeof(ElementType));
			memset(Data[i], 0, sizeof(ElementType));
		}
	}

	int Num()
	{
		return Size;
	}

	void Add(ElementType&& InType)
	{
		Add(InType);
	}

	void Add(ElementType& InType)
	{
		if (Size >= Allocation)
		{
		//判断内存不够了,就分配
			int LastAllocation = Allocation;
			Allocation += 10;
			Data = (ElementType**)realloc(Data, sizeof(int) * Allocation);
			for (int i = LastAllocation; i < Allocation; i++)
			{
				Data[i] = (ElementType*)malloc(sizeof(ElementType));
				memset(Data[i], 0, sizeof(ElementType));
			}
		}
		memcpy(Data[Size], &InType, sizeof(ElementType));
		Size++;
	}

	void RemoveAt(int Index)
	{
		for (int i = Index + 1; i <= Size; i++)
		{
			memcpy(Data[i - 1], Data[i], sizeof(ElementType));
		}

		memset(Data[Size], 0, sizeof(ElementType));

		Size--;
	}

	ElementType *operator[](int Index)
	{
		return Data[Index];
	}

	TIterator Begin()
	{
		return TIterator(*this, 0);
	}

	TIterator End()
	{
		return TIterator(*this, Size);
	}

	~TArray()
	{
		for (int i = 0; i < Allocation; i++)
		{
			free(Data[i]);
		}

		free(Data);
	}

protected:
	ElementType** Data; 
	int Size;
	int Allocation;	

};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值