<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Qt5 中的容器 > 原文: [http://zetcode.com/gui/qt5/containers/](http://zetcode.com/gui/qt5/containers/) 在 Qt5 教程的這一部分中,我們將討論 Qt5 中的容器。 提及以下容器:`QVector`,`QList`,`QStringList`,`QSet`和`QMap`。 容器是通用類,用于將給定類型的項目存儲在內存中。 C++ 具有標準模板庫(STL),該模板庫具有自己的容器。 對于 Qt,我們可以使用 Qt 容器或 STL 容器。 有兩種容器:順序容器和關聯容器。 順序容器一個接一個地存儲項目,而關聯容器存儲鍵值對。 `QList`,`QVector`,`QLinkedList`屬于順序容器; `QMap`和`QHash`是關聯容器的示例。 ## `QVector` `QVector`是提供動態數組的模板類。 它將項目存儲在相鄰的內存位置,并提供基于索引的快速訪問。 對于大向量,插入操作較慢,建議改用`QList`容器。 `myvector.cpp` ```cpp #include <QVector> #include <QTextStream> int main(void) { QTextStream out(stdout); QVector<int> vals = {1, 2, 3, 4, 5}; out << "The size of the vector is: " << vals.size() << endl; out << "The first item is: " << vals.first() << endl; out << "The last item is: " << vals.last() << endl; vals.append(6); vals.prepend(0); out << "Elements: "; for (int val : vals) { out << val << " "; } out << endl; return 0; } ``` 該示例使用整數向量。 ```cpp QVector<int> vals = {1, 2, 3, 4, 5}; ``` 創建一個整數向量。 ```cpp out << "The size of the vector is: " << vals.size() << endl; ``` `size()`方法給出向量的大小-向量中的項數。 ```cpp out << "The first item is: " << vals.first() << endl; ``` 使用`first()`方法檢索第一項。 ```cpp out << "The last item is: " << vals.last() << endl; ``` 使用`last()`方法可以找到向量的最后一項。 ```cpp vals.append(6); ``` `append()`方法將值插入向量的末尾。 ```cpp vals.prepend(0); ``` `prepend()`方法將值插入向量的開頭。 ```cpp for (int val : vals) { out << val << " "; } ``` 我們遍歷`for`循環中的向量并打印其內容。 輸出: ```cpp $ ./myvector The size of the vector is: 5 The first item is: 1 The last item is: 5 Elements: 0 1 2 3 4 5 6 ``` ## `QList` `QList`是用于創建元素列表的容器。 與`QVector`相似。 它存儲值列表,并提供基于索引的快速訪問以及快速插入和刪除。 它是 Qt 中最常用的容器之一。 `mylist.cpp` ```cpp #include <QTextStream> #include <QList> #include <algorithm> int main(void) { QTextStream out(stdout); QList<QString> authors = {"Balzac", "Tolstoy", "Gulbranssen", "London"}; for (int i=0; i < authors.size(); ++i) { out << authors.at(i) << endl; } authors << "Galsworthy" << "Sienkiewicz"; out << "***********************" << endl; std::sort(authors.begin(), authors.end()); out << "Sorted:" << endl; for (QString author : authors) { out << author << endl; } } ``` 該示例介紹了`QList`容器。 ```cpp QList<QString> authors = {"Balzac", "Tolstoy", "Gulbranssen", "London"}; ``` 創建一個`QList`容器。 它存儲作家的姓名。 ```cpp for (int i=0; i < authors.size(); ++i) { out << authors.at(i) << endl; } ``` 在`for`循環中,我們遍歷容器并打印其元素。 `at()`方法返回給定索引處的項目。 ```cpp authors << "Galsworthy" << "Sienkiewicz"; ``` `<<`運算符用于在列表中插入兩個新項目。 ```cpp std::sort(authors.begin(), authors.end()); ``` `std::sort()`方法按升序對列表進行排序。 ```cpp out << "Sorted:" << endl; for (QString author : authors) { out << author << endl; } ``` 現在我們打印排序列表。 輸出: ```cpp $ ./mylist Balzac Tolstoy Gulbranssen London *********************** Sorted: Balzac Galsworthy Gulbranssen London Sienkiewicz Tolstoy ``` ## `QStringList` `QStringList`是提供字符串列表的便捷容器。 它具有基于索引的快速訪問以及快速的插入和刪除。 `mystringlist.cpp` ```cpp #include <QTextStream> #include <QList> int main(void) { QTextStream out(stdout); QString string = "coin, book, cup, pencil, clock, bookmark"; QStringList items = string.split(","); QStringListIterator it(items); while (it.hasNext()) { out << it.next().trimmed() << endl; } } ``` 在示例中,我們從一個字符串創建一個字符串列表,并將元素打印到控制臺中。 ```cpp QString string = "coin, book, cup, pencil, clock, bookmark"; QStringList items = string.split(","); ``` `QString`的`split()`方法根據提供的分隔符將字符串切成子字符串。 子字符串在列表中返回。 ```cpp QStringListIterator it(items); ``` `QStringListIterator`為`QStringList`提供了 Java 樣式的常迭代器。 ```cpp while (it.hasNext()) { out << it.next().trimmed() << endl; } ``` 使用創建的迭代器,我們將列表的元素打印到終端。 `trimmed()`方法可修剪字符串元素中的空白。 輸出: ```cpp $ ./mystringlist coin book cup pencil clock bookmark ``` ## `QSet` `QSet`提供具有快速查找功能的單值數學集。 值以未指定的順序存儲。 `myset.cpp` ```cpp #include <QSet> #include <QList> #include <QTextStream> #include <algorithm> int main(void) { QTextStream out(stdout); QSet<QString> cols1 = {"yellow", "red", "blue"}; QSet<QString> cols2 = {"blue", "pink", "orange"}; out << "There are " << cols1.size() << " values in the set" << endl; cols1.insert("brown"); out << "There are " << cols1.size() << " values in the set" << endl; cols1.unite(cols2); out << "There are " << cols1.size() << " values in the set" << endl; for (QString val : cols1) { out << val << endl; } QList<QString> lcols = cols1.values(); std::sort(lcols.begin(), lcols.end()); out << "*********************" << endl; out << "Sorted:" << endl; for (QString val : lcols) { out << val << endl; } return 0; } ``` 在示例中,`QSet`用于存儲顏色。 多次指定一種顏色值沒有意義。 ```cpp QSet<QString> cols1 = {"yellow", "red", "blue"}; QSet<QString> cols2 = {"blue", "pink", "orange"}; ``` 我們有兩組顏色值。 兩組中都有藍色。 ```cpp out << "There are " << cols1.size() << " values in the set" << endl; ``` `size()`方法返回集合的大小。 ```cpp cols1.insert("brown"); ``` 我們使用`insert()`方法向集合中添加新值。 ```cpp cols1.unite(cols2); ``` `unite((`方法執行兩個集合的并集。 `cols1`設置將具有從`cols2`設置插入的所有尚不存在的項目。 在我們的案例中,除了藍色以外的所有顏色。 ```cpp for (QString val : cols1) { out << val << endl; } ``` 使用`for`循環,我們打印`cols1`集中的所有項目。 ```cpp QList<QString> lcols = cols1.values(); std::sort(lcols.begin(), lcols.end()); ``` 不支持對集合進行排序。 我們可以創建一個列表并對其進行排序。 `values()`方法返回一個新的`QList`,其中包含集合中的元素。 `QList`中元素的順序未定義。 輸出: ```cpp $ ./myset There are 3 values in the set There are 4 values in the set There are 6 values in the set pink orange brown blue yellow red ********************* Sorted: blue brown orange pink red yellow ``` ## `QMap` `QMap`是一個存儲鍵值對的關聯數組(字典)。 它提供與鍵關聯的值的快速查找。 `myqmap.cpp` ```cpp #include <QTextStream> #include <QMap> int main(void) { QTextStream out(stdout); QMap<QString, int> items = { {"coins", 5}, {"books", 3} }; items.insert("bottles", 7); QList<int> values = items.values(); out << "Values:" << endl; for (int val : values) { out << val << endl; } QList<QString> keys = items.keys(); out << "Keys:" << endl; for (QString key : keys) { out << key << endl; } QMapIterator<QString, int> it(items); out << "Pairs:" << endl; while (it.hasNext()) { it.next(); out << it.key() << ": " << it.value() << endl; } } ``` 在示例中,我們有一個字典,在其中將字符串鍵映射到整數值。 ```cpp QMap<QString, int> items = { {"coins", 5}, {"books", 3} }; ``` 創建了`QMap`。 它有兩對。 ```cpp items.insert("bottles", 7); ``` 使用`insert()`方法插入一對新的對。 ```cpp QList<int> values = items.values(); out << "Values:" << endl; for (int val : values) { out << val << endl; } ``` 我們獲取字典的所有值并將其打印到控制臺。 `values()`方法返回映射值列表。 ```cpp QList<QString> keys = items.keys(); out << "Keys:" << endl; for (QString key : keys) { out << key << endl; } ``` 同樣,我們打印字典的所有鍵。 `keys()`方法返回一個列表,其中包含字典中的所有鍵。 ```cpp QMapIterator<QString, int> it(items); ``` `QMapIterator`是`QMap`的 Java 樣式的迭代器。 它可用于遍歷映射元素。 ```cpp while (it.hasNext()) { it.next(); out << it.key() << ": " << it.value() << endl; } ``` 在迭代器的幫助下,我們遍歷了映射的所有元素。 `key()`方法返回當前鍵,`value()`方法返回當前值。 輸出: ```cpp $ ./myqmap Values: 3 7 5 Keys: books bottles coins Pairs: books: 3 bottles: 7 coins: 5 ``` ## 自定義類排序 在下面的示例中,我們將在`QList`中對自定義類的對象進行排序。 `book.h` ```cpp class Book { public: Book(QString, QString); QString getAuthor() const; QString getTitle() const; private: QString author; QString title; }; ``` 這是我們的自定義`Book`類的頭文件。 `book.cpp` ```cpp #include <QString> #include "book.h" Book::Book(QString auth, QString tit) { author = auth; title = tit; } QString Book::getAuthor() const { return author; } QString Book::getTitle() const { return title; } ``` 這是`Book`類的實現; 我們有兩種訪問器方法。 `sortcustomclass.cpp` ```cpp #include <QTextStream> #include <QList> #include <algorithm> #include "book.h" bool compareByTitle(const Book &b1, const Book &b2) { return b1.getTitle() < b2.getTitle(); } int main(void) { QTextStream out(stdout); QList<Book> books = { Book("Jack London", "The Call of the Wild"), Book("Honoré de Balzac", "Father Goriot"), Book("Leo Tolstoy", "War and Peace"), Book("Gustave Flaubert", "Sentimental education"), Book("Guy de Maupassant", "Une vie"), Book("William Shakespeare", "Hamlet") }; std::sort(books.begin(), books.end(), compareByTitle); for (Book book : books) { out << book.getAuthor() << ": " << book.getTitle() << endl; } } ``` 在示例中,我們創建了一些書本對象,并使用`std::sort`算法對其進行排序。 ```cpp bool compareByTitle(const Book &b1, const Book &b2) { return b1.getTitle() < b2.getTitle(); } ``` `compareByTitle()`是排序算法使用的比較功能。 ```cpp std::sort(books.begin(), books.end(), compareByTitle); ``` `std::sort`算法按書名對列表中的書進行排序。 輸出: ```cpp $ ./sortcustomclass Honoré de Balzac: Father Goriot William Shakespeare: Hamlet Gustave Flaubert: Sentimental education Jack London: The Call of the Wild Guy de Maupassant: Une vie Leo Tolstoy: War and Peace ``` 在本章中,我們使用了 Qt 的容器。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看