1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: documentation for wxArrayString class
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
13 wxArrayString is an efficient container for storing
14 wxString objects. It has the same features as all
15 wxArray classes, i.e. it dynamically expands when new items
16 are added to it (so it is as easy to use as a linked list), but the access
17 time to the elements is constant, instead of being linear in number of
18 elements as in the case of linked lists. It is also very size efficient and
19 doesn't take more space than a C array @e wxString[] type (wxArrayString
20 uses its knowledge of internals of wxString class to achieve this).
22 This class is used in the same way as other dynamic arrays,
23 except that no @e WX_DEFINE_ARRAY declaration is needed for it. When a
24 string is added or inserted in the array, a copy of the string is created, so
25 the original string may be safely deleted (e.g. if it was a @e wxChar *
26 pointer the memory it was using can be freed immediately after this). In
27 general, there is no need to worry about string memory deallocation when using
28 this class - it will always free the memory it uses itself.
30 The references returned by wxArrayString::Item,
31 wxArrayString::Last or
32 @ref wxArrayString::operatorindex operator[] are not constant, so the
33 array elements may be modified in place like this
36 array.Last().MakeUpper();
39 There is also a variant of wxArrayString called wxSortedArrayString which has
40 exactly the same methods as wxArrayString, but which always keeps the string
41 in it in (alphabetical) order. wxSortedArrayString uses binary search in its
42 wxArrayString::Index function (instead of linear search for
43 wxArrayString::Index) which makes it much more efficient if you add strings to
44 the array rarely (because, of course, you have to pay for Index() efficiency
45 by having Add() be slower) but search for them often. Several methods should
46 not be used with sorted array (basically, all which break the order of items)
47 which is mentioned in their description.
49 Final word: none of the methods of wxArrayString is virtual including its
50 destructor, so this class should not be used as a base class.
56 wxArray, wxString, @ref overview_wxstringoverview "wxString overview"
58 class wxArrayString
: public wxArray
63 Constructor from a wxString array. Pass a size @a sz and array @e arr.
66 wxArrayString(const wxArrayString
& array
);
67 wxArrayString(size_t sz
, const char** arr
);
68 wxArrayString(size_t sz
, const wchar_t** arr
);
69 wxArrayString(size_t sz
, const wxString
* arr
);
73 Destructor frees memory occupied by the array strings. For the performance
74 reasons it is not virtual, so this class should not be derived from.
79 Appends the given number of @a copies of the new item @a str to the
80 array and returns the index of the first new item in the array.
81 @b Warning: For sorted arrays, the index of the inserted item will not be,
82 in general, equal to GetCount() - 1 because
83 the item is inserted at the correct position to keep the array sorted and not
87 size_t Add(const wxString
& str
, size_t copies
= 1);
90 Preallocates enough memory to store @a nCount items. This function may be
91 used to improve array class performance before adding a known number of items
93 See also: @ref wxArray::memorymanagement "Dynamic array memory management"
95 void Alloc(size_t nCount
);
98 Clears the array contents and frees memory.
104 Empties the array: after a call to this function
105 GetCount() will return 0. However, this
106 function does not free the memory used by the array and so should be used when
107 the array is going to be reused for storing other strings. Otherwise, you
108 should use Clear() to empty the array and free
114 Returns the number of items in the array.
116 size_t GetCount() const;
119 Search the element in the array, starting from the beginning if
120 @a bFromEnd is @false or from end otherwise. If @e bCase, comparison is
121 case sensitive (default), otherwise the case is ignored.
122 This function uses linear search for wxArrayString and binary search for
123 wxSortedArrayString, but it ignores the @a bCase and @a bFromEnd
124 parameters in the latter case.
125 Returns index of the first item matched or @c wxNOT_FOUND if there is no match.
127 int Index(const wxString
& sz
, bool bCase
= true,
128 bool bFromEnd
= false);
131 Insert the given number of @a copies of the new element in the array before the
132 position @e nIndex. Thus, for
133 example, to insert the string in the beginning of the array you would write
135 If @a nIndex is equal to @e GetCount() this function behaves as
137 @b Warning: this function should not be used with sorted arrays because it
138 could break the order of items and, for example, subsequent calls to
139 Index() would then not work!
141 void Insert(const wxString
& str
, size_t nIndex
,
145 Returns @true if the array is empty, @false otherwise. This function returns the
146 same result as @e GetCount() == 0 but is probably easier to read.
151 Return the array element at position @e nIndex. An assert failure will
152 result from an attempt to access an element beyond the end of array in debug
153 mode, but no check is done in release mode.
154 See also @ref operatorindex() operator[] for the operator
157 wxString
Item(size_t nIndex
) const;
160 Returns the last element of the array. Attempt to access the last element of
161 an empty array will result in assert failure in debug build, however no checks
162 are done in release mode.
167 Removes the first item matching this value. An assert failure is provoked by
168 an attempt to remove an element which does not exist in debug build.
171 void Remove(const wxString
& sz
);
174 Removes @a count items starting at position @a nIndex from the array.
176 void RemoveAt(size_t nIndex
, size_t count
= 1);
179 Releases the extra memory allocated by the array. This function is useful to
180 minimize the array memory consumption.
181 See also: Alloc(), @ref wxArray::memorymanagement "Dynamic array memory
188 Sorts the array using the specified @a compareFunction for item comparison.
189 @e CompareFunction is defined as a function taking two @e const
190 wxString parameters and returning an @e int value less than, equal to or
191 greater than 0 if the first string is less than, equal to or greater than the
194 void Sort(bool reverseOrder
= false);
196 void Sort(CompareFunction compareFunction
);
200 Compares 2 arrays respecting the case. Returns @true if the arrays have
201 different number of elements or if the elements don't match pairwise.
203 bool operator !=(const wxArrayString
& array
) const;
208 wxArrayString
operator =(const wxArrayString
& array
);
211 Compares 2 arrays respecting the case. Returns @true only if the arrays have
212 the same number of elements and the same strings in the same order.
214 bool operator ==(const wxArrayString
& array
) const;
217 Return the array element at position @e nIndex. An assert failure will
218 result from an attempt to access an element beyond the end of array in debug
219 mode, but no check is done in release mode.
220 This is the operator version of Item() method.
222 wxString
operator[](size_t nIndex
);
226 // ============================================================================
227 // Global functions/macros
228 // ============================================================================
231 Splits the given wxString object using the separator @a sep and returns the
232 result as a wxArrayString.
233 If the @a escape character is non-@NULL, then the occurrences of @a sep
235 with @a escape are not considered as separators.
236 Note that empty tokens will be generated if there are two or more adjacent
241 wxArrayString
wxSplit(const wxString
& str
, const wxChar sep
,
242 const wxChar escape
= '
246 Concatenate all lines of the given wxArrayString object using the separator @a
248 the result as a wxString.
249 If the @a escape character is non-@NULL, then it's used as prefix for each
251 in the strings contained in @a arr before joining them which is necessary
252 in order to be able to recover the original array contents from the string
255 wxString
wxJoin(const wxArrayString
& arr
, const wxChar sep
,
256 const wxChar escape
= '\');