Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_corelib_tools_qlist.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QList>
5#include <iostream>
6#include <QDebug>
7
8using namespace std;
9
11{
12 {
13 //! [0]
14 QList<int> integerList;
15 QList<QString> stringList;
16 //! [0]
17 }
18
19 {
20 //! [1]
21 QList<QString> list(200);
22 //! [1]
23 }
24
25 {
26 //! [2]
27 QList<QString> list(200, "Pass");
28 //! [2]
29 }
30
31 {
32 QList<QString> list;
33
34 //! [3]
35 if (list[0] == "Liz")
36 list[0] = "Elizabeth";
37 //! [3]
38 }
39
40 {
41 QList<QString> list;
42
43 //! [4]
44 for (qsizetype i = 0; i < list.size(); ++i) {
45 if (list.at(i) == "Alfonso")
46 cout << "Found Alfonso at position " << i << endl;
47 }
48 //! [4]
49 }
50
51 {
52 QList<QString> list;
53
54 //! [5]
55 qsizetype i = list.indexOf("Harumi");
56 if (i != -1)
57 cout << "First occurrence of Harumi is at position " << i << endl;
58 //! [5]
59 }
60
61 {
62 //! [6]
63 QList<int> list(10);
64 int *data = list.data();
65 for (qsizetype i = 0; i < 10; ++i)
66 data[i] = 2 * i;
67 //! [6]
68 }
69
70 {
71 //! [7]
72 QList<QString> list;
73 list.append("one");
74 list.append("two");
75 QString three = "three";
76 list.append(three);
77 // list: ["one", "two", "three"]
78 // three: "three"
79 //! [7]
80 }
81
82 {
83 //! [move-append]
84 QList<QString> list;
85 list.append("one");
86 list.append("two");
87 QString three = "three";
88 list.append(std::move(three));
89 // list: ["one", "two", "three"]
90 // three: ""
91 //! [move-append]
92 }
93
94 {
95 //! [emplace]
96 QList<QString> list{"a", "ccc"};
97 list.emplace(1, 2, 'b');
98 // list: ["a", "bb", "ccc"]
99 //! [emplace]
100 }
101
102 {
103 //! [emplace-back]
104 QList<QString> list{"one", "two"};
105 list.emplaceBack(3, 'a');
106 qDebug() << list;
107 // list: ["one", "two", "aaa"]
108 //! [emplace-back]
109 }
110
111 {
112 //! [emplace-back-ref]
113 QList<QString> list;
114 auto &ref = list.emplaceBack();
115 ref = "one";
116 // list: ["one"]
117 //! [emplace-back-ref]
118 }
119
120 {
121 //! [8]
122 QList<QString> list;
123 list.prepend("one");
124 list.prepend("two");
125 list.prepend("three");
126 // list: ["three", "two", "one"]
127 //! [8]
128 }
129
130 {
131 //! [9]
132 QList<QString> list = {"alpha", "beta", "delta"};
133 list.insert(2, "gamma");
134 // list: ["alpha", "beta", "gamma", "delta"]
135 //! [9]
136 }
137
138 {
139 //! [10]
140 QList<double> list = {2.718, 1.442, 0.4342};
141 list.insert(1, 3, 9.9);
142 // list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342]
143 //! [10]
144 }
145
146 {
147 //! [11]
148 QList<QString> list(3);
149 list.fill("Yes");
150 // list: ["Yes", "Yes", "Yes"]
151
152 list.fill("oh", 5);
153 // list: ["oh", "oh", "oh", "oh", "oh"]
154 //! [11]
155 }
156
157 {
158 //! [12]
159 QList<QString> list{"A", "B", "C", "B", "A"};
160 list.indexOf("B"); // returns 1
161 list.indexOf("B", 1); // returns 1
162 list.indexOf("B", 2); // returns 3
163 list.indexOf("X"); // returns -1
164 //! [12]
165 }
166
167 {
168 //! [13]
169 QList<QString> list = {"A", "B", "C", "B", "A"};
170 list.lastIndexOf("B"); // returns 3
171 list.lastIndexOf("B", 3); // returns 3
172 list.lastIndexOf("B", 2); // returns 1
173 list.lastIndexOf("X"); // returns -1
174 //! [13]
175 }
176}
bool examples()
[3]