| Version: 3.0.5

- Main Page
- Related Pages
- Categories
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Types | Public Member Functions | List of all members
wxArrayString Class Reference
Class List by Category » Containers

`#include <wx/arrstr.h>`

Inheritance diagram for wxArrayString:
[legend]

## Detailed Description

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 wxChar * 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 wxArrayString::Item, wxArrayString::Last or wxArrayString::operator[] are not constant, so the array elements may be modified in place like this:

array.Last().MakeUpper();

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

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.

It also has the full set of `std::vector<wxString>` compatible methods, including nested `iterator` and `const_iterator` classes which should be used in the new code for forward compatibility with the future wxWidgets versions.

##

Library: wxBase
Category: Containers

See also
  wxArray<T>, wxString, wxString Overview

## Public Types

typedef int(* | CompareFunction )(const wxString &first, const wxString &second)
| The function type used with wxArrayString::Sort function. More...

## Public Member Functions

| wxArrayString ()
| Default constructor. More...
| wxArrayString (const wxArrayString &array)
| Copy constructor. More...
| wxArrayString (size_t sz, const wxString *arr)
| Constructor from a wxString array. More...
| ~wxArrayString ()
| Destructor frees memory occupied by the array strings. More...
size_t | Add (const wxString &str, size_t copies=1)
| Appends the given number of copies of the new item str to the array and returns the index of the first new item in the array. More...
void | Alloc (size_t nCount)
| Preallocates enough memory to store nCount items. More...
void | Clear ()
| Clears the array contents and frees memory. More...
void | Empty ()
| Empties the array: after a call to this function GetCount() will return 0. More...
size_t | GetCount () const
| Returns the number of items in the array. More...
int | Index (const wxString &sz, bool bCase=true, bool bFromEnd=false) const
| Search the element in the array, starting from the beginning if bFromEnd is false or from end otherwise. More...
void | Insert (wxString lItem, size_t nIndex, size_t copies=1)
| Insert the given number of copies of the new element in the array before the position nIndex. More...
bool | IsEmpty () const
| Returns true if the array is empty, false otherwise. More...
void | Remove (const wxString &sz)
| Removes the first item matching this value. More...
void | RemoveAt (size_t nIndex, size_t count=1)
| Removes count items starting at position nIndex from the array. More...
void | Shrink ()
| Releases the extra memory allocated by the array. More...
void | Sort (bool reverseOrder=false)
| Sorts the array in alphabetical order or in reverse alphabetical order if reverseOrder is true. More...
void | Sort (CompareFunction compareFunction)
| Sorts the array using the specified compareFunction for item comparison. More...
bool | operator!= (const wxArrayString &array) const
| Compares 2 arrays respecting the case. More...
wxArrayString & | operator= (const wxArrayString &)
| Assignment operator. More...
bool | operator== (const wxArrayString &array) const
| Compares 2 arrays respecting the case. More...
wxString & | operator[] (size_t nIndex) const
| Return the array element at position nIndex. More...
| wxArrayString (size_t sz, const char **arr)
| Constructor from a C string array. More...
| wxArrayString (size_t sz, const wchar_t **arr)
| Constructor from a C string array. More...
wxString & | Item (size_t nIndex)
| Return the array element at position nIndex. More...
const wxString & | Item (size_t nIndex) const
| Return the array element at position nIndex. More...
wxString & | Last ()
| Returns the last element of the array. More...
const wxString & | Last () const
| Returns the last element of the array. More...

## Member Typedef Documentation

typedef int(* wxArrayString::CompareFunction)(const wxString &first, const wxString &second)

The function type used with wxArrayString::Sort function.

## Constructor & Destructor Documentation

wxArrayString::wxArrayString | ( |  | ) |

Default constructor.

wxArrayString::wxArrayString | ( | const wxArrayString & | array | ) |

Copy constructor.

wxArrayString::wxArrayString | ( | size_t | sz,
|  | const char ** | arr
| ) |  |

Constructor from a C string array.

Pass a size sz and an array arr.

wxArrayString::wxArrayString | ( | size_t | sz,
|  | const wchar_t ** | arr
| ) |  |

Constructor from a C string array.

Pass a size sz and an array arr.

wxArrayString::wxArrayString | ( | size_t | sz,
|  | const wxString * | arr
| ) |  |

Constructor from a wxString array.

Pass a size sz and array arr.

wxArrayString::~wxArrayString | ( |  | ) |

Destructor frees memory occupied by the array strings.

For performance reasons it is not virtual, so this class should not be derived from.

## Member Function Documentation

size_t wxArrayString::Add | ( | const wxString & | str,
|  | size_t | copies = `1`
| ) |  |

Appends the given number of copies of the new item str to the array and returns the index of the first new item in the array.

See also
  Insert()

void wxArrayString::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.

void wxArrayString::Clear | ( |  | ) |

Clears the array contents and frees memory.

See also
  Empty()

void wxArrayString::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.

size_t wxArrayString::GetCount | ( |  | ) | const

Returns the number of items in the array.

int wxArrayString::Index | ( | const wxString & | sz,
|  | bool | bCase = `true`,
|  | bool | bFromEnd = `false`
| ) |  | const

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. Returns index of the first item matched or `wxNOT_FOUND` if there is no match.

void wxArrayString::Insert | ( | wxString | lItem,
|  | size_t | nIndex,
|  | size_t | copies = `1`
| ) |  |

Insert the given number of copies of the 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() this function behaves as Add().

bool wxArrayString::IsEmpty | ( |  | ) | const

Returns true if the array is empty, false otherwise.

This function returns the same result as GetCount() == 0 but is probably easier to read.

wxString& wxArrayString::Item | ( | 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.

See also
  operator[] for the operator version.

const wxString& wxArrayString::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.

wxString& wxArrayString::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.

const wxString& wxArrayString::Last | ( |  | ) | const

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.

bool wxArrayString::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& wxArrayString::operator= | ( | const wxArrayString & |  | ) |

Assignment operator.

bool wxArrayString::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.

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

This is the operator version of the Item() method.

void wxArrayString::Remove | ( | const wxString & | 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 wxArrayString::RemoveAt | ( | size_t | nIndex,
|  | size_t | count = `1`
| ) |  |

Removes count items starting at position nIndex from the array.

void wxArrayString::Shrink | ( |  | ) |

Releases the extra memory allocated by the array.

This function is useful to minimize the array memory consumption.

See also
  Alloc()

void wxArrayString::Sort | ( | bool | reverseOrder = `false` | ) |

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

The sort is case-sensitive.

void wxArrayString::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);