1. made wxBase compile/link/run again under Unix
[wxWidgets.git] / docs / latex / wx / arrstrng.tex
1 \section{\class{wxArrayString}}\label{wxarraystring}
2
3 wxArrayString is an efficient container for storing
4 \helpref{wxString}{wxstring} objects. It has the same features as all
5 \helpref{wxArray}{wxarray} classes, i.e. it dynamically expands when new items
6 are added to it (so it is as easy to use as a linked list), but the access
7 time to the elements is constant, instead of being linear in number of
8 elements as in the case of linked lists. It is also very size efficient and
9 doesn't take more space than a C array {\it wxString[]} type (wxArrayString
10 uses its knowledge of internals of wxString class to achieve this).
11
12 This class is used in the same way as other dynamic \helpref{arrays}{wxarray},
13 except that no {\it WX\_DEFINE\_ARRAY} declaration is needed for it. When a
14 string is added or inserted in the array, a copy of the string is created, so
15 the original string may be safely deleted (e.g. if it was a {\it char *}
16 pointer the memory it was using can be freed immediately after this). In
17 general, there is no need to worry about string memory deallocation when using
18 this class - it will always free the memory it uses itself.
19
20 The references returned by \helpref{Item}{wxarraystringitem},
21 \helpref{Last}{wxarraystringlast} or
22 \helpref{operator[]}{wxarraystringoperatorindex} are not constant, so the
23 array elements may be modified in place like this
24
25 \begin{verbatim}
26 array.Last().MakeUpper();
27 \end{verbatim}
28
29 There is also a varian of wxArrayString called wxSortedArrayString which has
30 exactly the same methods as wxArrayString, but which always keeps the string
31 in it in (alphabetical) order. wxSortedArrayString uses binary search in its
32 \helpref{Index}{wxarraystringindex} function (insteadf of linear search for
33 wxArrayString::Index) which makes it much more efficient if you add strings to
34 the array rarely (because, of course, you have to pay for Index() efficiency
35 by having Add() be slower) but search for them often. Several methods should
36 not be used with sorted array (basicly, all which break the order of items)
37 which is mentioned in their description.
38
39 Final word: none of the methods of wxArrayString is virtual including its
40 destructor, so this class should not be used as a base class.
41
42 \wxheading{Derived from}
43
44 Although this is not true strictly speaking, this class may be considered as a
45 specialization of \helpref{wxArray}{wxarray} class for the wxString member
46 data: it is not implemented like this, but it does have all of the wxArray
47 functions.
48
49 \wxheading{Include files}
50
51 <wx/string.h>
52
53 \wxheading{See also}
54
55 \helpref{wxArray}{wxarray}, \helpref{wxString}{wxstring}, \helpref{wxString overview}{wxstringoverview}
56
57 \latexignore{\rtfignore{\wxheading{Members}}}
58
59 \membersection{wxArrayString::wxArrayString}\label{wxarraystringctor}
60
61 \func{}{wxArrayString}{\void}
62
63 \func{}{wxArrayString}{\param{const wxArrayString\&}{ array}}
64
65 Default and copy constructors.
66
67 Note that when an array is assigned to a sorted array, its contents is
68 automatically sorted during construction.
69
70 \membersection{wxArrayString::\destruct{wxArrayString}}\label{wxarraystringdtor}
71
72 \func{}{\destruct{wxArrayString}}{}
73
74 Destructor frees memory occupied by the array strings. For the performance
75 reasons it is not virtual, so this class should not be derived from.
76
77 \membersection{wxArrayString::operator=}\label{wxarraystringoperatorassign}
78
79 \func{wxArrayString \&}{operator $=$}{\param{const wxArrayString\&}{ array}}
80
81 Assignment operator.
82
83 \membersection{wxArrayString::operator[]}\label{wxarraystringoperatorindex}
84
85 \func{wxString\&}{operator[]}{\param{size\_t }{nIndex}}
86
87 Return the array element at position {\it nIndex}. An assert failure will
88 result from an attempt to access an element beyond the end of array in debug
89 mode, but no check is done in release mode.
90
91 This is the operator version of \helpref{Item}{wxarraystringitem} method.
92
93 \membersection{wxArrayString::Add}\label{wxarraystringadd}
94
95 \func{void}{Add}{\param{const wxString\& }{str}}
96
97 Appends a new item to the array.
98
99 {\bf Warning:} For sorted arrays, the index of the inserted item will not be,
100 in general, equal to \helpref{GetCount()}{wxarraystringgetcount} - 1 because
101 the item is inserted at the correct position to keep the array sorted and not
102 appended.
103
104 See also: \helpref{Insert}{wxarraystringinsert}
105
106 \membersection{wxArrayString::Alloc}\label{wxarraystringalloc}
107
108 \func{void}{Alloc}{\param{size\_t }{nCount}}
109
110 Preallocates enough memory to store {\it nCount} items. This function may be
111 used to improve array class performance before adding a known number of items
112 consecutively.
113
114 See also: \helpref{Dynamic array memory management}{wxarraymemorymanagement}
115
116 \membersection{wxArrayString::Clear}\label{wxarraystringclear}
117
118 \func{void}{Clear}{\void}
119
120 Clears the array contents and frees memory.
121
122 See also: \helpref{Empty}{wxarraystringempty}
123
124 \membersection{wxArrayString::Count}\label{wxarraystringcount}
125
126 \constfunc{size\_t}{Count}{\void}
127
128 Returns the number of items in the array. This function is deprecated and is
129 for backwards compatibility only, please use
130 \helpref{GetCount}{wxarraystringgetcount} instead.
131
132 \membersection{wxArrayString::Empty}\label{wxarraystringempty}
133
134 \func{void}{Empty}{\void}
135
136 Empties the array: after a call to this function
137 \helpref{GetCount}{wxarraystringgetcount} will return $0$. However, this
138 function does not free the memory used by the array and so should be used when
139 the array is going to be reused for storing other strings. Otherwise, you
140 should use \helpref{Clear}{wxarraystringclear} to empty the array and free
141 memory.
142
143 \membersection{wxArrayString::GetCount}\label{wxarraystringgetcount}
144
145 \constfunc{size\_t}{GetCount}{\void}
146
147 Returns the number of items in the array.
148
149 \membersection{wxArrayString::Index}\label{wxarraystringindex}
150
151 \func{int}{Index}{\param{const char *}{ sz}, \param{bool}{ bCase = TRUE}, \param{bool}{ bFromEnd = FALSE}}
152
153 Search the element in the array, starting from the beginning if
154 {\it bFromEnd} is FALSE or from end otherwise. If {\it bCase}, comparison is
155 case sensitive (default), otherwise the case is ignored.
156
157 This function uses linear search for wxArrayString and binary search for
158 wxSortedArrayString, but it ignores the {\it bCase} and {\it bFromEnd}
159 parameters in the latter case.
160
161 Returns index of the first item matched or wxNOT\_FOUND if there is no match.
162
163 \membersection{wxArrayString::Insert}\label{wxarraystringinsert}
164
165 \func{void}{Insert}{\param{const wxString\& }{str}, \param{size\_t}{ nIndex}}
166
167 Insert a new element in the array before the position {\it nIndex}. Thus, for
168 example, to insert the string in the beginning of the array you would write
169
170 \begin{verbatim}
171 Insert("foo", 0);
172 \end{verbatim}
173
174 If {\it nIndex} is equal to {\it GetCount() + 1} this function behaves as
175 \helpref{Add}{wxarraystringadd}.
176
177 {\bf Warning:} this function should not be used with sorted array because it
178 could break the order of items and, for example, subsequent calls to
179 \helpref{Index()}{wxarraystringindex} would not work then!
180
181 \membersection{wxArrayString::IsEmpty}\label{wxarraystringisempty}
182
183 \func{}{IsEmpty}{}
184
185 Returns TRUE if the array is empty, FALSE otherwise. This function returns the
186 same result as {\it GetCount() == 0} but is probably easier to read.
187
188 \membersection{wxArrayString::Item}\label{wxarraystringitem}
189
190 \constfunc{wxString\&}{Item}{\param{size\_t }{nIndex}}
191
192 Return the array element at position {\it nIndex}. An assert failure will
193 result from an attempt to access an element beyond the end of array in debug
194 mode, but no check is done in release mode.
195
196 See also \helpref{operator[]}{wxarraystringoperatorindex} for the operator
197 version.
198
199 \membersection{wxArrayString::Last}\label{wxarraystringlast}
200
201 \func{}{Last}{}
202
203 Returns the last element of the array. Attempt to access the last element of
204 an empty array will result in assert failure in debug build, however no checks
205 are done in release mode.
206
207 \membersection{wxArrayString::Remove (by value)}\label{wxarraystringremoveval}
208
209 \func{void}{Remove}{\param{const char *}{ sz}}
210
211 Removes the first item matching this value. An assert failure is provoked by
212 an attempt to remove an element which does not exist in debug build.
213
214 See also: \helpref{Index}{wxarraystringindex}, \helpref{Remove}{wxarraystringremove}
215
216 \membersection{wxArrayString::Remove (by index)}\label{wxarraystringremove}
217
218 \func{void}{Remove}{\param{size\_t }{nIndex}}
219
220 Removes the item at given position.
221
222 See also: \helpref{Remove}{wxarraystringremoveval}
223
224 \membersection{wxArrayString::Shrink}\label{wxarraystringshrink}
225
226 \func{void}{Shrink}{\void}
227
228 Releases the extra memory allocated by the array. This function is useful to
229 minimize the array memory consumption.
230
231 See also: \helpref{Alloc}{wxarraystringalloc}, \helpref{Dynamic array memory management}{wxarraymemorymanagement}
232
233 \membersection{wxArrayString::Sort (alphabetically)}\label{wxarraystringsort}
234
235 \func{void}{Sort}{\param{bool}{ reverseOrder = FALSE}}
236
237 Sorts the array in alphabetical order or in reverse alphabetical order if
238 {\it reverseOrder} is TRUE.
239
240 {\bf Warning:} this function should not be used with sorted array because it
241 could break the order of items and, for example, subsequent calls to
242 \helpref{Index()}{wxarraystringindex} would not work then!
243
244 See also: \helpref{Sort}{wxarraystringsortcallback}
245
246 \membersection{wxArrayString::Sort (user defined)}\label{wxarraystringsortcallback}
247
248 \func{void}{Sort}{\param{CompareFunction }{compareFunction}}
249
250 Sorts the array using the specified {\it compareFunction} for item comparison.
251 {\it CompareFunction} is defined as a function taking two {\it const
252 wxString\&} parameters and returning {\it int} value less than, equal to or
253 greater than 0 if the first string is less than, equal to or greater than the
254 second one.
255
256 \wxheading{Example}
257
258 The following example sorts strings by their length.
259
260 \begin{verbatim}
261 static int CompareStringLen(const wxString& first, const wxString& second)
262 {
263 return first.length() - second.length();
264 }
265
266 ...
267
268 wxArrayString array;
269
270 array.Add("one");
271 array.Add("two");
272 array.Add("three");
273 array.Add("four");
274
275 array.Sort(CompareStringLen);
276 \end{verbatim}
277
278 {\bf Warning:} this function should not be used with sorted array because it
279 could break the order of items and, for example, subsequent calls to
280 \helpref{Index()}{wxarraystringindex} would not work then!
281
282 See also: \helpref{Sort}{wxarraystringsort}
283