zoukankan      html  css  js  c++  java
  • CArray类模板封装示例

    #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;
    }
    

      

  • 相关阅读:
    从美国单身男女的分布图说起纵论视觉化叙述
    职场笔记[0702]:用户体验和创业
    写《回国驯火记》的那个安普若
    《成长比成功更重要》的得失之间[Updated]
    什么时候你可以创业?
    技术英雄会【二】:问CSDN一个信息过滤器的问题
    技术英雄会【四】:也谈如何发掘到需要的内容和英雄
    技术英雄会【新闻】新浪/网易新闻:CSDN最有价值博客TOP10颁奖【图】
    技术英雄会【一】:问周鸿祎一个问题
    自然语言处理能够把全网内容组织到什么程度?
  • 原文地址:https://www.cnblogs.com/veis/p/12514391.html
Copyright © 2011-2022 走看看