自己实现几个容器
#ifndef __SLINKEDLIST_H__ #define __SLINKEDLIST_H__ #include <new> #include <assert.h> #include <stdio.h> #include <exception> #ifdef _TEST #define TEST_THROW(exceptioninfo) \ do \ { \ throw new exception(exceptioninfo); \ } \ while(false) #define TEST_ASSERT(assertinfo) \ do \ { \ assert(assertinfo); \ } \ while(false) #else #define TEST_THROW(exceptioninfo) \ do \ { \ } \ while(false) #define TEST_ASSERT(assertinfo) \ do \ { \ } \ while(false) #endif class CStr { public: CStr(const char * pStr) { int iLen = (int)strlen(pStr); m_pContent = new char[iLen + 1]; memcpy(m_pContent, pStr, iLen); m_pContent[iLen] = '\0'; m_iLen = iLen; } CStr(const CStr & str) { int iLen = str.Length(); m_pContent = new char[iLen + 1]; memcpy(m_pContent, str.Ptr(), iLen); m_pContent[iLen] = '\0'; m_iLen = iLen; } CStr() { int iLen = 0; m_pContent = new char[iLen + 1]; m_pContent[iLen] = '\0'; m_iLen = iLen; } ~CStr() { delete [] m_pContent; m_pContent = NULL; } const char * Ptr() const { return m_pContent; } unsigned int Length() const { return m_iLen; } CStr& operator = (const CStr & str) { delete[] m_pContent; m_pContent = NULL; int iLen = str.Length(); m_pContent = new char[iLen + 1]; memcpy(m_pContent, str.Ptr(), iLen); m_pContent[iLen] = '\0'; m_iLen = iLen; return *this; } CStr operator + (const CStr & str) const { if (str.Length() == 0) { return *this; } int iNewLen = Length() + str.Length(); char * temp = new char[iNewLen + 1]; memcpy(temp, Ptr(), Length()); memcpy(temp + Length(), str.Ptr(), str.Length()); temp[iNewLen] = '\0'; CStr ret = temp; delete [] temp; return ret; } bool operator > (const CStr & str) const { return (strcmp(Ptr(), str.Ptr()) > 0); } bool operator < (const CStr & str) const { return (strcmp(Ptr(), str.Ptr()) < 0); } bool operator == (const CStr & str) const { return (strcmp(Ptr(), str.Ptr()) == 0); } char & operator [] (unsigned int iIndex) { return m_pContent[iIndex]; } CStr SubStr(int iStartIndex, int iLen = -1) const { char * temp = new char[Length() + 1]; int j = 0; iStartIndex = iStartIndex < 0 ? 0 : iStartIndex; int iEndIndex = iLen < 0 ? Length() : iStartIndex + iLen; iEndIndex = iEndIndex > (int)Length() ? Length() : iEndIndex; for(int i = iStartIndex; i < iEndIndex; ++i, ++j) { temp[j] = Ptr()[i]; } temp[j] = '\0'; CStr ret = temp; delete [] temp; return ret; } int Find(const CStr & str) const { char * pSite = strstr(m_pContent, str.Ptr()); if(NULL == pSite) { return -1; } else { return (int)(pSite - m_pContent); } } private: int m_iLen; char * m_pContent; }; inline CStr ToStr(int v) { char temp[11]; sprintf(temp, "%d", v); return temp; } inline CStr ToStr(const CStr & v) { return v; } template<typename DATA> class CNoInitData { #define DATA_SIZE (sizeof(DATA)) public: CNoInitData() : m_bInit(false){} CNoInitData(const DATA & data) : m_bInit(true) { Create(data); } CNoInitData(const CNoInitData<DATA> & data) : m_bInit(data.m_bInit) { if(m_bInit) { Create(data.Get()); } }; ~CNoInitData() { if(m_bInit) { Destory(); } } CNoInitData<DATA> & operator = (const CNoInitData<DATA> & data) { if(m_bInit) { m_bInit = false; Destory(); } if(data.m_bInit) { m_bInit = true; Create(*(DATA *)data.m_value); } else { // 不必拷贝值了 } return *this; } operator DATA &() { if(!m_bInit) { TEST_THROW("DATA &()"); } return Get(); } operator const DATA &() const { if(!m_bInit) { TEST_THROW("DATA"); } return Get(); } operator DATA () const { if(!m_bInit) { TEST_THROW("DATA"); } return Get(); } private: void Create(const DATA & data) { ::new(m_value) DATA(data); } void Copy(const DATA & data) { *((DATA *)m_value) = data; } void Destory() { ((DATA *)m_value)->~DATA(); } DATA & Get() { return *((DATA *)m_value); } const DATA & Get() const { return *((DATA *)m_value); } private: bool m_bInit; unsigned char m_value[DATA_SIZE]; }; // 动态数组 template<typename DATA> class CArray { #define MIN_UNIT 1 public: /*构造函数*/ CArray() { m_iSize = 0; m_iCapability = MIN_UNIT; m_pData = new CNoInitData<DATA>[m_iCapability]; } CArray(const CArray<DATA> & li) { m_iSize = 0; m_iCapability = MIN_UNIT; m_pData = new CNoInitData<DATA>[m_iCapability]; PushB(li); } CArray & operator = (const CArray<DATA> & li) { Clear(); PushB(li); return *this; } /*析构函数*/ virtual ~CArray() { delete [] m_pData; } public: void Clear() { m_iSize = 0; } DATA & operator[](int iIndex) { TEST_ASSERT(iIndex >= 0); bool bNeedResize = false; while ((iIndex + 1) > m_iCapability) { m_iCapability = m_iCapability * 2; bNeedResize = true; } if(bNeedResize) { CNoInitData<DATA> * pNewData = new CNoInitData<DATA>[m_iCapability]; CopyFromAToB(m_pData, pNewData, m_iSize); delete []m_pData; m_pData = pNewData; } return m_pData[iIndex]; } const DATA & operator[](int iIndex) const { if(iIndex < 0 && iIndex >= m_iSize) { TEST_THROW("operator[]"); } return m_pData[iIndex]; } void Insert(int iIndex, const DATA & data) { bool bNeedResize = false; while (m_iSize + 1 > m_iCapability) { m_iCapability = m_iCapability * 2; bNeedResize = true; } if(bNeedResize) { CNoInitData<DATA> * pNewData = new CNoInitData<DATA>[m_iCapability]; CopyFromAToB(m_pData, pNewData, iIndex); CopyFromAToB(m_pData + iIndex, pNewData + iIndex + 1, m_iSize - iIndex); delete []m_pData; m_pData = pNewData; } else { CopyFromAToB(m_pData + iIndex, m_pData + iIndex + 1, m_iSize - iIndex); } m_pData[iIndex] = CNoInitData<DATA>(data); ++m_iSize; } void Del(unsigned int uiIndex) { CopyFromAToB(m_pData + uiIndex + 1, m_pData + uiIndex, m_iSize - (uiIndex + 1)); --m_iSize; } void PushF(const DATA & data) { Insert(0, data); } void PushB(const DATA & data) { Insert(m_iSize, data); } void PushB(const CArray<DATA> & li) { bool bNeedResize = false; while ((m_iSize + li.m_iSize) > m_iCapability) { m_iCapability = m_iCapability * 2; bNeedResize = true; } if(bNeedResize) { CNoInitData<DATA> * pNewData = new CNoInitData<DATA>[m_iCapability]; CopyFromAToB(m_pData, pNewData, m_iSize); delete []m_pData; m_pData = pNewData; } CopyFromAToB(li.m_pData, m_pData + m_iSize, li.m_iSize); m_iSize += li.m_iSize; } int Size()const { return m_iSize; } CStr toStr() const { CStr strRet; bool bFirst = true; for(int i = 0; i < m_iSize; ++i) { if(bFirst) { bFirst = false; } else { strRet = strRet + "|"; } strRet = strRet + ToStr((DATA&)m_pData[i]); } return strRet; } protected: static void CopyFromAToB(const CNoInitData<DATA> * pA, CNoInitData<DATA> * pB, int n) { if(n <= 0) { // 不拷贝 return; } if(pA == pB) { // 确保地址不同 return; } if((pA < pB) && ((pA + n - 1) >= pB)) { // 确保不能把源地址的内容覆盖掉,从后面开始拷贝 for(int i = n - 1; i >= 0; --i) { pB[i] = pA[i]; } } else { // 从前面开始拷贝 for(int i = 0; i < n; ++i) { pB[i] = pA[i]; } } } protected: CNoInitData<DATA> *m_pData; int m_iSize; int m_iCapability; }; // 值对 template<typename D1, typename D2> class DD { public: DD(){} DD(const D1 & v1, const D2 & v2) : m_v1(v1), m_v2(v2){} D1 m_v1; D2 m_v2; }; template<typename LESSCOMPDATA> class CCompData { public: CCompData(const CCompData & data) : m_data(data.m_data){} CCompData(const LESSCOMPDATA & data) : m_data(data){} bool operator < (const CCompData & operand) const { return (m_data < operand.m_data); } bool operator == (const CCompData & operand) const { return (!(m_data < operand.m_data)) && (!(operand.m_data < m_data)); } bool operator > (const CCompData & operand) const { return (operand.m_data < m_data); } private: LESSCOMPDATA m_data; }; // 查找队列 template<typename KEY, typename DATA> class CSFArray { public: typedef DD<CCompData<KEY>, DATA> PairData; // 返回存在个数 unsigned int Exist(const KEY & key) const { int iRet = 0; for(int i = 0; i < m_data.Size(); ++i) { if(m_data[i].m_v1 == key) { ++iRet; } } return iRet; } // 找到第一个的位置(找不到返回-1) int FindFirst(const KEY & key) const { for(int i = 0; i < m_data.Size(); ++i) { if(m_data[i].m_v1 == key) { return i; } } return -1; } DATA & operator[](int iIndex) { return m_data[iIndex].m_v2; } const DATA & operator[](int iIndex) const { return m_data[iIndex].m_v2; } int Size()const { return m_data.Size(); } void Clear() { m_data.Clear(); } void Add(const KEY & key, const DATA & data) { int i = 0; for(; i < m_data.Size();) { if(m_data[i].m_v1 < key) { ++i; } else { break; } } /* else { for(; i < m_data.Size();) { if(m_data[i].m_v1 > key) { ++i; } else { break; } } } */ m_data.Insert(i, PairData(key, data)); } void Del(int iIndex) { m_data.Del(iIndex); } protected: void SortA() { if(m_data.Size() < 2) { return; } // 冒泡吧 bool bChanged = true; while (bChanged) { bChanged = false; for(int i = 0; i < (m_data.Size() - 1); ++i) { if(m_data[i].m_v1 > m_data[i + 1].m_v1) { // 交换数据 PairData temp = m_data[i]; m_data[i] = m_data[i + 1]; m_data[i + 1] = temp; bChanged = true; } } } } void SortD() { if(m_data.Size() < 2) { return; } // 冒泡吧 bool bChanged = true; while (bChanged) { bChanged = false; for(int i = 0; i < (m_data.Size() - 1); ++i) { if(m_data[i].m_v1 < m_data[i + 1].m_v1) { // 交换数据 PairData temp = m_data[i]; m_data[i] = m_data[i + 1]; m_data[i + 1] = temp; bChanged = true; } } } } CArray< DD < CCompData < KEY >, DATA > > m_data; }; template<typename DATA> class CSelfSFArray : public CSFArray<DATA, DATA> { public: void Add(const DATA & data) { CSFArray<DATA, DATA>::Add(data, data); } CStr toStr() const { CStr strRet; bool bFirst = true; for(int i = 0; i < m_data.Size(); ++i) { if(bFirst) { bFirst = false; } else { strRet = strRet + "|"; } strRet = strRet + ToStr(m_data[i].m_v2); } return strRet; } }; #endif