File: wx28.htm

package info (click to toggle)
wxwin2-doc 2.01-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,540 kB
  • ctags: 5,968
  • sloc: cpp: 15,157; makefile: 434; sh: 6
file content (270 lines) | stat: -rw-r--r-- 11,202 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<HTML>
<head><title>wxArrayString</title></head>

<BODY BGCOLOR=#FFFFFF>
<A NAME="wxarraystring"></A><CENTER>
<A HREF="wx.htm"><img align=center src="contents.gif" BORDER=0 ALT="Contents"></A> <A HREF="wx22.htm#classref"><img align=center src="up.gif" BORDER=0 ALT="Up"></A> <A HREF="wx27.htm#wxarray"><img align=center src="back.gif" BORDER=0 ALT="Previous"></A> <A HREF="wx29.htm#wxautomationobject"><img align=center src="forward.gif" BORDER=0 ALT="Next"></A> </CENTER><HR>

<H2>wxArrayString</H2>
<P>
wxArrayString is an efficient container for storing 
<A HREF="wx224.htm#wxstring">wxString</A> objects. It has the same features as all 
<A HREF="wx27.htm#wxarray">wxArray</A> classes, i.e. it dynamically expands when new items
are added to it (so it is as easy to use as a linked list), but the access
time to the elements is constant, instead of being linear in number of
elements as in the case of linked lists. It is also very size efficient and
doesn't take more space than a C array <I>wxString[]</I> type. wxArrayString
uses its knowledge of internals of wxString class to achieve this.<P>
This class is used in the same way as other dynamic <A HREF="wx27.htm#wxarray">arrays</A>,
except that no <I>WX_DEFINE_ARRAY</I> declaration is needed for it. When a
string is added or inserted in the array, a copy of the string is created, so
the original string may be safely deleted (e.g. if it was a <I>char *</I> 
pointer the memory it was using can be freed immediately after this). In
general, there is no need to worry about string memory deallocation when using
this class - it will always free the memory it uses itself.<P>
The references returned by <A HREF="wx28.htm#wxarraystringitem">Item</A>, 
<A HREF="wx28.htm#wxarraystringlast">Last</A> or 
<A HREF="wx28.htm#wxarraystringoperatorindex">operator[]</A> are not constant, so the
array elements may be modified in place like this<P>
<PRE>
    array.Last().MakeUpper();
</PRE>
Finally, none of the methods of this class is virtual including its
destructor, so this class should not be derived from.<P>
<B><FONT COLOR="#FF0000">Derived from</FONT></B><P>
Although this is not true strictly speaking, this class may be considered as a
specialization of <A HREF="wx27.htm#wxarray">wxArray</A> class for the wxString member
data: it is not implemented like this, but it does have all of the wxArray
functions.<P>
<B><FONT COLOR="#FF0000">Include files</FONT></B><P>
&lt;wx/string.h&gt;<P>
<B><FONT COLOR="#FF0000">See also</FONT></B><P>
<A HREF="wx27.htm#wxarray">wxArray</A>, <A HREF="wx224.htm#wxstring">wxString</A>, <A HREF="wx280.htm#wxstringoverview">wxString overview</A><P>
<B><FONT COLOR="#FF0000">Members</FONT></B><P>
<A HREF="#wxarraystringctor">wxArrayString::wxArrayString</A><BR>
<A HREF="#wxarraystringdtor">wxArrayString::~wxArrayString</A><BR>
<A HREF="#wxarraystringoperatorassign">wxArrayString::operator=</A><BR>
<A HREF="#wxarraystringoperatorindex">wxArrayString::operator[]</A><BR>
<A HREF="#wxarraystringadd">wxArrayString::Add</A><BR>
<A HREF="#wxarraystringalloc">wxArrayString::Alloc</A><BR>
<A HREF="#wxarraystringclear">wxArrayString::Clear</A><BR>
<A HREF="#wxarraystringcount">wxArrayString::Count</A><BR>
<A HREF="#wxarraystringempty">wxArrayString::Empty</A><BR>
<A HREF="#wxarraystringgetcount">wxArrayString::GetCount</A><BR>
<A HREF="#wxarraystringindex">wxArrayString::Index</A><BR>
<A HREF="#wxarraystringinsert">wxArrayString::Insert</A><BR>
<A HREF="#wxarraystringisempty">wxArrayString::IsEmpty</A><BR>
<A HREF="#wxarraystringitem">wxArrayString::Item</A><BR>
<A HREF="#wxarraystringlast">wxArrayString::Last</A><BR>
<A HREF="#wxarraystringremoveval">wxArrayString::Remove (by value)</A><BR>
<A HREF="#wxarraystringremove">wxArrayString::Remove (by index)</A><BR>
<A HREF="#wxarraystringshrink">wxArrayString::Shrink</A><BR>
<A HREF="#wxarraystringsort">wxArrayString::Sort (alphabetically)</A><BR>
<A HREF="#wxarraystringsortcallback">wxArrayString::Sort (user defined)</A><BR>
<P>

<HR>
<A NAME="wxarraystringctor"></A>
<H3>wxArrayString::wxArrayString</H3>
<P>
<B></B> <B>wxArrayString</B>()<P>
<B></B> <B>wxArrayString</B>(<B>const wxArrayString&amp;</B><I> array</I>)<P>
Default and copy constructors.<P>

<HR>
<A NAME="wxarraystringdtor"></A>
<H3>wxArrayString::~wxArrayString</H3>
<P>
<B></B> <B>~wxArrayString</B>()<P>
Destructor frees memory occupied by the array strings. For the performance
reasons it is not virtual, so this class should not be derived from.<P>

<HR>
<A NAME="wxarraystringoperatorassign"></A>
<H3>wxArrayString::operator=</H3>
<P>
<B>wxArrayString &amp;</B> <B>operator =</B>(<B>const wxArrayString&amp;</B><I> array</I>)<P>
Assignment operator.<P>

<HR>
<A NAME="wxarraystringoperatorindex"></A>
<H3>wxArrayString::operator[]</H3>
<P>
<B>wxString&amp;</B> <B>operatorp[]</B>(<B>size_t </B><I>nIndex</I>)<P>
Return the array element at position <I>nIndex</I>. An assert failure will
result from an attempt to access an element beyond the end of array in debug
mode, but no check is done in release mode.<P>
This is the operator version of <A HREF="wx28.htm#wxarraystringitem">Item</A> method.<P>

<HR>
<A NAME="wxarraystringadd"></A>
<H3>wxArrayString::Add</H3>
<P>
<B>void</B> <B>Add</B>(<B>const wxString&amp; </B><I>str</I>)<P>
Appends a new item to the array.<P>
See also: <A HREF="wx28.htm#wxarraystringinsert">Insert</A><P>

<HR>
<A NAME="wxarraystringalloc"></A>
<H3>wxArrayString::Alloc</H3>
<P>
<B>void</B> <B>Alloc</B>(<B>size_t </B><I>nCount</I>)<P>
Preallocates enough memory to store <I>nCount</I> items. This function may be
used to improve array class performance before adding a known number of items
consecutively.<P>
See also: <A HREF="wx27.htm#wxarraymemorymanagement">Dynamic array memory management</A><P>

<HR>
<A NAME="wxarraystringclear"></A>
<H3>wxArrayString::Clear</H3>
<P>
<B>void</B> <B>Clear</B>()<P>
Clears the array contents and frees memory.<P>
See also: <A HREF="wx28.htm#wxarraystringempty">Empty</A><P>

<HR>
<A NAME="wxarraystringcount"></A>
<H3>wxArrayString::Count</H3>
<P>
<B>size_t</B> <B>Count</B>() <B>const</B><P>
Returns the number of items in the array. This function is deprecated and is
for backwards compatibility only, please use 
<A HREF="wx28.htm#wxarraystringgetcount">GetCount</A> instead.<P>

<HR>
<A NAME="wxarraystringempty"></A>
<H3>wxArrayString::Empty</H3>
<P>
<B>void</B> <B>Empty</B>()<P>
Empties the array: after a call to this function 
<A HREF="wx28.htm#wxarraystringgetcount">GetCount</A> will return 0. However, this
function does not free the memory used by the array and so should be used when
the array is going to be reused for storing other strings. Otherwise, you
should use <A HREF="wx28.htm#wxarraystringclear">Clear</A> to empty the array and free
memory.<P>

<HR>
<A NAME="wxarraystringgetcount"></A>
<H3>wxArrayString::GetCount</H3>
<P>
<B>size_t</B> <B>GetCount</B>() <B>const</B><P>
Returns the number of items in the array.<P>

<HR>
<A NAME="wxarraystringindex"></A>
<H3>wxArrayString::Index</H3>
<P>
<B>int</B> <B>Index</B>(<B>const char *</B><I> sz</I>, <B>bool</B><I> bCase = TRUE</I>, <B>bool</B><I> bFromEnd = FALSE</I>)<P>
Search the element in the array, starting from the beginning if
<I>bFromEnd</I> is FALSE or from end otherwise. If <I>bCase</I>, comparison is
case sensitive (default), otherwise the case is ignored.<P>
Returns index of the first item matched or wxNOT_FOUND if there is no match.<P>

<HR>
<A NAME="wxarraystringinsert"></A>
<H3>wxArrayString::Insert</H3>
<P>
<B>void</B> <B>Insert</B>(<B>const wxString&amp; </B><I>str</I>, <B>size_t</B><I> nIndex</I>)<P>
Insert a new element in the array before the position <I>nIndex</I>. Thus, for
example, to insert the string in the beginning of the array you would write<P>
<PRE>
Insert("foo", 0);
</PRE>
If <I>nIndex</I> is equal to <I>GetCount() + 1</I> this function behaves as 
<A HREF="wx28.htm#wxarraystringadd">Add</A>.<P>

<HR>
<A NAME="wxarraystringisempty"></A>
<H3>wxArrayString::IsEmpty</H3>
<P>
<B></B> <B>IsEmpty</B>()<P>
Returns TRUE if the array is empty, FALSE otherwise. This function returns the
same result as <I>GetCount() == 0</I> but is probably easier to read.<P>

<HR>
<A NAME="wxarraystringitem"></A>
<H3>wxArrayString::Item</H3>
<P>
<B>wxString&amp;</B> <B>Item</B>(<B>size_t </B><I>nIndex</I>) <B>const</B><P>
Return the array element at position <I>nIndex</I>. An assert failure will
result from an attempt to access an element beyond the end of array in debug
mode, but no check is done in release mode.<P>
See also <A HREF="wx28.htm#wxarraystringoperatorindex">operator[]</A> for the operator
version.<P>

<HR>
<A NAME="wxarraystringlast"></A>
<H3>wxArrayString::Last</H3>
<P>
<B></B> <B>Last</B>()<P>
Returns the last element of the array. Attempt to access the last element of
an empty array will result in assert failure in debug build, however no checks
are done in release mode.<P>

<HR>
<A NAME="wxarraystringremoveval"></A>
<H3>wxArrayString::Remove (by value)</H3>
<P>
<B>void</B> <B>Remove</B>(<B>const char *</B><I> sz</I>)<P>
Removes the first item matching this value. An assert failure is provoked by
an attempt to remove an element which does not exist in debug build.<P>
See also: <A HREF="wx28.htm#wxarraystringindex">Index</A>, <A HREF="wx28.htm#wxarraystringremove">Remove</A><P>

<HR>
<A NAME="wxarraystringremove"></A>
<H3>wxArrayString::Remove (by index)</H3>
<P>
<B>void</B> <B>Remove</B>(<B>size_t </B><I>nIndex</I>)<P>
Removes the item at given position.<P>
See also: <A HREF="wx28.htm#wxarraystringremoveval">Remove</A><P>

<HR>
<A NAME="wxarraystringshrink"></A>
<H3>wxArrayString::Shrink</H3>
<P>
<B>void</B> <B>Shrink</B>()<P>
Releases the extra memory allocated by the array. This function is useful to
minimize the array memory consumption.<P>
See also: <A HREF="wx28.htm#wxarraystringalloc">Alloc</A>, <A HREF="wx27.htm#wxarraymemorymanagement">Dynamic array memory management</A><P>

<HR>
<A NAME="wxarraystringsort"></A>
<H3>wxArrayString::Sort (alphabetically)</H3>
<P>
<B>void</B> <B>Sort</B>(<B>bool</B><I> reverseOrder = FALSE</I>)<P>
Sorts the array in alphabetical order or in reverse alphabetical order if
<I>reverseOrder</I> is TRUE.<P>
See also: <A HREF="wx28.htm#wxarraystringsortcallback">Sort</A><P>

<HR>
<A NAME="wxarraystringsortcallback"></A>
<H3>wxArrayString::Sort (user defined)</H3>
<P>
<B>void</B> <B>Sort</B>(<B>CompareFunction </B><I>compareFunction</I>)<P>
Sorts the array using the specified <I>compareFunction</I> for item comparison.
<I>CompareFunction</I> is defined as a function taking two <I>const
wxString&amp;</I> parameters and returning <I>int</I> value less than, equal to or
greater than 0 if the first string is less than, equal to or greater than the
second one.<P>
<B><FONT COLOR="#FF0000">Example</FONT></B><P>
The following example sorts strings by their length.<P>
<PRE>
static int CompareStringLen(const wxString& first, const wxString& second)
{
    return first.length() - second.length();
}

...

wxArrayString array;

array.Add("one");
array.Add("two");
array.Add("three");
array.Add("four");

array.Sort(CompareStringLen);
</PRE>
See also: <A HREF="wx28.htm#wxarraystringsort">Sort</A><P>

</BODY></HTML>