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>
QSet
QSet provides a single-valued mathematical set with fast lookups. The values are stored in an unspecified order.
#include <QSet>
#include <algorithm>
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>
自定义类排序
#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)
}