Contents Up Previous Next

wxArrayString

wxArrayString is an efficient container for storing wxString objects. It has the same features as all wxArray classes, i.e. it dynamically expands when new items are added to it (so it is as easy to use as a linked list), but the access time to the elements is constant, instead of being linear in number of elements as in the case of linked lists. It is also very size efficient and doesn't take more space than a C array wxString[] type. wxArrayString uses its knowledge of internals of wxString class to achieve this.

This class is used in the same way as other dynamic arrays, except that no WX_DEFINE_ARRAY declaration is needed for it. When a string is added or inserted in the array, a copy of the string is created, so the original string may be safely deleted (e.g. if it was a char * pointer the memory it was using can be freed immediately after this). In general, there is no need to worry about string memory deallocation when using this class - it will always free the memory it uses itself.

The references returned by Item, Last or operator[] are not constant, so the array elements may be modified in place like this

    array.Last().MakeUpper();
Finally, none of the methods of this class is virtual including its destructor, so this class should not be derived from.

Derived from

Although this is not true strictly speaking, this class may be considered as a specialization of wxArray class for the wxString member data: it is not implemented like this, but it does have all of the wxArray functions.

Include files

<wx/string.h>

See also

wxArray, wxString, wxString overview

Members

wxArrayString::wxArrayString
wxArrayString::~wxArrayString
wxArrayString::operator=
wxArrayString::operator[]
wxArrayString::Add
wxArrayString::Alloc
wxArrayString::Clear
wxArrayString::Count
wxArrayString::Empty
wxArrayString::GetCount
wxArrayString::Index
wxArrayString::Insert
wxArrayString::IsEmpty
wxArrayString::Item
wxArrayString::Last
wxArrayString::Remove (by value)
wxArrayString::Remove (by index)
wxArrayString::Shrink
wxArrayString::Sort (alphabetically)
wxArrayString::Sort (user defined)


wxArrayString::wxArrayString

wxArrayString()

wxArrayString(const wxArrayString& array)

Default and copy constructors.


wxArrayString::~wxArrayString

~wxArrayString()

Destructor frees memory occupied by the array strings. For the performance reasons it is not virtual, so this class should not be derived from.


wxArrayString::operator=

wxArrayString & operator =(const wxArrayString& array)

Assignment operator.


wxArrayString::operator[]

wxString& operatorp[](size_t nIndex)

Return the array element at position nIndex. An assert failure will result from an attempt to access an element beyond the end of array in debug mode, but no check is done in release mode.

This is the operator version of Item method.


wxArrayString::Add

void Add(const wxString& str)

Appends a new item to the array.

See also: Insert


wxArrayString::Alloc

void Alloc(size_t nCount)

Preallocates enough memory to store nCount items. This function may be used to improve array class performance before adding a known number of items consecutively.

See also: Dynamic array memory management


wxArrayString::Clear

void Clear()

Clears the array contents and frees memory.

See also: Empty


wxArrayString::Count

size_t Count() const

Returns the number of items in the array. This function is deprecated and is for backwards compatibility only, please use GetCount instead.


wxArrayString::Empty

void Empty()

Empties the array: after a call to this function GetCount will return 0. However, this function does not free the memory used by the array and so should be used when the array is going to be reused for storing other strings. Otherwise, you should use Clear to empty the array and free memory.


wxArrayString::GetCount

size_t GetCount() const

Returns the number of items in the array.


wxArrayString::Index

int Index(const char * sz, bool bCase = TRUE, bool bFromEnd = FALSE)

Search the element in the array, starting from the beginning if bFromEnd is FALSE or from end otherwise. If bCase, comparison is case sensitive (default), otherwise the case is ignored.

Returns index of the first item matched or wxNOT_FOUND if there is no match.


wxArrayString::Insert

void Insert(const wxString& str, size_t nIndex)

Insert a new element in the array before the position nIndex. Thus, for example, to insert the string in the beginning of the array you would write

Insert("foo", 0);
If nIndex is equal to GetCount() + 1 this function behaves as Add.


wxArrayString::IsEmpty

IsEmpty()

Returns TRUE if the array is empty, FALSE otherwise. This function returns the same result as GetCount() == 0 but is probably easier to read.


wxArrayString::Item

wxString& Item(size_t nIndex) const

Return the array element at position nIndex. An assert failure will result from an attempt to access an element beyond the end of array in debug mode, but no check is done in release mode.

See also operator[] for the operator version.


wxArrayString::Last

Last()

Returns the last element of the array. Attempt to access the last element of an empty array will result in assert failure in debug build, however no checks are done in release mode.


wxArrayString::Remove (by value)

void Remove(const char * sz)

Removes the first item matching this value. An assert failure is provoked by an attempt to remove an element which does not exist in debug build.

See also: Index, Remove


wxArrayString::Remove (by index)

void Remove(size_t nIndex)

Removes the item at given position.

See also: Remove


wxArrayString::Shrink

void Shrink()

Releases the extra memory allocated by the array. This function is useful to minimize the array memory consumption.

See also: Alloc, Dynamic array memory management


wxArrayString::Sort (alphabetically)

void Sort(bool reverseOrder = FALSE)

Sorts the array in alphabetical order or in reverse alphabetical order if reverseOrder is TRUE.

See also: Sort


wxArrayString::Sort (user defined)

void Sort(CompareFunction compareFunction)

Sorts the array using the specified compareFunction for item comparison. CompareFunction is defined as a function taking two const wxString& parameters and returning int value less than, equal to or greater than 0 if the first string is less than, equal to or greater than the second one.

Example

The following example sorts strings by their length.

static int CompareStringLen(const wxString& first, const wxString& second)
{
    return first.length() - second.length();
}

...

wxArrayString array;

array.Add("one");
array.Add("two");
array.Add("three");
array.Add("four");

array.Sort(CompareStringLen);
See also: Sort