Saturday, March 10, 2007

MFC collection classes

Three MFC collection classes are
1.CArray
2.CList
3.CMap


CArray usage...
The first CArray template parameter specifies the type of data stored in the array; the second specifies how the type is represented in parameter lists. You could use CPoints instead of CPoint references, but references are more efficient when the size of the item exceeds the size of a pointer.

class CPoint3D
{
public:
CPoint3D ()
{
x = y = z = 0;
}
CPoint3D (int xPos, int yPos, int zPos)
{
x = xPos;
y = yPos;
z = zPos;
}
int x, y, z;
};


void Test()
{

CArray array;

// Populate the array, growing it as needed.
for (int i=0; i<10; i++)
array.SetAtGrow (i, CPoint3D (i*10, 0, 0));

// Enumerate the items in the array.
int nCount = array.GetSize ();

for (i=0; i {
CPoint3D point = array[i];
TRACE (_T ("x=%d, y=%d, z=%d\n"), point.x, point.y, point.z);
}

}

typedef CArray CUIntArray;

No comments: