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 @e 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 @e copies of the new item @e str to the
80 array and returns the index of the first new item in the array.
82 @b Warning: For sorted arrays, the index of the inserted item will not be,
83 in general, equal to GetCount() - 1 because
84 the item is inserted at the correct position to keep the array sorted and not
89 #define size_t Add(const wxString& str, size_t copies = 1) /* implementation is private */
92 Preallocates enough memory to store @e nCount items. This function may be
93 used to improve array class performance before adding a known number of items
96 See also: @ref wxArray::memorymanagement "Dynamic array memory management"
98 void Alloc(size_t nCount
);
101 Clears the array contents and frees memory.
108 Empties the array: after a call to this function
109 GetCount() will return 0. However, this
110 function does not free the memory used by the array and so should be used when
111 the array is going to be reused for storing other strings. Otherwise, you
112 should use Clear() to empty the array and free
118 Returns the number of items in the array.
123 Search the element in the array, starting from the beginning if
124 @e bFromEnd is @false or from end otherwise. If @e bCase, comparison is
125 case sensitive (default), otherwise the case is ignored.
127 This function uses linear search for wxArrayString and binary search for
128 wxSortedArrayString, but it ignores the @e bCase and @e bFromEnd
129 parameters in the latter case.
131 Returns index of the first item matched or @c wxNOT_FOUND if there is no match.
133 int Index(const wxString
& sz
, bool bCase
= @
true,
134 bool bFromEnd
= @
false);
137 Insert the given number of @e copies of the new element in the array before the
138 position @e nIndex. Thus, for
139 example, to insert the string in the beginning of the array you would write
140 If @e nIndex is equal to @e GetCount() this function behaves as
143 @b Warning: this function should not be used with sorted arrays because it
144 could break the order of items and, for example, subsequent calls to
145 Index() would then not work!
147 void Insert(const wxString
& str
, size_t nIndex
,
151 Returns @true if the array is empty, @false otherwise. This function returns the
152 same result as @e GetCount() == 0 but is probably easier to read.
157 Return the array element at position @e nIndex. An assert failure will
158 result from an attempt to access an element beyond the end of array in debug
159 mode, but no check is done in release mode.
161 See also @ref operatorindex() operator[] for the operator
164 wxString
Item(size_t nIndex
);
167 Returns the last element of the array. Attempt to access the last element of
168 an empty array will result in assert failure in debug build, however no checks
169 are done in release mode.
174 Removes the first item matching this value. An assert failure is provoked by
175 an attempt to remove an element which does not exist in debug build.
179 void Remove(const wxString
& sz
);
182 Removes @e count items starting at position @e nIndex from the array.
184 void RemoveAt(size_t nIndex
, size_t count
= 1);
187 Releases the extra memory allocated by the array. This function is useful to
188 minimize the array memory consumption.
190 See also: Alloc(), @ref wxArray::memorymanagement "Dynamic array memory
197 Sorts the array using the specified @e compareFunction for item comparison.
198 @e CompareFunction is defined as a function taking two @e const
199 wxString parameters and returning an @e int value less than, equal to or
200 greater than 0 if the first string is less than, equal to or greater than the
203 void Sort(bool reverseOrder
= @
false);
205 void Sort(CompareFunction compareFunction
);
209 Compares 2 arrays respecting the case. Returns @true if the arrays have
210 different number of elements or if the elements don't match pairwise.
212 bool operator !=(const wxArrayString
& array
);
217 wxArrayString
operator =(const wxArrayString
& array
);
220 Compares 2 arrays respecting the case. Returns @true only if the arrays have
221 the same number of elements and the same strings in the same order.
223 bool operator ==(const wxArrayString
& array
);
226 Return the array element at position @e nIndex. An assert failure will
227 result from an attempt to access an element beyond the end of array in debug
228 mode, but no check is done in release mode.
230 This is the operator version of Item() method.
232 wxString
operator[](size_t nIndex
);
236 // ============================================================================
237 // Global functions/macros
238 // ============================================================================
241 Splits the given wxString object using the separator @e sep and returns the
242 result as a wxArrayString.
244 If the @e escape character is non-@NULL, then the occurrences of @e sep
246 with @e escape are not considered as separators.
248 Note that empty tokens will be generated if there are two or more adjacent
253 wxArrayString
wxSplit(const wxString
& str
, const wxChar sep
,
254 const wxChar escape
= '
258 Concatenate all lines of the given wxArrayString object using the separator @e
260 the result as a wxString.
262 If the @e escape character is non-@NULL, then it's used as prefix for each
264 in the strings contained in @e arr before joining them which is necessary
265 in order to be able to recover the original array contents from the string
268 wxString
wxJoin(const wxArrayString
& arr
, const wxChar sep
,
269 const wxChar escape
= '\');