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