1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 //! [0]
42 QStringList list;
43 list << "one" << "two" << "three";
44 
45 qFill(list.begin(), list.end(), "eleven");
46 // list: [ "eleven", "eleven", "eleven" ]
47 //! [0]
48 
49 
50 //! [1]
51 qFill(list.begin() + 1, list.end(), "six");
52 // list: [ "eleven", "six", "six" ]
53 //! [1]
54 
55 
56 //! [2]
resolveEntity(const QString & entity)57 QChar resolveEntity(const QString &entity)
58 {
59     static const QLatin1String name_table[] = {
60         "AElig", "Aacute", ..., "zwnj"
61     };
62     static const ushort value_table[] = {
63         0x0061, 0x00c1, ..., 0x200c
64     };
65     int N = sizeof(name_table) / sizeof(name_table[0]);
66 
67     const QLatin1String *name = qBinaryFind(name_table, name_table + N,
68                                             entity);
69     int index = name - name_table;
70     if (index == N)
71         return QChar();
72 
73     return QChar(value_table[index]);
74 }
75 //! [2]
76 
77 
78 //! [3]
resolveEntity(const QString & entity)79 QChar resolveEntity(const QString &entity)
80 {
81     static QMap<QString, int> entityMap;
82 
83     if (!entityMap) {
84         entityMap.insert("AElig", 0x0061);
85         entityMap.insert("Aacute", 0x00c1);
86         ...
87         entityMap.insert("zwnj", 0x200c);
88     }
89     return QChar(entityMap.value(entity));
90 }
91 //! [3]
92 
93 
94 //! [4]
95 QStringList list;
96 list << "one" << "two" << "three";
97 
98 QVector<QString> vect1(3);
99 qCopy(list.begin(), list.end(), vect1.begin());
100 // vect: [ "one", "two", "three" ]
101 
102 QVector<QString> vect2(8);
103 qCopy(list.begin(), list.end(), vect2.begin() + 2);
104 // vect: [ "", "", "one", "two", "three", "", "", "" ]
105 //! [4]
106 
107 
108 //! [5]
109 QStringList list;
110 list << "one" << "two" << "three";
111 
112 QVector<QString> vect(5);
113 qCopyBackward(list.begin(), list.end(), vect.end());
114 // vect: [ "", "", "one", "two", "three" ]
115 //! [5]
116 
117 
118 //! [6]
119 QStringList list;
120 list << "one" << "two" << "three";
121 
122 QVector<QString> vect(3);
123 vect[0] = "one";
124 vect[1] = "two";
125 vect[2] = "three";
126 
127 bool ret1 = qEqual(list.begin(), list.end(), vect.begin());
128 // ret1 == true
129 
130 vect[2] = "seven";
131 bool ret2 = qEqual(list.begin(), list.end(), vect.begin());
132 // ret2 == false
133 //! [6]
134 
135 
136 //! [7]
137 QStringList list;
138 list << "one" << "two" << "three";
139 
140 qFill(list.begin(), list.end(), "eleven");
141 // list: [ "eleven", "eleven", "eleven" ]
142 
143 qFill(list.begin() + 1, list.end(), "six");
144 // list: [ "eleven", "six", "six" ]
145 //! [7]
146 
147 
148 //! [8]
149 QStringList list;
150 list << "one" << "two" << "three";
151 
152 QStringList::iterator i1 = qFind(list.begin(), list.end(), "two");
153 // i1 == list.begin() + 1
154 
155 QStringList::iterator i2 = qFind(list.begin(), list.end(), "seventy");
156 // i2 == list.end()
157 //! [8]
158 
159 
160 //! [9]
161 QList<int> list;
162 list << 3 << 3 << 6 << 6 << 6 << 8;
163 
164 int countOf6 = 0;
165 qCount(list.begin(), list.end(), 6, countOf6);
166 // countOf6 == 3
167 
168 int countOf7 = 0;
169 qCount(list.begin(), list.end(), 7, countOf7);
170 // countOf7 == 0
171 //! [9]
172 
173 
174 //! [10]
175 double pi = 3.14;
176 double e = 2.71;
177 
178 qSwap(pi, e);
179 // pi == 2.71, e == 3.14
180 //! [10]
181 
182 
183 //! [11]
184 QList<int> list;
185 list << 33 << 12 << 68 << 6 << 12;
186 qSort(list.begin(), list.end());
187 // list: [ 6, 12, 12, 33, 68 ]
188 //! [11]
189 
190 
191 //! [12]
caseInsensitiveLessThan(const QString & s1,const QString & s2)192 bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
193 {
194     return s1.toLower() < s2.toLower();
195 }
196 
doSomething()197 int doSomething()
198 {
199     QStringList list;
200     list << "AlPha" << "beTA" << "gamma" << "DELTA";
201     qSort(list.begin(), list.end(), caseInsensitiveLessThan);
202     // list: [ "AlPha", "beTA", "DELTA", "gamma" ]
203 }
204 //! [12]
205 
206 
207 //! [13]
208 QList<int> list;
209 list << 33 << 12 << 68 << 6 << 12;
210 qSort(list.begin(), list.end(), qGreater<int>());
211 // list: [ 68, 33, 12, 12, 6 ]
212 //! [13]
213 
214 
215 //! [14]
216 QStringList list;
217 list << "AlPha" << "beTA" << "gamma" << "DELTA";
218 
219 QMap<QString, QString> map;
220 foreach (const QString &str, list)
221     map.insert(str.toLower(), str);
222 
223 list = map.values();
224 //! [14]
225 
226 
227 //! [15]
228 QList<int> list;
229 list << 33 << 12 << 68 << 6 << 12;
230 qStableSort(list.begin(), list.end());
231 // list: [ 6, 12, 12, 33, 68 ]
232 //! [15]
233 
234 
235 //! [16]
caseInsensitiveLessThan(const QString & s1,const QString & s2)236 bool caseInsensitiveLessThan(const QString &s1, const QString &s2)
237 {
238     return s1.toLower() < s2.toLower();
239 }
240 
doSomething()241 int doSomething()
242 {
243     QStringList list;
244     list << "AlPha" << "beTA" << "gamma" << "DELTA";
245     qStableSort(list.begin(), list.end(), caseInsensitiveLessThan);
246     // list: [ "AlPha", "beTA", "DELTA", "gamma" ]
247 }
248 //! [16]
249 
250 
251 //! [17]
252 QList<int> list;
253 list << 33 << 12 << 68 << 6 << 12;
254 qStableSort(list.begin(), list.end(), qGreater<int>());
255 // list: [ 68, 33, 12, 12, 6 ]
256 //! [17]
257 
258 
259 //! [18]
260 QList<int> list;
261 list << 3 << 3 << 6 << 6 << 6 << 8;
262 
263 QList<int>::iterator i = qLowerBound(list.begin(), list.end(), 5);
264 list.insert(i, 5);
265 // list: [ 3, 3, 5, 6, 6, 6, 8 ]
266 
267 i = qLowerBound(list.begin(), list.end(), 12);
268 list.insert(i, 12);
269 // list: [ 3, 3, 5, 6, 6, 6, 8, 12 ]
270 //! [18]
271 
272 
273 //! [19]
274 QVector<int> vect;
275 vect << 3 << 3 << 6 << 6 << 6 << 8;
276 QVector<int>::iterator begin6 =
277 	qLowerBound(vect.begin(), vect.end(), 6);
278 QVector<int>::iterator end6 =
279 	qUpperBound(begin6, vect.end(), 6);
280 
281 QVector<int>::iterator i = begin6;
282 while (i != end6) {
283     *i = 7;
284     ++i;
285 }
286 // vect: [ 3, 3, 7, 7, 7, 8 ]
287 //! [19]
288 
289 
290 //! [20]
291 QList<int> list;
292 list << 3 << 3 << 6 << 6 << 6 << 8;
293 
294 QList<int>::iterator i = qUpperBound(list.begin(), list.end(), 5);
295 list.insert(i, 5);
296 // list: [ 3, 3, 5, 6, 6, 6, 8 ]
297 
298 i = qUpperBound(list.begin(), list.end(), 12);
299 list.insert(i, 12);
300 // list: [ 3, 3, 5, 6, 6, 6, 8, 12 ]
301 //! [20]
302 
303 
304 //! [21]
305 QVector<int> vect;
306 vect << 3 << 3 << 6 << 6 << 6 << 8;
307 QVector<int>::iterator begin6 =
308 	qLowerBound(vect.begin(), vect.end(), 6);
309 QVector<int>::iterator end6 =
310 	qUpperBound(vect.begin(), vect.end(), 6);
311 
312 QVector<int>::iterator i = begin6;
313 while (i != end6) {
314     *i = 7;
315     ++i;
316 }
317 // vect: [ 3, 3, 7, 7, 7, 8 ]
318 //! [21]
319 
320 
321 //! [22]
322 QVector<int> vect;
323 vect << 3 << 3 << 6 << 6 << 6 << 8;
324 
325 QVector<int>::iterator i =
326 	qBinaryFind(vect.begin(), vect.end(), 6);
327 // i == vect.begin() + 2 (or 3 or 4)
328 //! [22]
329 
330 
331 //! [23]
332 QList<Employee *> list;
333 list.append(new Employee("Blackpool", "Stephen"));
334 list.append(new Employee("Twist", "Oliver"));
335 
336 qDeleteAll(list.begin(), list.end());
337 list.clear();
338 //! [23]
339 
340 
341 //! [24]
342 QList<int> list;
343 list << 33 << 12 << 68 << 6 << 12;
344 qSort(list.begin(), list.end(), qLess<int>());
345 // list: [ 6, 12, 12, 33, 68 ]
346 //! [24]
347 
348 
349 //! [25]
350 QList<int> list;
351 list << 33 << 12 << 68 << 6 << 12;
352 qSort(list.begin(), list.end(), qGreater<int>());
353 // list: [ 68, 33, 12, 12, 6 ]
354 //! [25]
355