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