vc++课程设计,求高手帮忙~
数组的类模板设计之:实现功能GetSize()设置数组大小、RemoveAll()清除数组所有元素...
数组的类模板设计之:
实现功能GetSize ()设置数组大小 、RemoveAll()清除数组所有元素 展开
实现功能GetSize ()设置数组大小 、RemoveAll()清除数组所有元素 展开
2个回答
展开全部
#include <iostream>
using std::cout;
using std::endl;
template<class T>
class CTest
{
public:
CTest(int iCapacity = 1);
~CTest();
int Size() const; //当前元素个数
void PushBack(T t);
void Print() const;
int Capacity() const; //当前容量大小
void SetSize(int iCapacity); //设置容量大小
void RemoveAll(); //清空所有元素
private:
T *m_pData;
int m_iSize;
int m_iCapacity;
};
template<class T>
CTest<T>::CTest(int iCapacity)
{
m_pData = new T[iCapacity];
m_iCapacity = iCapacity;
m_iSize = 0;
}
template<class T>
CTest<T>::~CTest()
{
delete []m_pData;
m_pData = NULL;
m_iSize = m_iCapacity = 0;
}
template<class T>
CTest<T>::Capacity() const
{
return m_iCapacity;
}
template<class T>
CTest<T>::Size() const
{
return m_iSize;
}
template<class T>
void CTest<T>::Print() const
{
for(int i=0; i<m_iSize; i++)
{
cout <<m_pData[i] <<" ";
}
cout <<endl;
}
template<class T>
void CTest<T>::SetSize(int iCapacity)
{
if(iCapacity <= m_iCapacity)
{
return ;
}
int i = 0;
//申请空间保存原来数据
T *pTemp = new T[m_iCapacity];
for(i=0; i<m_iCapacity; i++)
{
pTemp[i] = m_pData[i];
}
//将原来空间释放再申请iCapacity空间
delete []m_pData;
m_pData = new T[iCapacity];
for(i=0; i<m_iCapacity; i++)
{
m_pData[i] = pTemp[i];
}
m_iCapacity = iCapacity;
delete []pTemp;
}
template<class T>
void CTest<T>::RemoveAll()
{
//这个功能应该是清空当前数组中的所有元素,但空间还是保留着的。
m_iSize = 0;
}
template<class T>
void CTest<T>::PushBack(T t)
{
int i = 0;
if(m_iSize == m_iCapacity)
{
//申请空间保存原来数据
T *pTemp = new T[m_iCapacity];
for(i=0; i<m_iCapacity; i++)
{
pTemp[i] = m_pData[i];
}
//将原来空间释放再申请两倍空间
delete []m_pData;
m_pData = new T[m_iCapacity*2];
for(i=0; i<m_iCapacity; i++)
{
m_pData[i] = pTemp[i];
}
m_iCapacity *= 2;
delete []pTemp;
}
//存放到后面
m_pData[m_iSize++] = t;
}
int main(int argc, char* argv[])
{
CTest<int> test;
int i = 0;
for(i=0; i<10; i++)
{
test.PushBack(i);
}
cout<< test.Capacity() <<" " <<test.Size()<<endl;
test.Print();
cout <<"--------------------------\nSetSize:\n";
test.SetSize(20);
cout<< test.Capacity() <<" " <<test.Size()<<endl;
test.Print();
cout <<"--------------------------\nRemoveAll:\n";
test.RemoveAll();
cout<< test.Capacity() <<" " <<test.Size()<<endl;
test.Print();
cout <<"--------------------------\n";
for(i=10; i>0; i--)
{
test.PushBack(i);
}
cout<< test.Capacity() <<" " <<test.Size()<<endl;
test.Print();
return 0;
}
using std::cout;
using std::endl;
template<class T>
class CTest
{
public:
CTest(int iCapacity = 1);
~CTest();
int Size() const; //当前元素个数
void PushBack(T t);
void Print() const;
int Capacity() const; //当前容量大小
void SetSize(int iCapacity); //设置容量大小
void RemoveAll(); //清空所有元素
private:
T *m_pData;
int m_iSize;
int m_iCapacity;
};
template<class T>
CTest<T>::CTest(int iCapacity)
{
m_pData = new T[iCapacity];
m_iCapacity = iCapacity;
m_iSize = 0;
}
template<class T>
CTest<T>::~CTest()
{
delete []m_pData;
m_pData = NULL;
m_iSize = m_iCapacity = 0;
}
template<class T>
CTest<T>::Capacity() const
{
return m_iCapacity;
}
template<class T>
CTest<T>::Size() const
{
return m_iSize;
}
template<class T>
void CTest<T>::Print() const
{
for(int i=0; i<m_iSize; i++)
{
cout <<m_pData[i] <<" ";
}
cout <<endl;
}
template<class T>
void CTest<T>::SetSize(int iCapacity)
{
if(iCapacity <= m_iCapacity)
{
return ;
}
int i = 0;
//申请空间保存原来数据
T *pTemp = new T[m_iCapacity];
for(i=0; i<m_iCapacity; i++)
{
pTemp[i] = m_pData[i];
}
//将原来空间释放再申请iCapacity空间
delete []m_pData;
m_pData = new T[iCapacity];
for(i=0; i<m_iCapacity; i++)
{
m_pData[i] = pTemp[i];
}
m_iCapacity = iCapacity;
delete []pTemp;
}
template<class T>
void CTest<T>::RemoveAll()
{
//这个功能应该是清空当前数组中的所有元素,但空间还是保留着的。
m_iSize = 0;
}
template<class T>
void CTest<T>::PushBack(T t)
{
int i = 0;
if(m_iSize == m_iCapacity)
{
//申请空间保存原来数据
T *pTemp = new T[m_iCapacity];
for(i=0; i<m_iCapacity; i++)
{
pTemp[i] = m_pData[i];
}
//将原来空间释放再申请两倍空间
delete []m_pData;
m_pData = new T[m_iCapacity*2];
for(i=0; i<m_iCapacity; i++)
{
m_pData[i] = pTemp[i];
}
m_iCapacity *= 2;
delete []pTemp;
}
//存放到后面
m_pData[m_iSize++] = t;
}
int main(int argc, char* argv[])
{
CTest<int> test;
int i = 0;
for(i=0; i<10; i++)
{
test.PushBack(i);
}
cout<< test.Capacity() <<" " <<test.Size()<<endl;
test.Print();
cout <<"--------------------------\nSetSize:\n";
test.SetSize(20);
cout<< test.Capacity() <<" " <<test.Size()<<endl;
test.Print();
cout <<"--------------------------\nRemoveAll:\n";
test.RemoveAll();
cout<< test.Capacity() <<" " <<test.Size()<<endl;
test.Print();
cout <<"--------------------------\n";
for(i=10; i>0; i--)
{
test.PushBack(i);
}
cout<< test.Capacity() <<" " <<test.Size()<<endl;
test.Print();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询