QtBase  v6.3.1
src_corelib_tools_qlist.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
55 
56 
58 QList<QString> list(200);
60 
61 
63 QList<QString> list(200, "Pass");
65 
66 
68 if (list[0] == "Liz")
69  list[0] = "Elizabeth";
71 
72 
74 for (qsizetype i = 0; i < list.size(); ++i) {
75  if (list.at(i) == "Alfonso")
76  cout << "Found Alfonso at position " << i << Qt::endl;
77 }
79 
80 
82 qsizetype i = list.indexOf("Harumi");
83 if (i != -1)
84  cout << "First occurrence of Harumi is at position " << i << Qt::endl;
86 
87 
90 int *data = list.data();
91 for (qsizetype i = 0; i < 10; ++i)
92  data[i] = 2 * i;
94 
95 
98 list.append("one");
99 list.append("two");
100 QString three = "three";
102 // list: ["one", "two", "three"]
103 // three: "three"
105 
106 
109 list.append("one");
110 list.append("two");
111 QString three = "three";
112 list.append(std::move(three));
113 // list: ["one", "two", "three"]
114 // three: ""
116 
117 
119 QList<QString> list{"a", "ccc"};
120 list.emplace(1, 2, 'b');
121 // list: ["a", "bb", "ccc"]
123 
124 
126 QList<QString> list{"one", "two"};
129 // list: ["one", "two", "aaa"]
131 
132 
135 auto &ref = list.emplaceBack();
136 ref = "one";
137 // list: ["one"]
139 
140 
143 list.prepend("one");
144 list.prepend("two");
145 list.prepend("three");
146 // list: ["three", "two", "one"]
148 
149 
152 list << "alpha" << "beta" << "delta";
153 list.insert(2, "gamma");
154 // list: ["alpha", "beta", "gamma", "delta"]
156 
157 
160 list << 2.718 << 1.442 << 0.4342;
161 list.insert(1, 3, 9.9);
162 // list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342]
164 
165 
168 list.fill("Yes");
169 // list: ["Yes", "Yes", "Yes"]
170 
171 list.fill("oh", 5);
172 // list: ["oh", "oh", "oh", "oh", "oh"]
174 
175 
178 list << "A" << "B" << "C" << "B" << "A";
179 list.indexOf("B"); // returns 1
180 list.indexOf("B", 1); // returns 1
181 list.indexOf("B", 2); // returns 3
182 list.indexOf("X"); // returns -1
184 
185 
188 list << "A" << "B" << "C" << "B" << "A";
189 list.lastIndexOf("B"); // returns 3
190 list.lastIndexOf("B", 3); // returns 3
191 list.lastIndexOf("B", 2); // returns 1
192 list.lastIndexOf("X"); // returns -1
qsizetype size() const noexcept
Definition: qlist.h:414
QList< T > & fill(parameter_type t, qsizetype size=-1)
Definition: qlist.h:907
iterator insert(qsizetype i, parameter_type t)
Definition: qlist.h:499
const_reference at(qsizetype i) const noexcept
Definition: qlist.h:457
reference emplaceBack(Args &&... args)
Definition: qlist.h:886
void prepend(rvalue_ref t)
Definition: qlist.h:484
iterator emplace(const_iterator before, Args &&... args)
Definition: qlist.h:528
pointer data()
Definition: qlist.h:442
void append(parameter_type t)
Definition: qlist.h:469
The QString class provides a Unicode character string.
Definition: qstring.h:388
std::ostream & cout()
QTextStream & endl(QTextStream &stream)
ptrdiff_t qsizetype
Definition: qglobal.h:308
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint ref
qsizetype i
[4]
QString three
qDebug()<< list
[1]
QList< QString > stringList
QList< int > integerList
[0]
QList< QString > list(200)
[0]
qsizetype lastIndexOf(const QString &str, qsizetype from=-1) const noexcept
Definition: qstringlist.h:160
qsizetype indexOf(const QString &str, qsizetype from=0) const noexcept
Definition: qstringlist.h:158