#pragma once template<typename TYPE, typename ARG_TYPE=const TYPE&> class CArray { TYPE *m_pData; int m_nCount; int m_nSize; public: TYPE & GetAt(int nIndex) { if (nIndex >= 0 && nIndex < m_nSize) return m_pData[nIndex]; } ARG_TYPE GetAt(int nIndex) const { if (nIndex >= 0 && nIndex < m_nSize) return m_pData[nIndex]; } int GetSize() const { return m_nSize; } int GetCount() const { return m_nCount; } void RemoveAll() { m_nSize = 0; } bool IsEmpty() const { return m_nSize == 0; } TYPE & operator[](int nIndex) { return GetAt(nIndex); } ARG_TYPE operator[](int nIndex) const { return GetAt(nIndex); } const TYPE* GetData() const { return (const TYPE*)m_pData; } TYPE* GetData() { return m_pData; } void SetAt(int nIndex, ARG_TYPE newElement) { if (nIndex >= 0 && nIndex < m_nSize) m_pData[nIndex] = newElement; } public: CArray(int nCount = 4); ~CArray(); int Add(ARG_TYPE newElement); void Remalloc(int nCount); void freeExtra(); void SetSize(int nNewSize); void RemoveAt(int nIndex, int nCount = 1); void InsertAt(int nIndex, ARG_TYPE newElement, int nCount = 1); }; template<typename TYPE, typename ARG_TYPE = const TYPE&> CArray<TYPE,ARG_TYPE>::CArray(int nCount) { m_nSize = 0; m_nCount = nCount; m_pData = new TYPE[nCount]; } template<typename TYPE, typename ARG_TYPE = const TYPE&> CArray<TYPE, ARG_TYPE>::~CArray() { delete[]m_pData; } template<typename TYPE, typename ARG_TYPE = const TYPE&> int CArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement) { if (m_nSize >= m_nCount) Remalloc(m_nCount * 2); m_pData[m_nSize++] = newElement; return (m_nSize - 1); } template<typename TYPE, typename ARG_TYPE = const TYPE&> void CArray<TYPE, ARG_TYPE>::Remalloc(int nCount) { TYPE *p = new TYPE[nCount * 2]; memcpy(p, m_pData, sizeof(TYPE)*m_nSize); delete[]m_pData; m_pData = p; m_nCount = nCount * 2; } template<typename TYPE, typename ARG_TYPE = const TYPE&> void CArray<TYPE, ARG_TYPE>::freeExtra() { if (m_nSize != m_nCount) { TYPE* pNewData = NULL; if (m_nSize != 0) { pNewData = new TYPE[m_nSize]; memcpy_s(pNewData, m_nSize * sizeof(TYPE), m_pData, m_nSize * sizeof(TYPE)); } delete[]m_pData; m_pData = pNewData; m_nCount = m_nSize; } } template<typename TYPE, typename ARG_TYPE = const TYPE&> void CArray<TYPE, ARG_TYPE>::SetSize(int nNewSize) { if (nNewSize > m_nCount) Remalloc(nNewSize); m_nSize = nNewSize; } template<typename TYPE, typename ARG_TYPE = const TYPE&> void CArray<TYPE, ARG_TYPE>::RemoveAt(int nIndex, int nCount) { if (nIndex < 0 && nIndex >= m_nSize) return; if (nCount + nIndex >= m_nSize) { m_nSize = nIndex; return; } memmove(m_pData + nIndex, m_pData + nIndex + nCount, sizeof(TYPE)*(m_nSize - nCount - nIndex)); m_nSize -= nCount; } template<typename TYPE, typename ARG_TYPE = const TYPE&> void CArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount) { if (nIndex < 0 || nCount < 0) return; if (nIndex > m_nSize) { if (nIndex + nCount) Remalloc(nIndex + nCount); m_nSize = nIndex + nCount; } else { if (nCount + m_nSize > m_nCount) Remalloc(nCount + m_nSize); if (nIndex < m_nSize) // 如果在数组中间区域插入数据,需要移动把数据后移 memmove(m_pData + nCount + nIndex, m_pData + nIndex, sizeof(TYPE)*(m_nSize - nIndex)); m_nSize += nCount; } int i = 0; while (i < nCount) m_pData[nIndex + i++] = newElement; }