迭代器和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;
};