Elements can be added into an array or added to the end by specifying the insertion position.
Elements can be added into an array by inserting them into a specified position.
Adding elements into an array may cause memory to be allocated and this can fail, causing these functions to leave. User code must always be prepared to handle this.
The following code fragment appends three TElement objects to a CArrayFixFlat<class T> constructed with a granularity of four, each successive element containing the characters “X0”, “X1” etc.
_LIT(KFormatTxt,"X%u");
class TElement { public : TElement(); public : TBuf<4> iData; };
CArrayFixFlat<TElement>* fixflat; fixflat = new (ELeave) CArrayFixFlat<TElement>(3);
TElement theElement;
for (TInt value = 0; value < 3; value++) { theElement.iData.Format(KFormatTxt,value); fixflat->AppendL(theElement); }
Notes
Adding the first element into this array causes an array buffer with a capacity of 3 elements to be constructed. Attempting to add a 4th element causes the array buffer to be re-allocated so that it has a capacity of 6 elements.
In general, the granularity should be carefully chosen to minimise the number of calls to the memory allocator and to avoid wasted space within the array buffer.
The following code fragment uses InsertL() to insert a new element into a definite position in a CArrayFixFlat<class T> array. For example, to insert a TElement object at the beginning of the array (position zero in the array) :
CArrayFixFlat<TElement>* fixflat; fixflat = new (ELeave) CArrayFixFlat<TElement>(3);
TElement theElement;
_LIT(KTxtBeg,"BEG"); theElement.iData = KTxtBeg; fixflat->InsertL(0,theElement);
To insert a new element at (what is now) position 2 of the array:
_LIT(KTxtMid,"MID"); theElement.iData = KTxtMid; fixflat->InsertL(2,theElement);
To insert a new element at the back of the array:
_LIT(KTxtEnd,"END"); theElement.iData = KTxtEnd; fixflat->InsertL(fixflat->Count(),theElement);
Elements can also be inserted into the array at a position determined by a key (i.e. inserted in key sequence) using the InsertIsqL() member function.
The following code fragment uses the AppendL() and InsertL() functions to append and insert a pointer into a CArrayPtrFlat array. The pointer being added is a pointer to a CBase derived objects:
CArrayPtrFlat<TElement>* ptrflat; ptrflat = new (ELeave) CArrayPtrFlat<TElement>(3);
ptr = new (ELeave) CElement; ... ptrflat->AppendL(ptr); ... ptr = new (ELeave) CElement; ... ptrflat->InsertL(0,ptr);
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.