#include <wx/arrstr.h>
wxArrayString is a legacy class similar to std::vector<wxString>.
As all the other legacy container classes, this class shouldn't normally be used in the new code, but is still needed when passing multiple items to various functions in wxWidgets API, notably the constructors of various GUI control classes. Usually, even in this case it doesn't need to be used explicitly, as wxArrayString will be implicitly created if you use either an initializer list or a vector of strings, e.g. you can just pass either of those instead of wxArrayString, for example
When using a wxWidgets function returning an object of this class, you can either use it as if it were a std::vector<wxString>
, as this class has all vector methods, or actually convert it to such vector using its AsVector(), e.g.
Public Types | |
typedef int(* | CompareFunction) (const wxString &first, const wxString &second) |
The function type used with wxArrayString::Sort(). More... | |
Public Member Functions | |
wxArrayString () | |
Default constructor. More... | |
wxArrayString (const wxArrayString &array) | |
Copy constructor. 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 wide string array. More... | |
wxArrayString (size_t sz, const wxString *arr) | |
Constructor from a wxString array. More... | |
template<typename T > | |
wxArrayString (std::initializer_list< T > list) | |
Constructs the container with the contents of the initializer_list list. More... | |
template<typename T > | |
wxArrayString (const std::vector< T > &vec) | |
Constructs the container with the contents of the vector vec. More... | |
template<typename T > | |
wxArrayString (std::vector< T > &&vec) | |
Constructs the container with the contents of the vector vec. 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... | |
std::vector< wxString > | AsVector () const |
Constructs a std::vector containing the same strings as this array. 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 &str, bool bCase=true, bool bFromEnd=false) const |
Searches the array for str, starting from the beginning if bFromEnd is false or from the end otherwise. More... | |
void | Insert (const wxString &str, size_t nIndex, size_t copies=1) |
Inserts the given number of copies of str in the array before the array element at 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 |
Returns the array element at position nIndex. 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... | |
The function type used with wxArrayString::Sort().
This function uses the same conventions as the standard qsort()
comparison function, that is it should return a negative value if the first argument is less than the second one, a positive value if the first argument is greater than the second one and 0 if the arguments are equal.
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 wide 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 | ( | std::initializer_list< T > | list | ) |
Constructs the container with the contents of the initializer_list list.
wxArrayString::wxArrayString | ( | const std::vector< T > & | vec | ) |
wxArrayString::wxArrayString | ( | std::vector< T > && | vec | ) |
Constructs the container with the contents of the vector vec.
In the default build, in which wxArrayString is implemented using std::vector<>
internally, this constructor is more efficient than the overload taking const reference to the vector, as it reuses the existing vector data instead of copying it. Otherwise it is identical to the other overload, see its documentation for more details.
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.
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.
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.
std::vector<wxString> wxArrayString::AsVector | ( | ) | const |
Constructs a std::vector containing the same strings as this array.
In the default build configuration, this function returns a const reference to this object itself, without making a copy. But when using the legacy implementation of wxArrayString not based on std::vector
, it has to copy all the strings, making it expensive to call for big arrays.
Note that using it like this:
works in all build variants as long as you don't need to modify the returned vector and doesn't impose any extra overhead.
void wxArrayString::Clear | ( | ) |
Clears the array contents and frees memory.
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 & | str, |
bool | bCase = true , |
||
bool | bFromEnd = false |
||
) | const |
Searches the array for str, starting from the beginning if bFromEnd is false or from the end otherwise.
If bCase, comparison is case sensitive (default), otherwise the case is ignored.
This function uses linear search for wxArrayString. Returns the index of the first item matched or wxNOT_FOUND
if there is no match.
void wxArrayString::Insert | ( | const wxString & | str, |
size_t | nIndex, | ||
size_t | copies = 1 |
||
) |
Inserts the given number of copies of str in the array before the array element at the position nIndex.
Thus, for example, to insert the string in the beginning of the array you would write:
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.
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.
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 |
Returns 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.
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.
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.