source: trunk/doc/src/snippets/code/doc_src_containers.qdoc@ 865

Last change on this file since 865 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41//! [0]
42class Employee
43{
44public:
45 Employee() {}
46 Employee(const Employee &other);
47
48 Employee &operator=(const Employee &other);
49
50private:
51 QString myName;
52 QDate myDateOfBirth;
53};
54//! [0]
55
56
57//! [1]
58QList<QString> list;
59list << "A" << "B" << "C" << "D";
60
61QListIterator<QString> i(list);
62while (i.hasNext())
63 qDebug() << i.next();
64//! [1]
65
66
67//! [2]
68QListIterator<QString> i(list);
69i.toBack();
70while (i.hasPrevious())
71 qDebug() << i.previous();
72//! [2]
73
74
75//! [3]
76QMutableListIterator<int> i(list);
77while (i.hasNext()) {
78 if (i.next() % 2 != 0)
79 i.remove();
80}
81//! [3]
82
83
84//! [4]
85QMutableListIterator<int> i(list);
86i.toBack();
87while (i.hasPrevious()) {
88 if (i.previous() % 2 != 0)
89 i.remove();
90}
91//! [4]
92
93
94//! [5]
95QMutableListIterator<int> i(list);
96while (i.hasNext()) {
97 if (i.next() > 128)
98 i.setValue(128);
99}
100//! [5]
101
102
103//! [6]
104QMutableListIterator<int> i(list);
105while (i.hasNext())
106 i.next() *= 2;
107//! [6]
108
109
110//! [7]
111QMap<QString, QString> map;
112map.insert("Paris", "France");
113map.insert("Guatemala City", "Guatemala");
114map.insert("Mexico City", "Mexico");
115map.insert("Moscow", "Russia");
116...
117
118QMutableMapIterator<QString, QString> i(map);
119while (i.hasNext()) {
120 if (i.next().key().endsWith("City"))
121 i.remove();
122}
123//! [7]
124
125
126//! [8]
127QMap<int, QWidget *> map;
128QHash<int, QWidget *> hash;
129
130QMapIterator<int, QWidget *> i(map);
131while (i.hasNext()) {
132 i.next();
133 hash.insert(i.key(), i.value());
134}
135//! [8]
136
137
138//! [9]
139QMutableMapIterator<int, QWidget *> i(map);
140while (i.findNext(widget))
141 i.remove();
142//! [9]
143
144
145//! [10]
146QList<QString> list;
147list << "A" << "B" << "C" << "D";
148
149QList<QString>::iterator i;
150for (i = list.begin(); i != list.end(); ++i)
151 *i = (*i).toLower();
152//! [10]
153
154
155//! [11]
156QList<QString> list;
157list << "A" << "B" << "C" << "D";
158
159QList<QString>::iterator i = list.end();
160while (i != list.begin()) {
161 --i;
162 *i = (*i).toLower();
163}
164//! [11]
165
166
167//! [12]
168QList<QString>::const_iterator i;
169for (i = list.constBegin(); i != list.constEnd(); ++i)
170 qDebug() << *i;
171//! [12]
172
173
174//! [13]
175QMap<int, int> map;
176...
177QMap<int, int>::const_iterator i;
178for (i = map.constBegin(); i != map.constEnd(); ++i)
179 qDebug() << i.key() << ":" << i.value();
180//! [13]
181
182
183//! [14]
184// RIGHT
185const QList<int> sizes = splitter->sizes();
186QList<int>::const_iterator i;
187for (i = sizes.begin(); i != sizes.end(); ++i)
188 ...
189
190// WRONG
191QList<int>::const_iterator i;
192for (i = splitter->sizes().begin();
193 i != splitter->sizes().end(); ++i)
194 ...
195//! [14]
196
197
198//! [15]
199QLinkedList<QString> list;
200...
201QString str;
202foreach (str, list)
203 qDebug() << str;
204//! [15]
205
206
207//! [16]
208QLinkedList<QString> list;
209...
210QLinkedListIterator<QString> i(list);
211while (i.hasNext())
212 qDebug() << i.next();
213//! [16]
214
215
216//! [17]
217QLinkedList<QString> list;
218...
219foreach (const QString &str, list)
220 qDebug() << str;
221//! [17]
222
223
224//! [18]
225QLinkedList<QString> list;
226...
227foreach (const QString &str, list) {
228 if (str.isEmpty())
229 break;
230 qDebug() << str;
231}
232//! [18]
233
234
235//! [19]
236QMap<QString, int> map;
237...
238foreach (const QString &str, map.keys())
239 qDebug() << str << ":" << map.value(str);
240//! [19]
241
242
243//! [20]
244QMultiMap<QString, int> map;
245...
246foreach (const QString &str, map.uniqueKeys()) {
247 foreach (int i, map.values(str))
248 qDebug() << str << ":" << i;
249}
250//! [20]
251
252
253//! [21]
254forever {
255 ...
256}
257//! [21]
258
259
260//! [22]
261CONFIG += no_keywords
262//! [22]
263
264
265//! [23]
266QString onlyLetters(const QString &in)
267{
268 QString out;
269 for (int j = 0; j < in.size(); ++j) {
270 if (in[j].isLetter())
271 out += in[j];
272 }
273 return out;
274}
275//! [23]
Note: See TracBrowser for help on using the repository browser.