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