~gabriel1984sibiu/minitube/qt5.6

« back to all changes in this revision

Viewing changes to src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp

  • Committer: Grevutiu Gabriel
  • Date: 2017-06-13 08:43:17 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20170613084317-ek0zqe0u9g3ocvi8
OriginalĀ upstreamĀ code

Show diffs side-by-side

added added

removed removed

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