Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



How to append and insert elements

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.


Appending an element

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

[Top]


Inserting an element

The following code fragment uses InsertL() to insert a new element into a definite position in aCArrayFixFlat<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.

[Top]


Adding and inserting a pointer to an array of pointers

The following code fragment uses the AppendL() andInsertL() functions to append and insert a pointer into aCArrayPtrFlat array. The pointer being added is a pointer to aCBase 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);