diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2020-06-26 12:17:38 +0200 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2020-06-29 18:00:13 +0200 |
commit | c70c4e42665eb34e677fc51a49552c9af3f58d7a (patch) | |
tree | 0e7a70f5104025a3cd1ec33db746989415f7bb83 /src/corelib/doc | |
parent | f3c7d22dd04afe8d889585fb5d6426f3d4591e74 (diff) |
Use QList instead of QVector in corelib docs
Task-number: QTBUG-84469
Task-number: QTBUG-85221
Change-Id: Ieb0ba7d82409e3c053a5788a01e92ea495505643
Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Diffstat (limited to 'src/corelib/doc')
10 files changed, 209 insertions, 554 deletions
diff --git a/src/corelib/doc/snippets/code/doc_src_containers.cpp b/src/corelib/doc/snippets/code/doc_src_containers.cpp index 9b23a9056c..e791c0c664 100644 --- a/src/corelib/doc/snippets/code/doc_src_containers.cpp +++ b/src/corelib/doc/snippets/code/doc_src_containers.cpp @@ -205,7 +205,7 @@ for (i = splitter->sizes().begin(); //! [15] -QVector<QString> values; +QList<QString> values; ... QString str; foreach (str, values) @@ -214,16 +214,16 @@ foreach (str, values) //! [16] -QVector<QString> values; +QList<QString> values; ... -QVectorIterator<QString> i(values); +QListIterator<QString> i(values); while (i.hasNext()) qDebug() << i.next(); //! [16] //! [17] -QVector<QString> values; +QList<QString> values; ... foreach (const QString &str, values) qDebug() << str; @@ -231,7 +231,7 @@ foreach (const QString &str, values) //! [18] -QVector<QString> values; +QList<QString> values; ... foreach (const QString &str, values) { if (str.isEmpty()) @@ -284,10 +284,10 @@ QString onlyLetters(const QString &in) //! [23] //! [24] -QVector<int> a, b; -a.resize(100000); // make a big vector filled with 0. +QList<int> a, b; +a.resize(100000); // make a big list filled with 0. -QVector<int>::iterator i = a.begin(); +QList<int>::iterator i = a.begin(); // WRONG way of using the iterator i: b = a; /* @@ -309,13 +309,13 @@ int j = *i; // Undefined behavior! /* The data from b (which i pointed to) is gone. This would be well-defined with STL containers (and (*i) == 5), - but with QVector this is likely to crash. + but with QList this is likely to crash. */ //! [24] //! [25] -QVector<int> vector{1, 2, 3, 4, 4, 5}; -QSet<int> set(vector.begin(), vector.end()); +QList<int> list { 1, 2, 3, 4, 4, 5 }; +QSet<int> set(list.begin(), list.end()); /* Will generate a QSet containing 1, 2, 4, 5. */ diff --git a/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp b/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp index b86ea3d7e1..58f6b196b4 100644 --- a/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qalgorithms.cpp @@ -105,13 +105,13 @@ QChar resolveEntity(const QString &entity) QStringList list; list << "one" << "two" << "three"; -QVector<QString> vect1(3); -qCopy(list.begin(), list.end(), vect1.begin()); -// vect: [ "one", "two", "three" ] +QList<QString> list1(3); +qCopy(list.begin(), list.end(), list1.begin()); +// list1: [ "one", "two", "three" ] -QVector<QString> vect2(8); -qCopy(list.begin(), list.end(), vect2.begin() + 2); -// vect: [ "", "", "one", "two", "three", "", "", "" ] +QList<QString> list2(8); +qCopy(list.begin(), list.end(), list2.begin() + 2); +// list2: [ "", "", "one", "two", "three", "", "", "" ] //! [4] @@ -119,26 +119,26 @@ qCopy(list.begin(), list.end(), vect2.begin() + 2); QStringList list; list << "one" << "two" << "three"; -QVector<QString> vect(5); -qCopyBackward(list.begin(), list.end(), vect.end()); -// vect: [ "", "", "one", "two", "three" ] +QList<QString> backList(5); +qCopyBackward(list.begin(), list.end(), backList.end()); +// backList: [ "", "", "one", "two", "three" ] //! [5] //! [6] -QStringList list; -list << "one" << "two" << "three"; +QStringList listLeft; +listLeft << "one" << "two" << "three"; -QVector<QString> vect(3); -vect[0] = "one"; -vect[1] = "two"; -vect[2] = "three"; +QList<QString> listRight(3); +listRight[0] = "one"; +listRight[1] = "two"; +listRight[2] = "three"; -bool ret1 = qEqual(list.begin(), list.end(), vect.begin()); +bool ret1 = qEqual(listLeft.begin(), listLeft.end(), listRight.begin()); // ret1 == true -vect[2] = "seven"; -bool ret2 = qEqual(list.begin(), list.end(), vect.begin()); +listRight[2] = "seven"; +bool ret2 = qEqual(listLeft.begin(), listLeft.end(), listRight.begin()); // ret2 == false //! [6] @@ -281,19 +281,19 @@ list.insert(i, 12); //! [19] -QVector<int> vect; -vect << 3 << 3 << 6 << 6 << 6 << 8; -QVector<int>::iterator begin6 = - qLowerBound(vect.begin(), vect.end(), 6); -QVector<int>::iterator end6 = - qUpperBound(begin6, vect.end(), 6); - -QVector<int>::iterator i = begin6; +QList<int> list; +list << 3 << 3 << 6 << 6 << 6 << 8; +QList<int>::iterator begin6 = + qLowerBound(list.begin(), list.end(), 6); +QList<int>::iterator end6 = + qUpperBound(begin6, list.end(), 6); + +QList<int>::iterator i = begin6; while (i != end6) { *i = 7; ++i; } -// vect: [ 3, 3, 7, 7, 7, 8 ] +// list: [ 3, 3, 7, 7, 7, 8 ] //! [19] @@ -312,29 +312,29 @@ list.insert(i, 12); //! [21] -QVector<int> vect; -vect << 3 << 3 << 6 << 6 << 6 << 8; -QVector<int>::iterator begin6 = - qLowerBound(vect.begin(), vect.end(), 6); -QVector<int>::iterator end6 = - qUpperBound(vect.begin(), vect.end(), 6); - -QVector<int>::iterator i = begin6; +QList<int> list; +list << 3 << 3 << 6 << 6 << 6 << 8; +QList<int>::iterator begin6 = + qLowerBound(list.begin(), list.end(), 6); +QList<int>::iterator end6 = + qUpperBound(list.begin(), list.end(), 6); + +QList<int>::iterator i = begin6; while (i != end6) { *i = 7; ++i; } -// vect: [ 3, 3, 7, 7, 7, 8 ] +// list: [ 3, 3, 7, 7, 7, 8 ] //! [21] //! [22] -QVector<int> vect; -vect << 3 << 3 << 6 << 6 << 6 << 8; +QList<int> list; +list << 3 << 3 << 6 << 6 << 6 << 8; -QVector<int>::iterator i = - qBinaryFind(vect.begin(), vect.end(), 6); -// i == vect.begin() + 2 (or 3 or 4) +QList<int>::iterator i = + qBinaryFind(list.begin(), list.end(), 6); +// i == list.begin() + 2 (or 3 or 4) //! [22] diff --git a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp index 0d0b864a83..265e4a724a 100644 --- a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp +++ b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp @@ -64,23 +64,6 @@ while (i.hasPrevious()) qDebug() << i.previous(); //! [1] -//! [4] -QVector<float> vector; -... -QVectorIterator<float> i(vector); -while (i.hasNext()) - qDebug() << i.next(); -//! [4] - - -//! [5] -QVectorIterator<float> i(vector); -i.toBack(); -while (i.hasPrevious()) - qDebug() << i.previous(); -//! [5] - - //! [6] QSet<QString> set; ... @@ -127,35 +110,6 @@ while (i.hasNext()) { } //! [10] -//! [14] -QVector<float> vector; -... -QMutableVectorIterator<float> i(vector); -while (i.hasNext()) - qDebug() << i.next(); -//! [14] - -//! [15] -QMutableVectorIterator<float> i(vector); -i.toBack(); -while (i.hasPrevious()) - qDebug() << i.previous(); -//! [15] - - -//! [16] -QMutableVectorIterator<int> i(vector); -while (i.hasNext()) { - int val = i.next(); - if (val < 0) { - i.setValue(-val); - } else if (val == 0) { - i.remove(); - } -} -//! [16] - - //! [17] QSet<float> set; ... @@ -182,16 +136,6 @@ while (i.hasNext()) { } //! [19] -//! [21] -QMutableVectorIterator<int> i(vector); -while (i.hasNext()) { - int val = i.next(); - if (val < -32768 || val > 32767) - i.remove(); -} -//! [21] - - //! [22] QMutableSetIterator<int> i(set); while (i.hasNext()) { @@ -210,15 +154,6 @@ while (i.hasNext()) { } //! [23] -//! [25] -QMutableVectorIterator<double> i(list); -while (i.hasNext()) { - double val = i.next(); - i.setValue(std::sqrt(val)); -} -//! [25] - - //! [26] QMap<int, QWidget *> map; ... diff --git a/src/corelib/doc/snippets/code/src_corelib_global_qrandom.cpp b/src/corelib/doc/snippets/code/src_corelib_global_qrandom.cpp index 0dc47f2370..9f2884667c 100644 --- a/src/corelib/doc/snippets/code/src_corelib_global_qrandom.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_global_qrandom.cpp @@ -95,9 +95,9 @@ //! [8] //! [9] - QVector<quint32> vector; - vector.resize(16); - QRandomGenerator::global()->fillRange(vector.data(), vector.size()); + QList<quint32> list; + list.resize(16); + QRandomGenerator::global()->fillRange(list.data(), list.size()); //! [9] //! [10] diff --git a/src/corelib/doc/snippets/code/src_corelib_serialization_qcborstream.cpp b/src/corelib/doc/snippets/code/src_corelib_serialization_qcborstream.cpp index 973ae75847..cb6928b129 100644 --- a/src/corelib/doc/snippets/code/src_corelib_serialization_qcborstream.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_serialization_qcborstream.cpp @@ -208,7 +208,7 @@ //! [19] //! [20] - void appendList(QCborStreamWriter &writer, const QVector<QString> &values) + void appendList(QCborStreamWriter &writer, const QList<QString> &values) { writer.startArray(); for (const QString &s : values) @@ -228,7 +228,7 @@ //! [21] //! [22] - void appendMap(QCborStreamWriter &writer, const QVector<QPair<int, QString>> &values) + void appendMap(QCborStreamWriter &writer, const QList<QPair<int, QString>> &values) { writer.startMap(); for (const auto pair : values) { diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp index cdf1ae0eb1..26b952a77a 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qlist.cpp @@ -49,162 +49,162 @@ ****************************************************************************/ //! [0] -QList<int> integerVector; -QList<QString> stringVector; +QList<int> integerList; +QList<QString> stringList; //! [0] //! [1] -QList<QString> vector(200); +QList<QString> list(200); //! [1] //! [2] -QList<QString> vector(200, "Pass"); +QList<QString> list(200, "Pass"); //! [2] //! [3] -if (vector[0] == "Liz") - vector[0] = "Elizabeth"; +if (list[0] == "Liz") + list[0] = "Elizabeth"; //! [3] //! [4] -for (int i = 0; i < vector.size(); ++i) { - if (vector.at(i) == "Alfonso") +for (int i = 0; i < list.size(); ++i) { + if (list.at(i) == "Alfonso") cout << "Found Alfonso at position " << i << Qt::endl; } //! [4] //! [5] -int i = vector.indexOf("Harumi"); +int i = list.indexOf("Harumi"); if (i != -1) cout << "First occurrence of Harumi is at position " << i << Qt::endl; //! [5] //! [6] -QList<int> vector(10); -int *data = vector.data(); +QList<int> list(10); +int *data = list.data(); for (int i = 0; i < 10; ++i) data[i] = 2 * i; //! [6] //! [7] -QList<QString> vector; -vector.append("one"); -vector.append("two"); +QList<QString> list; +list.append("one"); +list.append("two"); QString three = "three"; -vector.append(three); -// vector: ["one", "two", "three"] +list.append(three); +// list: ["one", "two", "three"] // three: "three" //! [7] //! [move-append] -QList<QString> vector; -vector.append("one"); -vector.append("two"); +QList<QString> list; +list.append("one"); +list.append("two"); QString three = "three"; -vector.append(std::move(three)); -// vector: ["one", "two", "three"] +list.append(std::move(three)); +// list: ["one", "two", "three"] // three: "" //! [move-append] //! [emplace] -QList<QString> vector{"a", "ccc"}; -vector.emplace(1, 2, 'b'); -// vector: ["a", "bb", "ccc"] +QList<QString> list{"a", "ccc"}; +list.emplace(1, 2, 'b'); +// list: ["a", "bb", "ccc"] //! [emplace] //! [emplace-back] -QList<QString> vector{"one", "two"}; -vector.emplaceBack(3, 'a'); -qDebug() << vector; -// vector: ["one", "two", "aaa"] +QList<QString> list{"one", "two"}; +list.emplaceBack(3, 'a'); +qDebug() << list; +// list: ["one", "two", "aaa"] //! [emplace-back] //! [emplace-back-ref] -QList<QString> vector; -auto &ref = vector.emplaceBack(); +QList<QString> list; +auto &ref = list.emplaceBack(); ref = "one"; -// vector: ["one"] +// list: ["one"] //! [emplace-back-ref] //! [8] -QList<QString> vector; -vector.prepend("one"); -vector.prepend("two"); -vector.prepend("three"); -// vector: ["three", "two", "one"] +QList<QString> list; +list.prepend("one"); +list.prepend("two"); +list.prepend("three"); +// list: ["three", "two", "one"] //! [8] //! [9] -QList<QString> vector; -vector << "alpha" << "beta" << "delta"; -vector.insert(2, "gamma"); -// vector: ["alpha", "beta", "gamma", "delta"] +QList<QString> list; +list << "alpha" << "beta" << "delta"; +list.insert(2, "gamma"); +// list: ["alpha", "beta", "gamma", "delta"] //! [9] //! [10] -QList<double> vector; -vector << 2.718 << 1.442 << 0.4342; -vector.insert(1, 3, 9.9); -// vector: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342] +QList<double> list; +list << 2.718 << 1.442 << 0.4342; +list.insert(1, 3, 9.9); +// list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342] //! [10] //! [11] -QList<QString> vector(3); -vector.fill("Yes"); -// vector: ["Yes", "Yes", "Yes"] +QList<QString> list(3); +list.fill("Yes"); +// list: ["Yes", "Yes", "Yes"] -vector.fill("oh", 5); -// vector: ["oh", "oh", "oh", "oh", "oh"] +list.fill("oh", 5); +// list: ["oh", "oh", "oh", "oh", "oh"] //! [11] //! [12] -QList<QString> vector; -vector << "A" << "B" << "C" << "B" << "A"; -vector.indexOf("B"); // returns 1 -vector.indexOf("B", 1); // returns 1 -vector.indexOf("B", 2); // returns 3 -vector.indexOf("X"); // returns -1 +QList<QString> list; +list << "A" << "B" << "C" << "B" << "A"; +list.indexOf("B"); // returns 1 +list.indexOf("B", 1); // returns 1 +list.indexOf("B", 2); // returns 3 +list.indexOf("X"); // returns -1 //! [12] //! [13] -QList<QString> vector; -vector << "A" << "B" << "C" << "B" << "A"; -vector.lastIndexOf("B"); // returns 3 -vector.lastIndexOf("B", 3); // returns 3 -vector.lastIndexOf("B", 2); // returns 1 -vector.lastIndexOf("X"); // returns -1 +QList<QString> list; +list << "A" << "B" << "C" << "B" << "A"; +list.lastIndexOf("B"); // returns 3 +list.lastIndexOf("B", 3); // returns 3 +list.lastIndexOf("B", 2); // returns 1 +list.lastIndexOf("X"); // returns -1 //! [13] //! [16] std::vector<double> stdvector; -vector.push_back(1.2); -vector.push_back(0.5); -vector.push_back(3.14); +stdvector.push_back(1.2); +stdvector.push_back(0.5); +stdvector.push_back(3.14); -QList<double> vector = QList<double>::fromStdVector(stdvector); +QList<double> list = QList<double>::fromStdVector(stdvector); //! [16] //! [17] -QList<double> vector; -vector << 1.2 << 0.5 << 3.14; +QList<double> list; +list << 1.2 << 0.5 << 3.14; -std::vector<double> stdvector = vector.toStdVector(); +std::vector<double> stdlist = list.toStdVector(); //! [17] diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp deleted file mode 100644 index 38fa526ef4..0000000000 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qlistdata.cpp +++ /dev/null @@ -1,280 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -//! [0] -QList<int> integerList; -QList<QDate> dateList; -//! [0] - - -//! [1a] -QList<QString> list = { "one", "two", "three" }; -//! [1a] - - -//! [1b] -list << "four" << "five"; -//! [1b] - - -//! [2] -if (list[0] == "Bob") - list[0] = "Robert"; -//! [2] - - -//! [3] -for (int i = 0; i < list.size(); ++i) { - if (list.at(i) == "Jane") - cout << "Found Jane at position " << i << Qt::endl; -} -//! [3] - - -//! [4] -QList<QWidget *> list; -... -while (!list.isEmpty()) - delete list.takeFirst(); -//! [4] - - -//! [5] -int i = list.indexOf("Jane"); -if (i != -1) - cout << "First occurrence of Jane is at position " << i << Qt::endl; -//! [5] - - -//! [6] -QList<QString> list; -list.append("one"); -list.append("two"); -list.append("three"); -// list: ["one", "two", "three"] -//! [6] - - -//! [7] -QList<QString> list; -list.prepend("one"); -list.prepend("two"); -list.prepend("three"); -// list: ["three", "two", "one"] -//! [7] - - -//! [8] -QList<QString> list; -list << "alpha" << "beta" << "delta"; -list.insert(2, "gamma"); -// list: ["alpha", "beta", "gamma", "delta"] -//! [8] - - -//! [9] -QList<QString> list; -list << "sun" << "cloud" << "sun" << "rain"; -list.removeAll("sun"); -// list: ["cloud", "rain"] -//! [9] - - -//! [10] -QList<QString> list; -list << "sun" << "cloud" << "sun" << "rain"; -list.removeOne("sun"); -// list: ["cloud", "sun", "rain"] -//! [10] - - -//! [11] -QList<QString> list; -list << "A" << "B" << "C" << "D" << "E" << "F"; -list.move(1, 4); -// list: ["A", "C", "D", "E", "B", "F"] -//! [11] - - -//! [12] -QList<QString> list; -list << "A" << "B" << "C" << "D" << "E" << "F"; -list.swapItemsAt(1, 4); -// list: ["A", "E", "C", "D", "B", "F"] -//! [12] - - -//! [13] -QList<QString> list; -list << "A" << "B" << "C" << "B" << "A"; -list.indexOf("B"); // returns 1 -list.indexOf("B", 1); // returns 1 -list.indexOf("B", 2); // returns 3 -list.indexOf("X"); // returns -1 -//! [13] - - -//! [14] -QList<QString> list; -list << "A" << "B" << "C" << "B" << "A"; -list.lastIndexOf("B"); // returns 3 -list.lastIndexOf("B", 3); // returns 3 -list.lastIndexOf("B", 2); // returns 1 -list.lastIndexOf("X"); // returns -1 -//! [14] - - -//! [15] -QList<QString> list; -list.append("January"); -list.append("February"); -... -list.append("December"); - -QList<QString>::iterator i; -for (i = list.begin(); i != list.end(); ++i) - cout << *i << Qt::endl; -//! [15] - - -//! [16] -QList<int>::iterator i; -for (i = list.begin(); i != list.end(); ++i) - *i += 2; -//! [16] - - -//! [17] -QList<QWidget *> list; -... -qDeleteAll(list.begin(), list.end()); -//! [17] - - -//! [18] -if (*it == "Hello") - *it = "Bonjour"; -//! [18] - - -//! [19] -QList<QString> list; -list.append("January"); -list.append("February"); -... -list.append("December"); - -QList<QString>::const_iterator i; -for (i = list.constBegin(); i != list.constEnd(); ++i) - cout << *i << Qt::endl; -//! [19] - - -//! [20] -QList<QWidget *> list; -... -qDeleteAll(list.constBegin(), list.constEnd()); -//! [20] - - -//! [21] -QVector<double> vect; -vect << 20.0 << 30.0 << 40.0 << 50.0; - -QList<double> list = QVector<T>::fromVector(vect); -// list: [20.0, 30.0, 40.0, 50.0] -//! [21] - - -//! [22] -QStringList list; -list << "Sven" << "Kim" << "Ola"; - -QVector<QString> vect = list.toVector(); -// vect: ["Sven", "Kim", "Ola"] -//! [22] - - -//! [23] -QSet<int> set; -set << 20 << 30 << 40 << ... << 70; - -QList<int> list = QList<int>::fromSet(set); -std::sort(list.begin(), list.end()); -//! [23] - - -//! [24] -QStringList list; -list << "Julia" << "Mike" << "Mike" << "Julia" << "Julia"; - -QSet<QString> set = list.toSet(); -set.contains("Julia"); // returns true -set.contains("Mike"); // returns true -set.size(); // returns 2 -//! [24] - - -//! [25] -std::list<double> stdlist; -list.push_back(1.2); -list.push_back(0.5); -list.push_back(3.14); - -QList<double> list = QList<double>::fromStdList(stdlist); -//! [25] - - -//! [26] -QList<double> list; -list << 1.2 << 0.5 << 3.14; - -std::list<double> stdlist = list.toStdList(); -//! [26] diff --git a/src/corelib/doc/src/containers.qdoc b/src/corelib/doc/src/containers.qdoc index d73c9dd07b..5b9b700669 100644 --- a/src/corelib/doc/src/containers.qdoc +++ b/src/corelib/doc/src/containers.qdoc @@ -42,7 +42,7 @@ The Qt library provides a set of general purpose template-based container classes. These classes can be used to store items of a specified type. For example, if you need a resizable array of - \l{QString}s, use QVector<QString>. + \l{QString}s, use QList<QString>. These container classes are designed to be lighter, safer, and easier to use than the STL containers. If you are unfamiliar with @@ -74,9 +74,9 @@ \section1 The Container Classes - Qt provides the following sequential containers: QVector, + Qt provides the following sequential containers: QList, QStack, and QQueue. For most - applications, QVector is the best type to use. It provides very fast + applications, QList is the best type to use. It provides very fast appends. If you really need a linked-list, use std::list. QStack and QQueue are convenience classes that provide LIFO and FIFO semantics. @@ -93,30 +93,30 @@ \table \header \li Class \li Summary - \row \li \l{QVector}<T> + \row \li \l{QList}<T> \li This is by far the most commonly used container class. It stores a list of values of a given type (T) that can be accessed by index. Internally, it stores an array of values of a given type at adjacent positions in memory. Inserting at the front or in the middle of - a vector can be quite slow, because it can lead to large numbers + a list can be quite slow, because it can lead to large numbers of items having to be moved by one position in memory. \row \li \l{QVarLengthArray}<T, Prealloc> \li This provides a low-level variable-length array. It can be used - instead of QVector in places where speed is particularly important. + instead of QList in places where speed is particularly important. \row \li \l{QStack}<T> - \li This is a convenience subclass of QVector that provides + \li This is a convenience subclass of QList that provides "last in, first out" (LIFO) semantics. It adds the following - functions to those already present in QVector: + functions to those already present in QList: \l{QStack::push()}{push()}, \l{QStack::pop()}{pop()}, and \l{QStack::top()}{top()}. \row \li \l{QQueue}<T> - \li This is a convenience subclass of QVector that provides + \li This is a convenience subclass of QList that provides "first in, first out" (FIFO) semantics. It adds the following - functions to those already present in QQVector: + functions to those already present in QList: \l{QQueue::enqueue()}{enqueue()}, \l{QQueue::dequeue()}{dequeue()}, and \l{QQueue::head()}{head()}. @@ -147,11 +147,11 @@ \endtable Containers can be nested. For example, it is perfectly possible - to use a QMap<QString, QVector<int>>, where the key type is - QString and the value type QVector<int>. + to use a QMap<QString, QList<int>>, where the key type is + QString and the value type QList<int>. The containers are defined in individual header files with the - same name as the container (e.g., \c <QVector>). For + same name as the container (e.g., \c <QList>). For convenience, the containers are forward declared in \c <QtContainerFwd>. @@ -167,10 +167,10 @@ double, pointer types, and Qt data types such as QString, QDate, and QTime, but it doesn't cover QObject or any QObject subclass (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a - QVector<QWidget>, the compiler will complain that QWidget's copy + QList<QWidget>, the compiler will complain that QWidget's copy constructor and assignment operators are disabled. If you want to store these kinds of objects in a container, store them as - pointers, for example as QVector<QWidget *>. + pointers, for example as QList<QWidget *>. Here's an example custom data type that meets the requirement of an assignable data type: @@ -210,7 +210,7 @@ \target default-constructed value The documentation of certain container class functions refer to - \e{default-constructed values}; for example, QVector + \e{default-constructed values}; for example, QList automatically initializes its items with default-constructed values, and QMap::value() returns a default-constructed value if the specified key isn't in the map. For most value types, this @@ -241,22 +241,22 @@ read-write access. \table - \header \li Containers \li Read-only iterator + \header \li Containers \li Read-only iterator \li Read-write iterator \li QMutableListIterator<T> - \row \li QVector<T>, QStack<T>, QQueue<T> \li QVectorIterator<T> - \li QMutableVectorIterator<T> - \row \li QSet<T> \li QSetIterator<T> + \row \li QList<T>, QQueue<T>, QStack<T>, \li QListIterator<T> + \li QMutableListIterator<T> + \row \li QSet<T> \li QSetIterator<T> \li QMutableSetIterator<T> - \row \li QMap<Key, T>, QMultiMap<Key, T> \li QMapIterator<Key, T> + \row \li QMap<Key, T>, QMultiMap<Key, T> \li QMapIterator<Key, T> \li QMutableMapIterator<Key, T> - \row \li QHash<Key, T>, QMultiHash<Key, T> \li QHashIterator<Key, T> + \row \li QHash<Key, T>, QMultiHash<Key, T> \li QHashIterator<Key, T> \li QMutableHashIterator<Key, T> \endtable - In this discussion, we will concentrate on QVector and QMap. The + In this discussion, we will concentrate on QList and QMap. The iterator types for QSet have exactly - the same interface as QVector's iterators; similarly, the iterator + the same interface as QList's iterators; similarly, the iterator types for QHash have the same interface as QMap's iterators. Unlike STL-style iterators (covered \l{STL-style @@ -271,59 +271,59 @@ \image javaiterators1.png Here's a typical loop for iterating through all the elements of a - QVector<QString> in order and printing them to the console: + QList<QString> in order and printing them to the console: \snippet code/doc_src_containers.cpp 1 - It works as follows: The QVector to iterate over is passed to the - QVectorIterator constructor. At that point, the iterator is located + It works as follows: The QList to iterate over is passed to the + QListIterator constructor. At that point, the iterator is located just in front of the first item in the list (before item "A"). - Then we call \l{QVectorIterator::hasNext()}{hasNext()} to + Then we call \l{QListIterator::hasNext()}{hasNext()} to check whether there is an item after the iterator. If there is, we - call \l{QVectorIterator::next()}{next()} to jump over that + call \l{QListIterator::next()}{next()} to jump over that item. The next() function returns the item that it jumps over. For - a QVector<QString>, that item is of type QString. + a QList<QString>, that item is of type QString. - Here's how to iterate backward in a QVector: + Here's how to iterate backward in a QList: \snippet code/doc_src_containers.cpp 2 The code is symmetric with iterating forward, except that we - start by calling \l{QVectorIterator::toBack()}{toBack()} + start by calling \l{QListIterator::toBack()}{toBack()} to move the iterator after the last item in the list. The diagram below illustrates the effect of calling - \l{QVectorIterator::next()}{next()} and - \l{QVectorIterator::previous()}{previous()} on an iterator: + \l{QListIterator::next()}{next()} and + \l{QListIterator::previous()}{previous()} on an iterator: \image javaiterators2.png - The following table summarizes the QVectorIterator API: + The following table summarizes the QListIterator API: \table \header \li Function \li Behavior - \row \li \l{QVectorIterator::toFront()}{toFront()} + \row \li \l{QListIterator::toFront()}{toFront()} \li Moves the iterator to the front of the list (before the first item) - \row \li \l{QVectorIterator::toBack()}{toBack()} + \row \li \l{QListIterator::toBack()}{toBack()} \li Moves the iterator to the back of the list (after the last item) - \row \li \l{QVectorIterator::hasNext()}{hasNext()} + \row \li \l{QListIterator::hasNext()}{hasNext()} \li Returns \c true if the iterator isn't at the back of the list - \row \li \l{QVectorIterator::next()}{next()} + \row \li \l{QListIterator::next()}{next()} \li Returns the next item and advances the iterator by one position - \row \li \l{QVectorIterator::peekNext()}{peekNext()} + \row \li \l{QListIterator::peekNext()}{peekNext()} \li Returns the next item without moving the iterator - \row \li \l{QVectorIterator::hasPrevious()}{hasPrevious()} + \row \li \l{QListIterator::hasPrevious()}{hasPrevious()} \li Returns \c true if the iterator isn't at the front of the list - \row \li \l{QVectorIterator::previous()}{previous()} + \row \li \l{QListIterator::previous()}{previous()} \li Returns the previous item and moves the iterator back by one position - \row \li \l{QVectorIterator::peekPrevious()}{peekPrevious()} + \row \li \l{QListIterator::peekPrevious()}{peekPrevious()} \li Returns the previous item without moving the iterator \endtable - QVectorIterator provides no functions to insert or remove items + QListIterator provides no functions to insert or remove items from the list as we iterate. To accomplish this, you must use QMutableListIterator. Here's an example where we remove all - odd numbers from a QVector<int> using QMutableListIterator: + odd numbers from a QList<int> using QMutableListIterator: \snippet code/doc_src_containers.cpp 3 @@ -357,11 +357,11 @@ \snippet code/doc_src_containers.cpp 6 As mentioned above QSet's iterator - classes have exactly the same API as QVector's. We will now turn to + classes have exactly the same API as QList's. We will now turn to QMapIterator, which is somewhat different because it iterates on (key, value) pairs. - Like QVectorIterator, QMapIterator provides + Like QListIterator, QMapIterator provides \l{QMapIterator::toFront()}{toFront()}, \l{QMapIterator::toBack()}{toBack()}, \l{QMapIterator::hasNext()}{hasNext()}, @@ -407,48 +407,48 @@ possible because they are faster than read-write iterators. \table - \header \li Containers \li Read-only iterator + \header \li Containers \li Read-only iterator \li Read-write iterator - \row \li QVector<T>, QStack<T>, QQueue<T> \li QVector<T>::const_iterator - \li QVector<T>::iterator - \row \li QSet<T> \li QSet<T>::const_iterator + \row \li QList<T>, QStack<T>, QQueue<T> \li QList<T>::const_iterator + \li QList<T>::iterator + \row \li QSet<T> \li QSet<T>::const_iterator \li QSet<T>::iterator - \row \li QMap<Key, T>, QMultiMap<Key, T> \li QMap<Key, T>::const_iterator + \row \li QMap<Key, T>, QMultiMap<Key, T> \li QMap<Key, T>::const_iterator \li QMap<Key, T>::iterator - \row \li QHash<Key, T>, QMultiHash<Key, T> \li QHash<Key, T>::const_iterator + \row \li QHash<Key, T>, QMultiHash<Key, T> \li QHash<Key, T>::const_iterator \li QHash<Key, T>::iterator \endtable The API of the STL iterators is modelled on pointers in an array. For example, the \c ++ operator advances the iterator to the next item, and the \c * operator returns the item that the iterator - points to. In fact, for QVector and QStack, which store their + points to. In fact, for QList and QStack, which store their items at adjacent memory positions, the - \l{QVector::iterator}{iterator} type is just a typedef for \c{T *}, - and the \l{QVector::iterator}{const_iterator} type is + \l{QList::iterator}{iterator} type is just a typedef for \c{T *}, + and the \l{QList::iterator}{const_iterator} type is just a typedef for \c{const T *}. - In this discussion, we will concentrate on QVector and QMap. The + In this discussion, we will concentrate on QList and QMap. The iterator types for QSet have exactly - the same interface as QVector's iterators; similarly, the iterator + the same interface as QList's iterators; similarly, the iterator types for QHash have the same interface as QMap's iterators. Here's a typical loop for iterating through all the elements of a - QVector<QString> in order and converting them to lowercase: + QList<QString> in order and converting them to lowercase: \snippet code/doc_src_containers.cpp 10 Unlike \l{Java-style iterators}, STL-style iterators point - directly at items. The \l{QVector::begin()}{begin()} function of a container returns an + directly at items. The \l{QList::begin()}{begin()} function of a container returns an iterator that points to the first item in the container. The - \l{QVector::end()}{end()} function of a container returns an iterator to the + \l{QList::end()}{end()} function of a container returns an iterator to the imaginary item one position past the last item in the container. - \l {QVector::end()}{end()} marks an invalid position; it must never be dereferenced. + \l {QList::end()}{end()} marks an invalid position; it must never be dereferenced. It is typically used in a loop's break condition. If the list is - empty, \l{QVector::begin}{begin()} equals \l{QVector::end()}{end()}, so we never execute the loop. + empty, \l{QList::begin}{begin()} equals \l{QList::end()}{end()}, so we never execute the loop. The diagram below shows the valid iterator positions as red - arrows for a vector containing four items: + arrows for a list containing four items: \image stliterators1.png @@ -462,8 +462,8 @@ compilers also allow us to write \c{i->toLower()}, but some don't. - For read-only access, you can use const_iterator, \l{QVector::constBegin}{constBegin()}, - and \l{QVector::constEnd()}{constEnd()}. For example: + For read-only access, you can use const_iterator, \l{QList::constBegin}{constBegin()}, + and \l{QList::constEnd()}{constEnd()}. For example: \snippet code/doc_src_containers.cpp 12 @@ -501,7 +501,7 @@ Thanks to \l{implicit sharing}, it is very inexpensive for a function to return a container per value. The Qt API contains - dozens of functions that return a QVector or QStringList per value + dozens of functions that return a QList or QStringList per value (e.g., QSplitter::sizes()). If you want to iterate over these using an STL iterator, you should always take a copy of the container and iterate over the copy. For example: @@ -521,7 +521,7 @@ \snippet code/doc_src_containers.cpp 24 - The above example only shows a problem with QVector, but + The above example only shows a problem with QList, but the problem exists for all the implicitly shared Qt containers. \target foreach @@ -534,7 +534,7 @@ Its syntax is: \c foreach (\e variable, \e container) \e statement. For example, here's how to use \c foreach to iterate - over a QVector<QString>: + over a QList<QString>: \snippet code/doc_src_containers.cpp 15 @@ -620,8 +620,8 @@ example, inserting an item in the middle of a std::list is an extremely fast operation, irrespective of the number of items stored in the list. On the other hand, inserting an item - in the middle of a QVector is potentially very expensive if the - QVector contains many items, since half of the items must be + in the middle of a QList is potentially very expensive if the + QList contains many items, since half of the items must be moved one position in memory. To describe algorithmic complexity, we use the following @@ -637,7 +637,7 @@ \li \b{Constant time:} O(1). A function is said to run in constant time if it requires the same amount of time no matter how many items are present in the container. One example is - QVector::push_back(). + QList::push_back(). \li \b{Logarithmic time:} O(log \e n). A function that runs in logarithmic time is a function whose running time is @@ -647,7 +647,7 @@ \li \b{Linear time:} O(\e n). A function that runs in linear time will execute in a time directly proportional to the number of items stored in the container. One example is - QVector::insert(). + QList::insert(). \li \b{Linear-logarithmic time:} O(\e{n} log \e n). A function that runs in linear-logarithmic time is asymptotically slower @@ -660,11 +660,11 @@ \endlist The following table summarizes the algorithmic complexity of the sequential - container QVector<T>: + container QList<T>: \table - \header \li \li Index lookup \li Insertion \li Prepending \li Appending - \row \li QVector<T> \li O(1) \li O(n) \li O(n) \li Amort. O(1) + \header \li \li Index lookup \li Insertion \li Prepending \li Appending + \row \li QList<T> \li O(1) \li O(n) \li O(n) \li Amort. O(1) \endtable In the table, "Amort." stands for "amortized behavior". For @@ -685,15 +685,15 @@ \row \li QSet<Key> \li Amort. O(1) \li O(\e n) \li Amort. O(1) \li O(\e n) \endtable - With QVector, QHash, and QSet, the performance of appending items + With QList, QHash, and QSet, the performance of appending items is amortized O(log \e n). It can be brought down to O(1) by - calling QVector::reserve(), QHash::reserve(), or QSet::reserve() + calling QList::reserve(), QHash::reserve(), or QSet::reserve() with the expected number of items before you insert the items. The next section discusses this topic in more depth. \section1 Growth Strategies - QVector<T>, QString, and QByteArray store their items + QList<T>, QString, and QByteArray store their items contiguously in memory; QHash<Key, T> keeps a hash table whose size is proportional to the number of items in the hash. To avoid reallocating the data every single @@ -732,12 +732,12 @@ QByteArray uses more or less the same algorithm as QString. - QVector<T> also uses that algorithm for data types that can be + QList<T> also uses that algorithm for data types that can be moved around in memory using \c memcpy() (including the basic C++ types, the pointer types, and Qt's \l{shared classes}) but uses a different algorithm for data types that can only be moved by calling the copy constructor and a destructor. Since the cost of - reallocating is higher in that case, QVector<T> reduces the + reallocating is higher in that case, QList<T> reduces the number of reallocations by always doubling the memory when running out of space. @@ -748,7 +748,7 @@ QSet<T> and QCache<Key, T> as well. For most applications, the default growing algorithm provided by - Qt does the trick. If you need more control, QVector<T>, + Qt does the trick. If you need more control, QList<T>, QHash<Key, T>, QSet<T>, QString, and QByteArray provide a trio of functions that allow you to check and specify how much memory to use to store the items: diff --git a/src/corelib/doc/src/datastreamformat.qdoc b/src/corelib/doc/src/datastreamformat.qdoc index 1e23b19c1f..303d374678 100644 --- a/src/corelib/doc/src/datastreamformat.qdoc +++ b/src/corelib/doc/src/datastreamformat.qdoc @@ -66,6 +66,7 @@ \li QIcon \li QImage \li QKeySequence + \li QList<T> \li QMap<Key, T> \li QMargins \li QMatrix4x4 @@ -88,7 +89,6 @@ \li QVector2D \li QVector3D \li QVector4D - \li QVector<T> \endlist \sa {JSON Support in Qt} diff --git a/src/corelib/doc/src/implicit-sharing.qdoc b/src/corelib/doc/src/implicit-sharing.qdoc index 03cbdd7ff0..d789f8bf92 100644 --- a/src/corelib/doc/src/implicit-sharing.qdoc +++ b/src/corelib/doc/src/implicit-sharing.qdoc @@ -139,7 +139,7 @@ is called for \c p2, because painting a pixmap will modify it. \warning Be careful with copying an implicitly shared container - (QMap, QVector, etc.) while you use + (QMap, QList, etc.) while you use \l{STL-style iterators}{STL-style iterator}. See \l{Implicit sharing iterator problem}. \target implicitly shared classes |