## 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();

There is also a varian of wxArrayString called wxSortedArrayString which has exactly the same methods as wxArrayString, but which always keeps the string in it in (alphabetical) order. wxSortedArrayString uses binary search in its Index function (insteadf of linear search for wxArrayString::Index) which makes it much more efficient if you add strings to the array rarely (because, of course, you have to pay for Index() efficiency by having Add() be slower) but search for them often. Several methods should not be used with sorted array (basicly, all which break the order of items) which is mentioned in their description.

Final word: none of the methods of wxArrayString is virtual including its destructor, so this class should not be used as a base class.

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::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
wxArrayString::Shrink
wxArrayString::Sort

### wxArrayString::wxArrayString

wxArrayString()

wxArrayString(const wxArrayString& array)

Default and copy constructors.

Note that when an array is assigned to a sorted array, its contents is automatically sorted during construction.

### 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==

bool operator ==(const wxArrayString& array) const

Compares 2 arrays respecting the case. Returns TRUE only if the arrays have the same number of elements and the same strings in the same order.

### wxArrayString::operator!=

bool operator !=(const wxArrayString& array) const

Compares 2 arrays respecting the case. Returns TRUE if the arrays have different number of elements or if the elements don't match pairwise.

### wxArrayString::operator[]

wxString& operator[](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

size_t Add(const wxString& str)

Appends a new item to the array and return the index of th new item in the array.

Warning: For sorted arrays, the index of the inserted item will not be, in general, equal to GetCount() - 1 because the item is inserted at the correct position to keep the array sorted and not appended.

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.

This function uses linear search for wxArrayString and binary search for wxSortedArrayString, but it ignores the bCase and bFromEnd parameters in the latter case.

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.

Warning: this function should not be used with sorted arrays because it could break the order of items and, for example, subsequent calls to Index() would then not work!

### 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

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

void Remove(size_t nIndex)

Removes the item at given position.

### 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

void Sort(bool reverseOrder = FALSE)

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

Warning: this function should not be used with sorted array because it could break the order of items and, for example, subsequent calls to Index() would then not work!

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 an 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);

Warning: this function should not be used with sorted array because it could break the order of items and, for example, subsequent calls to Index() would then not work!