Version: 3.3.0
wxArrayString Class Reference

#include <wx/arrstr.h>

+ Inheritance diagram for wxArrayString:

Detailed Description

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

// wxListBox ctor is documented as taking wxArrayString but you can
// pass an initializer_list to it directly:
auto listbox = new wxListBox(parent, wxID_ANY,
{ "some", "items", "for", "the", "listbox" });
// Similarly, if you already have a vector filled with strings
// elsewhere in your program, you can just pass it instead:
std::vector<std::string> countries = GetListOfCountries();
auto choices = new wxChoice(parent, wxID_ANY,
countries);
A choice item is used to select one of a list of strings.
Definition: choice.h:34
A listbox is used to select one or more of a list of strings.
Definition: listbox.h:74
@ wxID_ANY
Any id: means that we don't care about the id, whether when installing an event handler or when creat...
Definition: defs.h:590
const wxSize wxDefaultSize
Global instance of a wxSize object initialized to (-1,-1).
Definition: gdicmn.h:1149
const wxPoint wxDefaultPosition
Global instance of a wxPoint initialized with values (-1,-1).
Definition: gdicmn.h:808

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.

wxDir::GetAllFiles("/some/path", &files);
// Can use the usual accessors:
if ( !files.empty() ) {
auto first = files[0];
auto total = files.size();
...
}
// Can iterate over it like over a vector, too.
for ( const wxString& file: files ) {
...
}
// Or can just convert it to the "real" vector:
const std::vector<wxString>& vec = files.AsVector();
wxArrayString is a legacy class similar to std::vector<wxString>.
Definition: arrstr.h:67
std::vector< wxString > AsVector() const
Constructs a std::vector containing the same strings as this array.
static size_t GetAllFiles(const wxString &dirname, wxArrayString *files, const wxString &filespec=wxEmptyString, int flags=wxDIR_DEFAULT)
The function appends the names of all the files under directory dirname to the array files (note that...
String class for passing textual data to or receiving it from wxWidgets.
Definition: string.h:372

Library:  wxBase
Category:  Containers
See also
wxSortedArrayString, wxArray<T>, wxString, wxString Overview

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 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< wxStringAsVector () 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...
 
wxArrayStringoperator= (const wxArrayString &)
 Assignment operator. More...
 
bool operator== (const wxArrayString &array) const
 Compares 2 arrays respecting the case. More...
 
wxStringoperator[] (size_t nIndex) const
 Returns 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...
 
wxStringItem (size_t nIndex)
 Return the array element at position nIndex. More...
 
const wxStringItem (size_t nIndex) const
 Return the array element at position nIndex. More...
 
wxStringLast ()
 Returns the last element of the array. More...
 
const wxStringLast () const
 Returns the last element of the array. More...
 

Member Typedef Documentation

◆ CompareFunction

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

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.

Since
3.1.0

Constructor & Destructor Documentation

◆ wxArrayString() [1/8]

wxArrayString::wxArrayString ( )

Default constructor.

◆ wxArrayString() [2/8]

wxArrayString::wxArrayString ( const wxArrayString array)

Copy constructor.

◆ wxArrayString() [3/8]

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

Constructor from a C string array.

Pass a size sz and an array arr.

◆ wxArrayString() [4/8]

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

Constructor from a C string array.

Pass a size sz and an array arr.

◆ wxArrayString() [5/8]

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

Constructor from a wxString array.

Pass a size sz and array arr.

◆ wxArrayString() [6/8]

template<typename T >
wxArrayString::wxArrayString ( std::initializer_list< T >  list)

Constructs the container with the contents of the initializer_list list.

Since
3.2.3

◆ wxArrayString() [7/8]

template<typename T >
wxArrayString::wxArrayString ( const std::vector< T > &  vec)

Constructs the container with the contents of the vector vec.

Template parameter T must be convertible to wxString, i.e. it can be wxString itself or std::string, std::wstring etc.

Since
3.3.0

◆ wxArrayString() [8/8]

template<typename T >
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.

Since
3.3.0

◆ ~wxArrayString()

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

◆ Add()

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

◆ Alloc()

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.

◆ AsVector()

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:

const std::vector<wxString>& vec = array.AsVector();

works in all build variants as long as you don't need to modify the returned vector and doesn't impose any extra overhead.

Since
3.3.0

◆ Clear()

void wxArrayString::Clear ( )

Clears the array contents and frees memory.

See also
Empty()

◆ 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.

◆ GetCount()

size_t wxArrayString::GetCount ( ) const

Returns the number of items in the array.

◆ Index()

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.

◆ Insert()

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:

Insert("foo", 0);
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 nInde...

If nIndex is equal to GetCount() this function behaves as Add().

◆ IsEmpty()

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.

◆ Item() [1/2]

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.

◆ Item() [2/2]

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.

◆ Last() [1/2]

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.

◆ Last() [2/2]

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.

◆ operator!=()

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.

◆ operator=()

wxArrayString& wxArrayString::operator= ( const wxArrayString )

Assignment operator.

◆ 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.

◆ operator[]()

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.

◆ Remove()

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

◆ RemoveAt()

void wxArrayString::RemoveAt ( size_t  nIndex,
size_t  count = 1 
)

Removes count items starting at position nIndex from the array.

◆ Shrink()

void wxArrayString::Shrink ( )

Releases the extra memory allocated by the array.

This function is useful to minimize the array memory consumption.

See also
Alloc()

◆ Sort() [1/2]

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.

◆ Sort() [2/2]

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);
size_t length() const