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

Last change on this file since 244 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 3.5 KB
Line 
1//! [0]
2class Employee
3{
4public:
5 Employee() {}
6 Employee(const Employee &other);
7
8 Employee &operator=(const Employee &other);
9
10private:
11 QString myName;
12 QDate myDateOfBirth;
13};
14//! [0]
15
16
17//! [1]
18QList<QString> list;
19list << "A" << "B" << "C" << "D";
20
21QListIterator<QString> i(list);
22while (i.hasNext())
23 qDebug() << i.next();
24//! [1]
25
26
27//! [2]
28QListIterator<QString> i(list);
29i.toBack();
30while (i.hasPrevious())
31 qDebug() << i.previous();
32//! [2]
33
34
35//! [3]
36QMutableListIterator<int> i(list);
37while (i.hasNext()) {
38 if (i.next() % 2 != 0)
39 i.remove();
40}
41//! [3]
42
43
44//! [4]
45QMutableListIterator<int> i(list);
46i.toBack();
47while (i.hasPrevious()) {
48 if (i.previous() % 2 != 0)
49 i.remove();
50}
51//! [4]
52
53
54//! [5]
55QMutableListIterator<int> i(list);
56while (i.hasNext()) {
57 if (i.next() > 128)
58 i.setValue(128);
59}
60//! [5]
61
62
63//! [6]
64QMutableListIterator<int> i(list);
65while (i.hasNext())
66 i.next() *= 2;
67//! [6]
68
69
70//! [7]
71QMap<QString, QString> map;
72map.insert("Paris", "France");
73map.insert("Guatemala City", "Guatemala");
74map.insert("Mexico City", "Mexico");
75map.insert("Moscow", "Russia");
76...
77
78QMutableMapIterator<QString, QString> i(map);
79while (i.hasNext()) {
80 if (i.next().key().endsWith("City"))
81 i.remove();
82}
83//! [7]
84
85
86//! [8]
87QMap<int, QWidget *> map;
88QHash<int, QWidget *> hash;
89
90QMapIterator<int, QWidget *> i(map);
91while (i.hasNext()) {
92 i.next();
93 hash.insert(i.key(), i.value());
94}
95//! [8]
96
97
98//! [9]
99QMutableMapIterator<int, QWidget *> i(map);
100while (i.findNext(widget))
101 i.remove();
102//! [9]
103
104
105//! [10]
106QList<QString> list;
107list << "A" << "B" << "C" << "D";
108
109QList<QString>::iterator i;
110for (i = list.begin(); i != list.end(); ++i)
111 *i = (*i).toLower();
112//! [10]
113
114
115//! [11]
116QList<QString> list;
117list << "A" << "B" << "C" << "D";
118
119QList<QString>::iterator i = list.end();
120while (i != list.begin()) {
121 --i;
122 *i = (*i).toLower();
123}
124//! [11]
125
126
127//! [12]
128QList<QString>::const_iterator i;
129for (i = list.constBegin(); i != list.constEnd(); ++i)
130 qDebug() << *i;
131//! [12]
132
133
134//! [13]
135QMap<int, int> map;
136...
137QMap<int, int>::const_iterator i;
138for (i = map.constBegin(); i != map.constEnd(); ++i)
139 qDebug() << i.key() << ":" << i.value();
140//! [13]
141
142
143//! [14]
144// RIGHT
145const QList<int> sizes = splitter->sizes();
146QList<int>::const_iterator i;
147for (i = sizes.begin(); i != sizes.end(); ++i)
148 ...
149
150// WRONG
151QList<int>::const_iterator i;
152for (i = splitter->sizes().begin();
153 i != splitter->sizes().end(); ++i)
154 ...
155//! [14]
156
157
158//! [15]
159QLinkedList<QString> list;
160...
161QString str;
162foreach (str, list)
163 qDebug() << str;
164//! [15]
165
166
167//! [16]
168QLinkedList<QString> list;
169...
170QLinkedListIterator<QString> i(list);
171while (i.hasNext())
172 qDebug() << i.next();
173//! [16]
174
175
176//! [17]
177QLinkedList<QString> list;
178...
179foreach (QString str, list)
180 qDebug() << str;
181//! [17]
182
183
184//! [18]
185QLinkedList<QString> list;
186...
187foreach (QString str, list) {
188 if (str.isEmpty())
189 break;
190 qDebug() << str;
191}
192//! [18]
193
194
195//! [19]
196QMap<QString, int> map;
197...
198foreach (QString str, map.keys())
199 qDebug() << str << ":" << map.value(str);
200//! [19]
201
202
203//! [20]
204QMultiMap<QString, int> map;
205...
206foreach (QString str, map.uniqueKeys()) {
207 foreach (int i, map.values(str))
208 qDebug() << str << ":" << i;
209}
210//! [20]
211
212
213//! [21]
214forever {
215 ...
216}
217//! [21]
218
219
220//! [22]
221CONFIG += no_keywords
222//! [22]
223
224
225//! [23]
226QString onlyLetters(const QString &in)
227{
228 QString out;
229 for (int j = 0; j < in.size(); ++j) {
230 if (in[j].isLetter())
231 out += in[j];
232 }
233 return out;
234}
235//! [23]
Note: See TracBrowser for help on using the repository browser.