wxWidgets: wxArrayString Class Reference

| Version: 3.1.7

Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
wxArrayString Class Reference
Class List by Category » Containers

`#include </cygdrive/c/Users/Hamis/Cygwin-packaging/wxWidgets3.1/wxWidgets3.1-3.1.7-1.x86_64/src/wxWidgets-3.1.7/interface/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
  wxSortedArrayString, wxArray<T>, wxString, wxString Overview

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

## Public Member Functions
| wxArrayString ()
| Default constructor.
| wxArrayString (const wxArrayString &array)
| Copy constructor.
| wxArrayString (size_t sz, const char **arr)
| Constructor from a C string array.
| wxArrayString (size_t sz, const wchar_t **arr)
| wxArrayString (size_t sz, const wxString *arr)
| Constructor from a wxString array.
| ~wxArrayString ()
| Destructor frees memory occupied by the array strings.
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.
void | Alloc (size_t nCount)
| Preallocates enough memory to store nCount items.
void | Clear ()
| Clears the array contents and frees memory.
void | Empty ()
| Empties the array: after a call to this function GetCount() will return 0.
size_t | GetCount () const
| Returns the number of items in the array.
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.
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.
bool | IsEmpty () const
| Returns true if the array is empty, false otherwise.
wxString & | Item (size_t nIndex)
| Return the array element at position nIndex.
const wxString & | Item (size_t nIndex) const
wxString & | Last ()
| Returns the last element of the array.
const wxString & | Last () const
void | Remove (const wxString &sz)
| Removes the first item matching this value.
void | RemoveAt (size_t nIndex, size_t count=1)
| Removes count items starting at position nIndex from the array.
void | Shrink ()
| Releases the extra memory allocated by the array.
void | Sort (bool reverseOrder=false)
| Sorts the array in alphabetical order or in reverse alphabetical order if reverseOrder is true.
void | Sort (CompareFunction compareFunction)
| Sorts the array using the specified compareFunction for item comparison.
bool | operator!= (const wxArrayString &array) const
| Compares 2 arrays respecting the case.
wxArrayString & | operator= (const wxArrayString &)
| Assignment operator.
bool | operator== (const wxArrayString &array) const
| Compares 2 arrays respecting the case.
wxString & | operator[] (size_t nIndex) const
| Returns the array element at position nIndex.

## 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/5]

wxArrayString::wxArrayString | ( |  | ) |

Default constructor.

## ◆ wxArrayString() [2/5]

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

Copy constructor.

## ◆ wxArrayString() [3/5]

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

Constructor from a C string array.

Pass a size sz and an array arr.

## ◆ wxArrayString() [4/5]

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

## ◆ wxArrayString() [5/5]

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

Constructor from a wxString array.

Pass a size sz and array arr.

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

## ◆ 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);
wxArrayString::Insert
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

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

## ◆ 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);
wxString
String class for passing textual data to or receiving it from wxWidgets.
Definition string.h:315
wxString::length
size_t length() const
An 'invalid' value for string index.