Huy Vũ Quang

29 bundles
3 filesa month ago
1

Vector Template

CustomVector.h

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
#ifndef _CUSTOM_VECTOR_ #define _CUSTOM_VECTOR_ #include <exception> template<typename T, int s> class CustomVector final { private: T* mData; int mIdx; public: CustomVector(); ~CustomVector(); void push(const T& item); T pop(); T& back(); T& front(); }; template <typename T, int s> CustomVector<T,s>::CustomVector() { mData = new T[s]; mIdx = 0; } template <typename T, int s> CustomVector<T,s>::~CustomVector() { delete[] mData; } template <typename T, int s> void CustomVector<T,s>::push(const T& item) { if (mIdx < s) { mData[mIdx] = item; mIdx++; } else { throw std::runtime_error("Insufficient size"); } } template <typename T, int s> T CustomVector<T,s>::pop() { if (mIdx >= 0) { mIdx--; T item = mData[mIdx]; return item; } throw std::runtime_error("Vector is empty"); } template <typename T, int s> T& CustomVector<T,s>::front() { return mData[0]; } template <typename T, int s> T& CustomVector<T,s>::back() { return mData[mIdx - 1]; } #endif

CustomVectorOp.h

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
#ifndef _CUSTOM_VECTOR_ #define _CUSTOM_VECTOR_ #include <exception> template <typename T, int s> class VectorIterator; template<typename T, int s> class CustomVector final { friend class VectorIterator<T, s>; private: T* mData; int mIdx; public: CustomVector(); ~CustomVector(); void push(const T& item); T pop(); T& back(); T& front(); VectorIterator<T, s> createIterator() { return VectorIterator<T, s>(this); } }; template <typename T, int s> CustomVector<T,s>::CustomVector() { mData = new T[s]; mIdx = 0; } template <typename T, int s> CustomVector<T,s>::~CustomVector() { delete[] mData; } template <typename T, int s> void CustomVector<T,s>::push(const T& item) { if (mIdx < s) { mData[mIdx] = item; mIdx++; } else { throw std::runtime_error("Insufficient size"); } } template <typename T, int s> T CustomVector<T,s>::pop() { if (mIdx >= 0) { mIdx--; T item = mData[mIdx]; return item; } throw std::runtime_error("Vector is empty"); } template <typename T, int s> T& CustomVector<T,s>::front() { return mData[0]; } template <typename T, int s> T& CustomVector<T,s>::back() { return mData[mIdx - 1]; } template <typename T, int s> class VectorIterator { CustomVector<T, s> *mVector; int mIdx; public: VectorIterator(CustomVector<T, s> *v) { mVector = v; mIdx = 0; } void next() { mIdx++; } bool isDone() { return mIdx == s; } T& currentItem() { return mVector->mData[mIdx]; } T& operator*() { return currentItem(); } }; #endif

VectorIterator

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
#ifndef _CUSTOM_VECTOR_ #define _CUSTOM_VECTOR_ #include <exception> template <typename T, typename U, int s> class VectorIterator; template<typename T, int s> class CustomVector final { friend class VectorIterator<T, CustomVector, s>; private: T* mData; int mIdx; public: CustomVector(); ~CustomVector(); void push(const T& item); T pop(); T& back(); T& front(); VectorIterator<T, CustomVector, s> createIterator() { return VectorIterator<T, CustomVector, s>(this); } }; template <typename T, int s> CustomVector<T,s>::CustomVector() { mData = new T[s]; mIdx = 0; } template <typename T, int s> CustomVector<T,s>::~CustomVector() { delete[] mData; } template <typename T, int s> void CustomVector<T,s>::push(const T& item) { if (mIdx < s) { mData[mIdx] = item; mIdx++; } else { throw std::runtime_error("Insufficient size"); } } template <typename T, int s> T CustomVector<T,s>::pop() { if (mIdx >= 0) { mIdx--; T item = mData[mIdx]; return item; } throw std::runtime_error("Vector is empty"); } template <typename T, int s> T& CustomVector<T,s>::front() { return mData[0]; } template <typename T, int s> T& CustomVector<T,s>::back() { return mData[mIdx - 1]; } template <typename T, typename U, int s> class VectorIterator { CustomVector<T, s> *mVector; int mIdx; public: VectorIterator(CustomVector<T, s> *v) { mVector = v; mIdx = 0; } void next() { mIdx++; } bool isDone() { return mIdx == s; } const T& currentItem() { return mVector->mData[mIdx]; } }; #endif