1 | //! [0]
|
---|
2 | foreach (variable, container)
|
---|
3 | statement;
|
---|
4 | //! [0]
|
---|
5 |
|
---|
6 |
|
---|
7 | //! [1]
|
---|
8 | QList<QString> list;
|
---|
9 | ...
|
---|
10 | foreach (QString str, list)
|
---|
11 | cout << str.ascii() << endl;
|
---|
12 | //! [1]
|
---|
13 |
|
---|
14 |
|
---|
15 | //! [2]
|
---|
16 | QString str;
|
---|
17 | foreach (str, list)
|
---|
18 | cout << str.ascii() << endl;
|
---|
19 | //! [2]
|
---|
20 |
|
---|
21 |
|
---|
22 | //! [3]
|
---|
23 | // forward // backward
|
---|
24 | QList<QString> list; QList<QString> list;
|
---|
25 | ... ...
|
---|
26 | QListIterator<QString> i(list); QListIterator<QString> i(list);
|
---|
27 | while (i.hasNext()) i.toBack();
|
---|
28 | cout << i.next().ascii() << endl; while (i.hasPrev())
|
---|
29 | cout << i.prev().ascii() << endl;
|
---|
30 | //! [3]
|
---|
31 |
|
---|
32 |
|
---|
33 | //! [4]
|
---|
34 | // forward // backward
|
---|
35 | QMutableListIterator<int> i(list); QMutableListIterator<int> i(list);
|
---|
36 | while (i.hasNext()) i.toBack();
|
---|
37 | if (i.next() > 128) while (i.hasPrev())
|
---|
38 | i.setValue(128); if (i.prev() > 128)
|
---|
39 | i.setValue(128);
|
---|
40 | //! [4]
|
---|
41 |
|
---|
42 |
|
---|
43 | //! [5]
|
---|
44 | // forward // backward
|
---|
45 | QMutableListIterator<int> i(list); QMutableListIterator<int> i(list);
|
---|
46 | while (i.hasNext()) i.toBack();
|
---|
47 | if (i.next() % 2 != 0) while (i.hasPrev())
|
---|
48 | i.remove(); if (i.prev() % 2 != 0)
|
---|
49 | i.remove();
|
---|
50 | //! [5]
|
---|
51 |
|
---|
52 |
|
---|
53 | //! [6]
|
---|
54 | // STL-style // Java-style
|
---|
55 | QMap<int, QWidget *>::const_iterator i; QMapIterator<int, QWidget *> i(map);
|
---|
56 | for (i = map.begin(); i != map.end(); ++i) while (i.findNext(widget))
|
---|
57 | if (i.value() == widget) cout << "Found widget " << widget
|
---|
58 | cout << "Found widget " << widget << " under key "
|
---|
59 | << " under key " << i.key() << endl;
|
---|
60 | << i.key() << endl;
|
---|
61 | //! [6]
|
---|
62 |
|
---|
63 |
|
---|
64 | //! [7]
|
---|
65 | // STL-style // Java-style
|
---|
66 | QList<int>::iterator i = list.begin(); QMutableListIterator<int> i(list);
|
---|
67 | while (i != list.end()) { while (i.hasNext()) {
|
---|
68 | if (*i == 0) { int val = i.next();
|
---|
69 | i = list.erase(i); if (val < 0)
|
---|
70 | } else { i.setValue(-val);
|
---|
71 | if (*i < 0) else if (val == 0)
|
---|
72 | *i = -*i; i.remove();
|
---|
73 | ++i; }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | //! [7]
|
---|
77 |
|
---|
78 |
|
---|
79 | //! [8]
|
---|
80 | QList<double> list;
|
---|
81 | ...
|
---|
82 | for (int i = 0; i < list.size(); ++i) {
|
---|
83 | if (list[i] < 0.0)
|
---|
84 | list[i] = 0.0;
|
---|
85 | }
|
---|
86 | //! [8]
|
---|
87 |
|
---|
88 |
|
---|
89 | //! [9]
|
---|
90 | QMap<QString, int> map;
|
---|
91 | ...
|
---|
92 | map.value("TIMEOUT", 30); // returns 30 if "TIMEOUT" isn't in the map
|
---|
93 | //! [9]
|
---|
94 |
|
---|
95 |
|
---|
96 | //! [10]
|
---|
97 | QMultiMap<QString, int> map;
|
---|
98 | ...
|
---|
99 | QList<int> values = map.values("TIMEOUT");
|
---|
100 | //! [10]
|
---|