一个方便好用的数组类
<P>to Whanxy </P><P>int N;
N=StrToInt(Edit1->Text);
int RESULT;
哈哈,这样的代码很有意思哦.
在c++里没有现成的类似vb的redim那样可以自由定义数组边界的办法,很多人提供了一些数组类以实现你这样的需求,同样微软也提供了这样的模板类CArray ,你可以去参考这个摸板,不过 CArray 写的不够好只能给单线程的地方使用(后来个跟踪进去才发现是它的问题,没把偶气死)并且不能很好的支持使用 & 引用内部的成员(底层机制的问题),我从新改写了它的底层实现,写了一个摸板类,给你用正好满足你的需求.:
#if !defined(CYCArray_INCLUDED)
#define CYCArray_INCLUDED
#include "afxtempl.h"
/**
* 动态数组的模板类 (Write By yzhlinux ,you could use it any time any way if you could email me about it <a href="http://bbs.echot.net/mailtyzhlinux@hotmail.com" target="_blank" >yzhlinux@hotmail.com</A>)
* 1.支持字符索引
* 2.方便的添加删除修改任意一项
* 最后更新 2004-8-9
**1.优化了字符索引的运作方式,使用数组存储
**2.重写了底层数据的存储,将连续性的存储方式改为了非连续,
*** 从而很好有效地支持了“引用”,并且让数据的删除增加变的更为快速
/////
* 用法句举例1:
* YCArray<int,int> test1 ;
* test1.Add("Number2",4);
* test1.Add("Number1",2);
* printf("%d %d",test1["Number1"],test1["Number2"]);
* 显示:
* 2 4
/////
* 用法句举例2:
* YCArray<CString,CString> test2 ;
* test2.Add("string2","hahahaha");
* test2.Add("string1","yes yes yes yes");
* printf("%s %s %s",test2["string1"],test2["string2],test2);
* 显示:
* yes yes yes yes hahahaha yes yes yes yes
/////
* 用法句举例3:
* YCArray<CString,CString> test3 ;
* test3.Add("string2","hahahaha");
* test3.Add("string1","yes yes yes yes");
* test3.Add("","no no no");
* test3.AddR("string2","yes yes yes yes");//AddR方法会去搜索字符索引,如果存在则替换
* printf("%s %s %s",test3["string1"],test3["string2],test3);
* 显示:
* yes yes yes yes yes yes yes yes no no no
**/
/////////////////////////////////////////////////////////////////////////////
// YCArray<TYPE, ARG_TYPE>
#include <afxmt.h>
template<class TYPE, class ARG_TYPE>
class YCArray : public CObject
{
//friend YCArray<TYPE, ARG_TYPE>;
CCriticalSection YCArray_Add;
public:
// Attributes
int GetSize() const;
int GetUpperBound() const;
void SetSize(int nNewSize, int nGrowBy = -1);</P>
<P>// Operations
// Clean up
void FreeExtra();
void RemoveAll();</P>
<P>// Accessing elements
TYPE GetAt(int nIndex) const;
void SetAt(int nIndex, ARG_TYPE newElement);
TYPE& ElementAt(int nIndex);</P>
<P>TYPE GetAt(CString cIndex) const;
void SetAt(CString cIndex, ARG_TYPE newElement);
TYPE& ElementAt(CString cIndex);</P>
<P>// Direct Access to the element data (may return NULL)
const TYPE** GetData() const;
TYPE** GetData();</P>
<P>// Potentially growing the array
void SetAtGrow(int nIndex, ARG_TYPE newElement);
void SetAtGrow(CString cIndex, ARG_TYPE newElement);
int Add(ARG_TYPE newElement,CString cIndex = "");
int AddR(ARG_TYPE newElement,CString cIndex = "")//如果存在就替换
{
int nIndex = GetIndex(cIndex);
if(cIndex!=""&&nIndex >-1){
operator[](nIndex)=newElement;
}else{
nIndex = m_nSize;
SetAtGrow(nIndex, newElement);
SETIndex(cIndex,nIndex);
return nIndex;
}
}
int AddM(ARG_TYPE newElement,CString cIndex = "")
{
static int nIndex ;
YCArray_Add.Lock();
nIndex = m_nSize;
SetAtGrow(nIndex, newElement);
YCArray_Add.Unlock();
SETIndex(cIndex,nIndex);
return nIndex;
};
int Append(const YCArray& src);
void Copy(const YCArray& src);</P>
<P>// overloaded operator helpers
TYPE operator[](int nIndex) const;
TYPE& operator[](int nIndex);
TYPE operator[](CString cIndex) const;
TYPE& operator[](CString cIndex);
//YCArray<TYPE,ARG_TYPE> operator=(YCArray<TYPE,ARG_TYPE>& tparr)const;
YCArray<TYPE,ARG_TYPE>& operator=(YCArray<TYPE,ARG_TYPE>& tparr);
// Operations that move elements around
void InsertAt(int nIndex, ARG_TYPE newElement, int nCount = 1);
void RemoveAt(int nIndex, int nCount = 1);
void RemoveAt(CString cIndex,int nCount = 1);
void InsertAt(int nStartIndex, YCArray* pNewArray);
void InsertAt(ARG_TYPE newElement,int nIndex,CString cIndex);</P>
<P>int GetIndex(CString cIndex);
CString GetIndex(int nIndex);</P>
<P>CString GetSign();
int SetSign(CString& sign);</P>
<P>// Implementation
protected:
TYPE** Ym_pData; // the actual array of data
int m_nSize; // # of elements (upperBound - 1)
int m_nMaxSize;// max allocated
int m_nGrowBy; // grow amount
private:
int lock_sign;
CString Sign;
int MAXSIGNTIME;</P>
<P>CString* strIndex;
int strIndexNum;
BOOL SetIndexSize(int nIndex);
BOOL SETIndex(CString cIndex,int nIndex);
BOOL INSERTIndex(int nIndex,int nCount =1);
BOOL DELIndex(int nIndex,int nCount = 1);
BOOL DELIndex(CString cIndex,int nCount = 1);
void DestructAllElement(int nIndex,int nCount)
{//销毁对象,包括每个指针指向的对象
DELIndex(nIndex,nCount);
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
ASSERT(nIndex + nCount <= m_nSize);
if(nCount>0&&m_nSize>0){
for(int i =nIndex;i<nIndex+nCount;i++){
//Ym_pData->~TYPE(); // 由于ConstructAllElements 中是 Ym_pData = new TYPE;所以不需要Ym_pData->~TYPE()
delete Ym_pData;
}
}
};
void ConstructAllElements(int nIndex,int nCount)
{//创建对象,包括 new 出每个指针指向的对象
//nIndex = 0;
memset((void*)(Ym_pData+nIndex), 0, nCount * sizeof(TYPE*));
for (; nCount--; nIndex++)
Ym_pData = new TYPE;
};
public:
// Construction
YCArray();
YCArray(YCArray<TYPE, ARG_TYPE>& tp);
~YCArray();
void Serialize(CArchive&);
#ifdef _DEBUG
void Dump(CDumpContext&) const;
void AssertValid() const;
#endif</P>
<P>};</P>
[此贴子已经被作者于2005-1-10 14:04:39编辑过]
<P>//接上面</P><P>
template<class TYPE, class ARG_TYPE>
YCArray<TYPE, ARG_TYPE>::~YCArray()
{
ASSERT_VALID(this);</P><P> if (Ym_pData != NULL)
{
DestructAllElement(0,m_nSize);
//DestructElements<TYPE>(Ym_pData, m_nSize);
delete[] (BYTE*)Ym_pData;
}
}
template<class TYPE, class ARG_TYPE>
CString YCArray<TYPE, ARG_TYPE>::GetSign()
{
lock_sign=0;
return Sign;
}
template<class TYPE, class ARG_TYPE>
int YCArray<TYPE, ARG_TYPE>::SetSign(CString& sign)
{
int i=0;
while(lock_sign&&i<MAXSIGNTIME){
Sleep(1);
i++;
}
lock_sign=1;
Sign=sign;
return TRUE;
}
//用与把 nindex 后的索引往后推 nCount ,自动调整好buffer
template<class TYPE, class ARG_TYPE>
BOOL YCArray<TYPE, ARG_TYPE>::SetIndexSize(int nNewSize)
{
if(strIndexNum < nNewSize){
CString* tp = new CString;//新的buffer
for(int i=0;i<strIndexNum;i++){//把老索引复制过来
tp = strIndex;
}
for(i=strIndexNum;i<nNewSize;i++){
tp = "" ;
}
delete[] strIndex ;
strIndex = tp ;
strIndexNum= nNewSize ;
}else if(strIndexNum < nNewSize){
for(int i=nNewSize;i<strIndexNum;i++){
strIndex = "" ;
}
}
return TRUE;
}
template<class TYPE, class ARG_TYPE>
BOOL YCArray<TYPE, ARG_TYPE>::INSERTIndex(int nIndex,int nCount /*=1*/)
{
CString* tp = new CString;//新的buffer
for(int i=0;i<nIndex;i++){//把老索引复制过来
tp = strIndex;
}int j =0 ;
for(i=nIndex+nCount;i<m_nSize;i++){//把老索引复制过来
tp = strIndex;
j++;
}
delete[] strIndex ;
strIndex = tp ;
return TRUE;
}
template<class TYPE, class ARG_TYPE>
BOOL YCArray<TYPE, ARG_TYPE>::SETIndex(CString cIndex,int nIndex)
{//在 nIndex 后面添加一个 字符串索引
strIndex = cIndex ;
return TRUE;
}
template<class TYPE, class ARG_TYPE>
BOOL YCArray<TYPE, ARG_TYPE>::DELIndex(int nIndex,int nCount /*=1*/)
{//需要在 m_nSize 变化之前调用!!
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
ASSERT(nIndex + nCount <= m_nSize);
int j =0 ;
for(int i=nIndex+nCount;i<m_nSize;i++){//把老索引复制过来
strIndex = strIndex;
j++;
}
return TRUE;
}
template<class TYPE, class ARG_TYPE>
BOOL YCArray<TYPE, ARG_TYPE>::DELIndex(CString cIndex,int nCount /*=1*/)
{
int nIndex=this->GetIndex(cIndex);
return this->DELIndex(nIndex,nCount);
}
template<class TYPE, class ARG_TYPE>
int YCArray<TYPE, ARG_TYPE>::GetIndex(CString cIndex)
{//得到 cIndex 的数字索引
int nPos = -1;
for(int i=0;i<m_nSize;i++){
if(strIndex == cIndex){
nPos = i ;break;
}
}
return nPos;
}
template<class TYPE, class ARG_TYPE>
CString YCArray<TYPE, ARG_TYPE>::GetIndex(int nIndex)
{//返回 nIndex 的字符串索引
return strIndex;
}
/////////////////////////////////////////////////////////////////////////////
// YCArray<TYPE, ARG_TYPE> inline functions</P><P>template<class TYPE, class ARG_TYPE>
AFX_INLINE int YCArray<TYPE, ARG_TYPE>::GetSize() const
{ return m_nSize; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE int YCArray<TYPE, ARG_TYPE>::GetUpperBound() const
{ return m_nSize-1; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE void YCArray<TYPE, ARG_TYPE>::RemoveAll()
{ SetSize(0, -1); }
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE YCArray<TYPE, ARG_TYPE>::GetAt(int nIndex) const
{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
return *Ym_pData; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE void YCArray<TYPE, ARG_TYPE>::SetAt(int nIndex, ARG_TYPE newElement)
{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
*(Ym_pData) = newElement; }</P><P>template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE& YCArray<TYPE, ARG_TYPE>::ElementAt(int nIndex)
{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
return *Ym_pData; }</P><P>template<class TYPE, class ARG_TYPE>
TYPE YCArray<TYPE, ARG_TYPE>::GetAt(CString cIndex) const
{
int nIndex=GetIndex(cIndex);
return GetAt(nIndex);
}
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::SetAt(CString cIndex, ARG_TYPE newElement)
{
int nIndex=GetIndex(cIndex);
return SetAt(nIndex, newElement);
}
template<class TYPE, class ARG_TYPE>
TYPE& YCArray<TYPE, ARG_TYPE>::ElementAt(CString cIndex)
{
int nIndex=GetIndex(cIndex);
return ElementAt(nIndex);
}
template<class TYPE, class ARG_TYPE>
AFX_INLINE const TYPE** YCArray<TYPE, ARG_TYPE>::GetData() const
{ return (const TYPE**)Ym_pData; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE** YCArray<TYPE, ARG_TYPE>::GetData()
{ return (TYPE**)Ym_pData; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE int YCArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement,CString cIndex /* ="" */)
{ int nIndex = m_nSize;
SetAtGrow(nIndex, newElement);
SETIndex(cIndex,nIndex);
return nIndex; }
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE YCArray<TYPE, ARG_TYPE>::operator[](int nIndex) const
{ return GetAt(nIndex); }
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE& YCArray<TYPE, ARG_TYPE>::operator[](int nIndex)
{ return ElementAt(nIndex); }
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE YCArray<TYPE, ARG_TYPE>::operator[](CString cIndex) const
{
int nIndex=GetIndex(cIndex);</P><P> return operator[](nIndex);
}
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE& YCArray<TYPE, ARG_TYPE>::operator[](CString cIndex)
{
int nIndex=GetIndex(cIndex);
return operator[](nIndex);
}
/*
template<class TYPE, class ARG_TYPE>
AFX_INLINE YCArray<TYPE,ARG_TYPE> YCArray<TYPE, ARG_TYPE>::operator=(YCArray<TYPE,ARG_TYPE>& tparr) const
{
int i,j;
for(i=0;i<tparr.GetSize();i++){
j = GetIndex(tparr.GetIndex(i));
if(j>-1){
operator[](tparr.GetIndex(i)) = tparr;
}else{
Add(tparr,tparr.GetIndex(i));
}
}
return this;
}
*/
template<class TYPE, class ARG_TYPE>
AFX_INLINE YCArray<TYPE,ARG_TYPE>& YCArray<TYPE, ARG_TYPE>::operator=(YCArray<TYPE,ARG_TYPE>& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself</P><P> SetSize(src.m_nSize);
for(int i=0;i<m_nSize;i++){
/*将此句修改为内存拷贝*///
*Ym_pData = *src.Ym_pData ;
//memcpy(Ym_pData,src.Ym_pData,sizeof(TYPE));
SETIndex(src.GetIndex(i),i);
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////
// YCArray<TYPE, ARG_TYPE> out-of-line functions</P><P>template<class TYPE, class ARG_TYPE>
YCArray<TYPE, ARG_TYPE>::YCArray()
{
Ym_pData = NULL;
strIndexNum = m_nSize = m_nMaxSize = m_nGrowBy = 0;
strIndex=NULL;MAXSIGNTIME=10;
}
template<class TYPE, class ARG_TYPE>
YCArray<TYPE, ARG_TYPE>::YCArray(YCArray<TYPE, ARG_TYPE>& tp)
{
Ym_pData = NULL;
strIndexNum = m_nSize = m_nMaxSize = m_nGrowBy = 0;
strIndex=NULL;MAXSIGNTIME=10;
operator=(tp);
}</P><P>template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::SetSize(int nNewSize, int nGrowBy)
{
ASSERT_VALID(this);
ASSERT(nNewSize >= 0);</P><P> if (nGrowBy != -1)
m_nGrowBy = nGrowBy;// set new size</P><P> if (nNewSize == 0){
// shrink to nothing
if (Ym_pData != NULL){
DestructAllElement(0,m_nSize);
//DestructElements<TYPE>(Ym_pData, m_nSize);
delete[] (BYTE*)Ym_pData;
Ym_pData = NULL;
}
m_nSize = m_nMaxSize = 0;
} else if (Ym_pData == NULL){
// create one with exact size
#ifdef SIZE_T_MAX
ASSERT(nNewSize <= SIZE_T_MAX/sizeof(TYPE*)); // no overflow
#endif
Ym_pData = (TYPE**) new BYTE;
ConstructAllElements(0,nNewSize);//ConstructElements<TYPE>(Ym_pData, nNewSize);
m_nSize = m_nMaxSize = nNewSize;
} else if (nNewSize <= m_nMaxSize){
// it fits
if (nNewSize > m_nSize)
{
// initialize the new elements
ConstructAllElements(m_nSize,nNewSize-m_nSize);//ConstructElements<TYPE>(&Ym_pData, nNewSize-m_nSize);
}
else if (m_nSize > nNewSize)
{
// destroy the old elements
DestructAllElement(nNewSize,m_nSize-nNewSize);
//DestructElements<TYPE>(&Ym_pData, m_nSize-nNewSize);
}
m_nSize = nNewSize;
}
else
{
// otherwise, grow array
int nGrowBy = m_nGrowBy;
if (nGrowBy == 0)
{
// heuristically determine growth when nGrowBy == 0
//(this avoids heap fragmentation in many situations)
nGrowBy = m_nSize / 8;
nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy);
}
int nNewMax;
if (nNewSize < m_nMaxSize + nGrowBy)
nNewMax = m_nMaxSize + nGrowBy;// granularity
else
nNewMax = nNewSize;// no slush</P><P>ASSERT(nNewMax >= m_nMaxSize);// no wrap around
#ifdef SIZE_T_MAX
ASSERT(nNewMax <= SIZE_T_MAX/sizeof(TYPE)); // no overflow
#endif
TYPE** pNewData = (TYPE**) new BYTE;//TYPE* pNewData = (TYPE*) new BYTE;</P><P>// copy new data from old
memcpy(pNewData, Ym_pData, m_nSize * sizeof(TYPE*));</P><P>// construct remaining elements
ASSERT(nNewSize > m_nSize);//throw("/*wait for me --- yzhlinux*/");
delete[] (BYTE*)Ym_pData;
Ym_pData = pNewData;
ConstructAllElements(m_nSize,nNewSize-m_nSize);//ConstructElements<TYPE>(&pNewData, nNewSize-m_nSize);</P><P>// get rid of old stuff (note: no destructors called)
m_nSize = nNewSize;
m_nMaxSize = nNewMax;
}
SetIndexSize(nNewSize);
}
</P> <P>//接上面</P><P>
template<class TYPE, class ARG_TYPE>
int YCArray<TYPE, ARG_TYPE>::Append(const YCArray& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself</P><P> int nOldSize = m_nSize;
SetSize(m_nSize + src.m_nSize);</P><P> ConstructAllElements(nOldSize,src.m_nSize);
for(int i=nOldSize;i<m_nSize;i++){
/*将此句修改为内存拷贝*///
*Ym_pData = *src.Ym_pData ;
//memcpy(Ym_pData,src.Ym_pData,sizeof(TYPE));
SETIndex(src.GetIndex(i-nOldSize),i);
}
/*wait for me*///CopyElements<TYPE>(Ym_pData + nOldSize, src.Ym_pData, src.m_nSize);
return nOldSize;
}</P><P>template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::Copy(const YCArray& src)
{
ASSERT_VALID(this);
ASSERT(this != &src); // cannot append to itself</P><P> SetSize(src.m_nSize);
for(int i=0;i<m_nSize;i++){
/*将此句修改为内存拷贝*///*Ym_pData = *src.Ym_pData ;
memcpy(Ym_pData,src.Ym_pData,sizeof(TYPE));
SETIndex(src.GetIndex(i),i);
}
/*wait for me*///CopyElements<TYPE>(Ym_pData, src.Ym_pData, src.m_nSize);
}</P><P>template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::FreeExtra()
{
ASSERT_VALID(this);</P><P> if (m_nSize != m_nMaxSize)
{
// shrink to desired size
#ifdef SIZE_T_MAX
ASSERT(m_nSize <= SIZE_T_MAX/sizeof(TYPE)); // no overflow
#endif
TYPE* pNewData = NULL;
if (m_nSize != 0)
{
pNewData = (TYPE**) new BYTE;
// copy new data from old
memcpy(pNewData, Ym_pData, m_nSize * sizeof(TYPE*));
}</P><P>// get rid of old stuff (note: no destructors called)
delete[] (BYTE*)Ym_pData;
Ym_pData = pNewData;
m_nMaxSize = m_nSize;
}
}
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::SetAtGrow(CString cIndex, ARG_TYPE newElement)
{
int nIndex=GetIndex(cIndex);
return SetAtGrow(nIndex,newElement);
}
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);</P><P> if (nIndex >= m_nSize)
SetSize(nIndex+1, -1);
*Ym_pData = newElement;
}</P><P>template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount /*=1*/)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0); // will expand to meet need
ASSERT(nCount > 0); // zero or negative size not allowed</P><P> if (nIndex >= m_nSize)
{
// adding after the end of the array
SetSize(nIndex + nCount, -1); // grow so nIndex is valid
}
else
{
// inserting in the middle of the array
int nOldSize = m_nSize;
SetSize(m_nSize + nCount, -1);// grow it to new size
// destroy intial data before copying over it
/*不需要销毁了,因为 SetSize 的是指针*///DestructAllElement(nOldSize,nCount);
//DestructElements<TYPE>(&Ym_pData, nCount);
// shift old data up to fill gap
memmove(&Ym_pData, &Ym_pData,
(nOldSize-nIndex) * sizeof(TYPE*));</P><P>// re-init slots we copied from
/*不需要销毁了,因为 SetSize 的是指针*///ConstructAllElements(nIndex,nCount);//ConstructElements<TYPE>(&Ym_pData, nCount);
}</P><P> // insert new value in the gap
ASSERT(nIndex + nCount <= m_nSize);
while (nCount--){
*Ym_pData = newElement;
}
}
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::RemoveAt(CString cIndex,int nCount /*=1*/)
{
int nIndex = GetIndex(cIndex);
RemoveAt(nIndex,nCount);
}
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::RemoveAt(int nIndex, int nCount /*=1*//*=1*/)
{
ASSERT_VALID(this);
ASSERT(nIndex >= 0);
ASSERT(nCount >= 0);
ASSERT(nIndex + nCount <= m_nSize);
//yzh
DELIndex(nIndex);
//yzh
// just remove a range
int nMoveCount = m_nSize - (nIndex + nCount); //需要移动的数目
DestructAllElement(nIndex,nCount);
//DestructElements<TYPE>(&Ym_pData, nCount);
if (nMoveCount)
memmove(&Ym_pData, &Ym_pData,
nMoveCount * sizeof(TYPE*));
m_nSize -= nCount;
}</P><P>template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::InsertAt(int nStartIndex, YCArray* pNewArray)
{
ASSERT_VALID(this);
ASSERT(pNewArray != NULL);
ASSERT_VALID(pNewArray);
ASSERT(nStartIndex >= 0);</P><P> if (pNewArray->GetSize() > 0)
{
InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
for (int i = 0; i < pNewArray->GetSize(); i++)
SetAt(nStartIndex + i, pNewArray->GetAt(i));
}
}
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::InsertAt(ARG_TYPE newElement, int nIndex,CString cIndex)
{
ADDIndex(cIndex,nIndex+1);
InsertAt(newElement,nIndex);
}
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::Serialize(CArchive& ar)
{
ASSERT_VALID(this);
throw("/*wait for me --- yzhlinux*/");
CObject::Serialize(ar);
if (ar.IsStoring())
{
ar.WriteCount(m_nSize);
}
else
{
DWORD nOldSize = ar.ReadCount();
SetSize(nOldSize, -1);
}
//SerializeElements<TYPE>(ar, Ym_pData, m_nSize);
}</P><P>#ifdef _DEBUG
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::AssertValid() const
{
CObject::AssertValid();</P><P> if (Ym_pData == NULL)
{
ASSERT(m_nSize == 0);
ASSERT(m_nMaxSize == 0);
}
else
{
ASSERT(m_nSize >= 0);
ASSERT(m_nMaxSize >= 0);
ASSERT(m_nSize <= m_nMaxSize);
ASSERT(AfxIsValidAddress(Ym_pData, m_nMaxSize * sizeof(TYPE*)));
}
}
template<class TYPE, class ARG_TYPE>
void YCArray<TYPE, ARG_TYPE>::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
throw("/*wait for me --- yzhlinux*/");
dc << "with " << m_nSize << " elements";
if (dc.GetDepth() > 0)
{
dc << "\n";
/*wait for me --- yzhlinux*///DumpElements<TYPE>(dc, Ym_pData, m_nSize);
}</P><P> dc << "\n";
}
#endif </P><P>#endif </P><P>//完</P> 太长了点,叶师兄应该打包上传的。 <DIV class=quote><B>以下是引用<I>Eagle</I>在2005-1-10 14:44:00的发言:</B>
太长了点,叶师兄应该打包上传的。</DIV>
就是因为发不了文件啊,奇怪,现在不能上传文件了,不然不用那么辛苦
先传到帖图区…… 赚帖子不好吗 现在可以传文件了,附上,好用的摸板数组类,支持 ATL 可以在任何地方使用。
std::map 模板的作用好像就是这样的……
页:
[1]