CStringArray Class

Supports arrays of CString objects.


class CStringArray : public CObject


The member functions of CStringArray are similar to the member functions of class CObArray. Because of this similarity, you can use the CObArray reference documentation for member function specifics. Wherever you see a CObject pointer as a return value, substitute a CString object (not a CString pointer). Wherever you see a CObject pointer as a function parameter, substitute a LPCTSTR.

CObject* CObArray::GetAt( int <nIndex> ) const;

for example, translates to

CString CStringArray::GetAt( int <nIndex> ) const;


void SetAt( int <nIndex>, CObject* <newElement> )

translates to

void SetAt( int <nIndex>, LPCTSTR <newElement> )

Public Constructors

Name Description
CStringArray::CStringArray Constructs an empty array.

Public Methods

Name Description
CStringArray::Add Adds an element to the end of the array; grows the array if necessary.
CStringArray::Append Appends another array to the array; grows the array if necessary.
CStringArray::Copy Copies another array to the array; grows the array if necessary.
CStringArray::ElementAt Returns a temporary reference to the element pointer within the array.
CStringArray::FreeExtra Frees all unused memory above the current upper bound.
CStringArray::GetAt Returns the value at a given index.
CStringArray::GetCount Gets the number of elements in this array.
CStringArray::GetData Allows access to elements in the array. Can be NULL.
CStringArray::GetSize Gets the number of elements in this array.
CStringArray::GetUpperBound Returns the largest valid index.
CStringArray::InsertAt Inserts an element (or all the elements in another array) at a specified index.
CStringArray::IsEmpty Determines if the array is empty.
CStringArray::RemoveAll Removes all the elements from this array.
CStringArray::RemoveAt Removes an element at a specific index.
CStringArray::SetAt Sets the value for a given index; array not allowed to grow.
CStringArray::SetAtGrow Sets the value for a given index; grows the array if necessary.
CStringArray::SetSize Sets the number of elements to be contained in this array.

Public Operators

Name Description
CStringArray::operator [ ] Sets or gets the element at the specified index.


CStringArray incorporates the IMPLEMENT_SERIAL macro to support serialization and dumping of its elements. If an array of CString objects is stored to an archive, either with an overloaded insertion operator or with the Serialize member function, each element is serialized in turn.


Before using an array, use SetSize to establish its size and allocate memory for it. If you do not use SetSize, adding elements to your array causes it to be frequently reallocated and copied. Frequent reallocation and copying are inefficient and can fragment memory.

If you need a dump of individual string elements in the array, you must set the depth of the dump context to 1 or greater.

When a CString array is deleted, or when its elements are removed, string memory is freed as appropriate.

For more information on using CStringArray, see the article Collections.

Inheritance Hierarchy




Header: afxcoll.h

See also

CObject Class
Hierarchy Chart