Presentation is loading. Please wait.

Presentation is loading. Please wait.

Containers QVector, QList, QLinkedList, QMap, QHash, QByteArray, QString and QVariant.

Similar presentations


Presentation on theme: "Containers QVector, QList, QLinkedList, QMap, QHash, QByteArray, QString and QVariant."— Presentation transcript:

1 Containers QVector, QList, QLinkedList, QMap, QHash, QByteArray, QString and QVariant

2 QVector QVector<double> vector;
vector.insert(1, 3, 9.9); // vector: [2.718, 9.9, 9.9, 9.9, 1.442, ] QVector<QString> vector(0); vector.append("one"); vector.append("two"); vector.append("three"); // vector: ["one", "two", "three"]

3 QVector for (i=0; i<vector.size(); i++) {
qDebug() << vector.at(i); qDebug() << vector[i]; }

4 QList QList<QString> list;
list << "one" << "two" << "three"; // list: ["one", "two", "three"] if (list[0] == "one") { list[0] = "zero"; } for (int i = 0; i < list.size(); ++i) { if (list.at(i) == "Jane") { cout << "Found Jane at " << i << endl;

5 QList STL-Style QList<QString> list;
list << "A" << "B" << "C" << "D"; QList<QString>::iterator i; for (i = list.begin(); i != list.end(); ++i) { *i = (*i).toLower(); }

6 QList Java Style QList<QString> list;
list << "A" << "B" << "C" << "D"; QListIterator<QString> i(list); while (i.hasNext()) { qDebug() << i.next(); } QListIterator<QString> j(list); j.toBack(); while (j.hasPrevious()) { qDebug() << j.previous();

7 QList Iterator Error QList<QString> list;
list << "A" << "B" << "C" << "D"; QList<QString>::iterator i; QList<QString>::iterator next; for (i = list.begin(); i != list.end(); ++i) next = i+1; list.erace(i); qDebug() << *next; } THIS WILL CRASH!!!

8 Qt Containers object requirements
The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator. class Employee { public: Employee() {} Employee(const Employee &other); Employee &operator=(const Employee &other); ... };

9 foreach QList<QString> list; QString str; foreach (str, list) {
qDebug() << str; }

10 QMap QMap<QString, int> map; map["one"] = 1; map["three"] = 3;
map["seven"] = 7; ... map.insert("twelve", 12); int num1 = map["thirteen"]; // insert & return 0 int num2 = map.value("thirteen"); // return 0

11 QMap Iterators QMapIterator<QString, int> i(map);
while (i.hasNext()) { i.next(); cout << i.key() << ": " << i.value() << endl; }

12 QMultiMap map.insert("plenty", 100); map.insert("plenty", 2000);
// map.value("plenty") == 2000 insertMulti() QList<int> values = map.values("plenty"); for (int i = 0; i < values.size(); ++i) cout << values.at(i) << endl;

13 QHash Similar to QMap: - QHash provides faster lookups than QMap.
- When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered. - The key type of a QMap must provide operator<(). The key type of a QHash must provide operator==() and a global hash function called qHash()

14 QStringList QStringList is an extended QList<QString>
QStringList tmp; tmp << “hello” << “Qt” << “World”; qDebug() << tmp.join(“ “);


Download ppt "Containers QVector, QList, QLinkedList, QMap, QHash, QByteArray, QString and QVariant."

Similar presentations


Ads by Google