其他分享
首页 > 其他分享> > Qt Container

Qt Container

作者:互联网

qt containers

QVector

 #include <QVector>
 QVector values = {1,2,3,4,5};
 values.first();
 values.last();
 values.append(6);
 values.preppend(0);
 for(int val : values){
     
 }
QList
 #include <QList>
 #include <algorithm>
 //初始化
 QList<QString> authors = {"a","b"}
 //大小
 authors.size();
 //遍历
 for(int i = 0;i<authors.size();i++){
     authors.at(i);
 }
 //增加
 autors << "c" << "d";
 //排序
 std::sort(authors.begin(),authors.end());
 //遍历
 for(QString author: authors){
 }
QSet

QSet provides a single-valued mathematical set with fast lookups. The values are stored in an unspecified order.

 #include <QSet>
 #include <algorithm>
 //初始化
 QSet<QString> col1 = {"yellow", "red", "blue"};
 QSet<QString> col2 = {"blue", "pink", "orange"};
 //大小
 col1.size();
 //插入
 col1.insert("brown");
 //连接
 col1.unite(col2);
 //遍历
 for(QString val : col1){
     
 }
 //排序
 std:sort(col1.begin(),col1.end());
 //遍历

The QSet is used to store colours in the example. It makes no sense to have one colour value specified more times.

QMap
 #include <QMap>
 ​
 //初始化
 QMap<QString, int> items = {{"coin",5},{"books",3}};
 //插入
 items.insert("bottles",7);
 //值
 items.values();
 //键
 items.keys();
 //遍历
 QMapIterator<QString, int> it(items);
 whie(it.hasNext()){
     it.next();
     it.key();
     it.value();
 }
自定义类排序
 #include <QList>
 #include <algorithm>
 #include "book"
 ​
 bool compareByTitle(const Book &b1, const Book &b2){
     return b1.getTitle() < b2.getTitle();
 }
 int main(void){
     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(),compareTyTitle)
 }

 

标签:Container,Qt,items,col1,Book,values,authors,include
来源: https://www.cnblogs.com/mtgold/p/15528525.html