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

Last change on this file since 769 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 5.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42//! [0]
43class Employee
44{
45public:
46 Employee() {}
47 Employee(const Employee &other);
48
49 Employee &operator=(const Employee &other);
50
51private:
52 QString myName;
53 QDate myDateOfBirth;
54};
55//! [0]
56
57
58//! [1]
59QList<QString> list;
60list << "A" << "B" << "C" << "D";
61
62QListIterator<QString> i(list);
63while (i.hasNext())
64 qDebug() << i.next();
65//! [1]
66
67
68//! [2]
69QListIterator<QString> i(list);
70i.toBack();
71while (i.hasPrevious())
72 qDebug() << i.previous();
73//! [2]
74
75
76//! [3]
77QMutableListIterator<int> i(list);
78while (i.hasNext()) {
79 if (i.next() % 2 != 0)
80 i.remove();
81}
82//! [3]
83
84
85//! [4]
86QMutableListIterator<int> i(list);
87i.toBack();
88while (i.hasPrevious()) {
89 if (i.previous() % 2 != 0)
90 i.remove();
91}
92//! [4]
93
94
95//! [5]
96QMutableListIterator<int> i(list);
97while (i.hasNext()) {
98 if (i.next() > 128)
99 i.setValue(128);
100}
101//! [5]
102
103
104//! [6]
105QMutableListIterator<int> i(list);
106while (i.hasNext())
107 i.next() *= 2;
108//! [6]
109
110
111//! [7]
112QMap<QString, QString> map;
113map.insert("Paris", "France");
114map.insert("Guatemala City", "Guatemala");
115map.insert("Mexico City", "Mexico");
116map.insert("Moscow", "Russia");
117...
118
119QMutableMapIterator<QString, QString> i(map);
120while (i.hasNext()) {
121 if (i.next().key().endsWith("City"))
122 i.remove();
123}
124//! [7]
125
126
127//! [8]
128QMap<int, QWidget *> map;
129QHash<int, QWidget *> hash;
130
131QMapIterator<int, QWidget *> i(map);
132while (i.hasNext()) {
133 i.next();
134 hash.insert(i.key(), i.value());
135}
136//! [8]
137
138
139//! [9]
140QMutableMapIterator<int, QWidget *> i(map);
141while (i.findNext(widget))
142 i.remove();
143//! [9]
144
145
146//! [10]
147QList<QString> list;
148list << "A" << "B" << "C" << "D";
149
150QList<QString>::iterator i;
151for (i = list.begin(); i != list.end(); ++i)
152 *i = (*i).toLower();
153//! [10]
154
155
156//! [11]
157QList<QString> list;
158list << "A" << "B" << "C" << "D";
159
160QList<QString>::iterator i = list.end();
161while (i != list.begin()) {
162 --i;
163 *i = (*i).toLower();
164}
165//! [11]
166
167
168//! [12]
169QList<QString>::const_iterator i;
170for (i = list.constBegin(); i != list.constEnd(); ++i)
171 qDebug() << *i;
172//! [12]
173
174
175//! [13]
176QMap<int, int> map;
177...
178QMap<int, int>::const_iterator i;
179for (i = map.constBegin(); i != map.constEnd(); ++i)
180 qDebug() << i.key() << ":" << i.value();
181//! [13]
182
183
184//! [14]
185// RIGHT
186const QList<int> sizes = splitter->sizes();
187QList<int>::const_iterator i;
188for (i = sizes.begin(); i != sizes.end(); ++i)
189 ...
190
191// WRONG
192QList<int>::const_iterator i;
193for (i = splitter->sizes().begin();
194 i != splitter->sizes().end(); ++i)
195 ...
196//! [14]
197
198
199//! [15]
200QLinkedList<QString> list;
201...
202QString str;
203foreach (str, list)
204 qDebug() << str;
205//! [15]
206
207
208//! [16]
209QLinkedList<QString> list;
210...
211QLinkedListIterator<QString> i(list);
212while (i.hasNext())
213 qDebug() << i.next();
214//! [16]
215
216
217//! [17]
218QLinkedList<QString> list;
219...
220foreach (QString str, list)
221 qDebug() << str;
222//! [17]
223
224
225//! [18]
226QLinkedList<QString> list;
227...
228foreach (QString str, list) {
229 if (str.isEmpty())
230 break;
231 qDebug() << str;
232}
233//! [18]
234
235
236//! [19]
237QMap<QString, int> map;
238...
239foreach (QString str, map.keys())
240 qDebug() << str << ":" << map.value(str);
241//! [19]
242
243
244//! [20]
245QMultiMap<QString, int> map;
246...
247foreach (QString str, map.uniqueKeys()) {
248 foreach (int i, map.values(str))
249 qDebug() << str << ":" << i;
250}
251//! [20]
252
253
254//! [21]
255forever {
256 ...
257}
258//! [21]
259
260
261//! [22]
262CONFIG += no_keywords
263//! [22]
264
265
266//! [23]
267QString onlyLetters(const QString &in)
268{
269 QString out;
270 for (int j = 0; j < in.size(); ++j) {
271 if (in[j].isLetter())
272 out += in[j];
273 }
274 return out;
275}
276//! [23]
Note: See TracBrowser for help on using the repository browser.