]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/arrstrng.tex
Fix formatting tag in frame.tex
[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
2edb0bde 29There is also a variant of wxArrayString called wxSortedArrayString which has
e87271f3
VZ
30exactly the same methods as wxArrayString, but which always keeps the string
31in it in (alphabetical) order. wxSortedArrayString uses binary search in its
2edb0bde 32\helpref{Index}{wxarraystringindex} function (instead of linear search for
e87271f3
VZ
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
2edb0bde 36not be used with sorted array (basically, all which break the order of items)
e87271f3
VZ
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
df5168c4 51<wx/arrstr.h>
9e2be6f0
VZ
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
d44d0cbd 63Default constructor.
9e2be6f0 64
d44d0cbd 65\func{}{wxArrayString}{\param{const wxArrayString\&}{ array}}
9e2be6f0 66
d44d0cbd 67Copy constructor. Note that when an array is assigned to a sorted array, its contents is
e87271f3
VZ
68automatically sorted during construction.
69
d44d0cbd
JS
70\func{}{wxArrayString}{\param{size\_t}{ sz}, \param{const wxChar**}{ arr}}
71
72Constructor from a C string array. Pass a size {\it sz} and array {\it arr}.
73
74\func{}{wxArrayString}{\param{size\_t}{ sz}, \param{const wxString*}{ arr}}
75
76Constructor from a wxString array. Pass a size {\it sz} and array {\it arr}.
77
9e2be6f0
VZ
78\membersection{wxArrayString::\destruct{wxArrayString}}\label{wxarraystringdtor}
79
80\func{}{\destruct{wxArrayString}}{}
81
82Destructor frees memory occupied by the array strings. For the performance
83reasons it is not virtual, so this class should not be derived from.
84
85\membersection{wxArrayString::operator=}\label{wxarraystringoperatorassign}
86
87\func{wxArrayString \&}{operator $=$}{\param{const wxArrayString\&}{ array}}
88
89Assignment operator.
90
f6bcfd97
BP
91\membersection{wxArrayString::operator==}\label{wxarraystringoperatorequal}
92
93\constfunc{bool}{operator $==$}{\param{const wxArrayString\&}{ array}}
94
cc81d32f 95Compares 2 arrays respecting the case. Returns true only if the arrays have
f6bcfd97
BP
96the same number of elements and the same strings in the same order.
97
98\membersection{wxArrayString::operator!=}\label{wxarraystringoperatornotequal}
99
100\constfunc{bool}{operator $!=$}{\param{const wxArrayString\&}{ array}}
101
cc81d32f 102Compares 2 arrays respecting the case. Returns true if the arrays have
f6bcfd97
BP
103different number of elements or if the elements don't match pairwise.
104
9e2be6f0
VZ
105\membersection{wxArrayString::operator[]}\label{wxarraystringoperatorindex}
106
e87271f3 107\func{wxString\&}{operator[]}{\param{size\_t }{nIndex}}
9e2be6f0
VZ
108
109Return the array element at position {\it nIndex}. An assert failure will
110result from an attempt to access an element beyond the end of array in debug
111mode, but no check is done in release mode.
112
113This is the operator version of \helpref{Item}{wxarraystringitem} method.
114
115\membersection{wxArrayString::Add}\label{wxarraystringadd}
116
8a2a6bbf 117\func{size\_t}{Add}{\param{const wxString\& }{str}, \param{size\_t}{ copies = $1$}}
9e2be6f0 118
8a2a6bbf
SN
119Appends the given number of {\it copies} of the new item {\it str} to the
120array and returns the index of the first new item in the array.
9e2be6f0 121
e87271f3
VZ
122{\bf Warning:} For sorted arrays, the index of the inserted item will not be,
123in general, equal to \helpref{GetCount()}{wxarraystringgetcount} - 1 because
124the item is inserted at the correct position to keep the array sorted and not
125appended.
126
9e2be6f0
VZ
127See also: \helpref{Insert}{wxarraystringinsert}
128
129\membersection{wxArrayString::Alloc}\label{wxarraystringalloc}
130
131\func{void}{Alloc}{\param{size\_t }{nCount}}
132
133Preallocates enough memory to store {\it nCount} items. This function may be
134used to improve array class performance before adding a known number of items
135consecutively.
136
137See also: \helpref{Dynamic array memory management}{wxarraymemorymanagement}
138
139\membersection{wxArrayString::Clear}\label{wxarraystringclear}
140
141\func{void}{Clear}{\void}
142
143Clears the array contents and frees memory.
144
145See also: \helpref{Empty}{wxarraystringempty}
146
147\membersection{wxArrayString::Count}\label{wxarraystringcount}
148
149\constfunc{size\_t}{Count}{\void}
150
151Returns the number of items in the array. This function is deprecated and is
152for backwards compatibility only, please use
153\helpref{GetCount}{wxarraystringgetcount} instead.
154
155\membersection{wxArrayString::Empty}\label{wxarraystringempty}
156
157\func{void}{Empty}{\void}
158
159Empties the array: after a call to this function
160\helpref{GetCount}{wxarraystringgetcount} will return $0$. However, this
161function does not free the memory used by the array and so should be used when
162the array is going to be reused for storing other strings. Otherwise, you
163should use \helpref{Clear}{wxarraystringclear} to empty the array and free
164memory.
165
166\membersection{wxArrayString::GetCount}\label{wxarraystringgetcount}
167
168\constfunc{size\_t}{GetCount}{\void}
169
170Returns the number of items in the array.
171
172\membersection{wxArrayString::Index}\label{wxarraystringindex}
173
cc81d32f 174\func{int}{Index}{\param{const char *}{ sz}, \param{bool}{ bCase = true}, \param{bool}{ bFromEnd = false}}
9e2be6f0
VZ
175
176Search the element in the array, starting from the beginning if
cc81d32f 177{\it bFromEnd} is false or from end otherwise. If {\it bCase}, comparison is
9e2be6f0
VZ
178case sensitive (default), otherwise the case is ignored.
179
e87271f3
VZ
180This function uses linear search for wxArrayString and binary search for
181wxSortedArrayString, but it ignores the {\it bCase} and {\it bFromEnd}
182parameters in the latter case.
183
a8d08dbd 184Returns index of the first item matched or {\tt wxNOT\_FOUND} if there is no match.
9e2be6f0
VZ
185
186\membersection{wxArrayString::Insert}\label{wxarraystringinsert}
187
8a2a6bbf 188\func{void}{Insert}{\param{const wxString\& }{str}, \param{size\_t}{ nIndex}, \param{size\_t }{copies = $1$}}
9e2be6f0 189
8a2a6bbf 190Insert the given number of {\it copies} of the new element in the array before the position {\it nIndex}. Thus, for
9e2be6f0
VZ
191example, to insert the string in the beginning of the array you would write
192
193\begin{verbatim}
194Insert("foo", 0);
195\end{verbatim}
196
d3f7a53b 197If {\it nIndex} is equal to {\it GetCount()} this function behaves as
9e2be6f0
VZ
198\helpref{Add}{wxarraystringadd}.
199
fa482912 200{\bf Warning:} this function should not be used with sorted arrays because it
e87271f3 201could break the order of items and, for example, subsequent calls to
fa482912 202\helpref{Index()}{wxarraystringindex} would then not work!
e87271f3 203
9e2be6f0
VZ
204\membersection{wxArrayString::IsEmpty}\label{wxarraystringisempty}
205
0e10e38d 206\func{bool}{IsEmpty}{}
9e2be6f0 207
cc81d32f 208Returns true if the array is empty, false otherwise. This function returns the
9e2be6f0
VZ
209same result as {\it GetCount() == 0} but is probably easier to read.
210
211\membersection{wxArrayString::Item}\label{wxarraystringitem}
212
213\constfunc{wxString\&}{Item}{\param{size\_t }{nIndex}}
214
215Return the array element at position {\it nIndex}. An assert failure will
216result from an attempt to access an element beyond the end of array in debug
217mode, but no check is done in release mode.
218
219See also \helpref{operator[]}{wxarraystringoperatorindex} for the operator
220version.
221
222\membersection{wxArrayString::Last}\label{wxarraystringlast}
223
0e10e38d 224\func{wxString&}{Last}{}
9e2be6f0
VZ
225
226Returns the last element of the array. Attempt to access the last element of
227an empty array will result in assert failure in debug build, however no checks
228are done in release mode.
229
f6bcfd97 230\membersection{wxArrayString::Remove}\label{wxarraystringremove}
9e2be6f0
VZ
231
232\func{void}{Remove}{\param{const char *}{ sz}}
233
234Removes the first item matching this value. An assert failure is provoked by
235an attempt to remove an element which does not exist in debug build.
236
f6bcfd97 237See also: \helpref{Index}{wxarraystringindex}
9e2be6f0 238
d4c92f55
MB
239\membersection{wxArrayString::RemoveAt}\label{wxarraystringremoveat}
240
241\func{void}{RemoveAt}{\param{size\_t }{nIndex}, \param{size\_t }{count = $1$}}
9e2be6f0 242
8a2a6bbf 243Removes {\it count} items starting at position {\it nIndex} from the array.
9e2be6f0 244
9e2be6f0
VZ
245\membersection{wxArrayString::Shrink}\label{wxarraystringshrink}
246
247\func{void}{Shrink}{\void}
248
249Releases the extra memory allocated by the array. This function is useful to
250minimize the array memory consumption.
251
252See also: \helpref{Alloc}{wxarraystringalloc}, \helpref{Dynamic array memory management}{wxarraymemorymanagement}
253
f6bcfd97 254\membersection{wxArrayString::Sort}\label{wxarraystringsort}
9e2be6f0 255
cc81d32f 256\func{void}{Sort}{\param{bool}{ reverseOrder = false}}
9e2be6f0 257
fa482912 258Sorts the array in alphabetical order or in reverse alphabetical order if
2f930c85 259{\it reverseOrder} is true. The sort is case-sensitive.
9e2be6f0 260
e87271f3
VZ
261{\bf Warning:} this function should not be used with sorted array because it
262could break the order of items and, for example, subsequent calls to
fa482912 263\helpref{Index()}{wxarraystringindex} would then not work!
e87271f3 264
9e2be6f0
VZ
265\func{void}{Sort}{\param{CompareFunction }{compareFunction}}
266
267Sorts the array using the specified {\it compareFunction} for item comparison.
268{\it CompareFunction} is defined as a function taking two {\it const
fa482912 269wxString\&} parameters and returning an {\it int} value less than, equal to or
9e2be6f0
VZ
270greater than 0 if the first string is less than, equal to or greater than the
271second one.
272
2a47d3c1 273\wxheading{Example}
9e2be6f0 274
2a47d3c1 275The following example sorts strings by their length.
9e2be6f0 276
2a47d3c1 277\begin{verbatim}
9e2be6f0
VZ
278static int CompareStringLen(const wxString& first, const wxString& second)
279{
280 return first.length() - second.length();
281}
282
283...
284
285wxArrayString array;
286
287array.Add("one");
288array.Add("two");
289array.Add("three");
290array.Add("four");
291
292array.Sort(CompareStringLen);
9e2be6f0
VZ
293\end{verbatim}
294
e87271f3
VZ
295{\bf Warning:} this function should not be used with sorted array because it
296could break the order of items and, for example, subsequent calls to
fa482912 297\helpref{Index()}{wxarraystringindex} would then not work!
e87271f3 298