Introduction:
the container class and iterator classes provide the ability to delete, modify, insert, and save data.
these two categories can store data of various data types in an element-based unit.
For example:
Do not use the container classes provided by QT: If you want to store data of type qstring into an array, we must know beforehand how many elements need to be stored.
Use the container class provided by QT: When a container class stores QString data in a variable-sized array, you can use Qvector<qstring> 's container class to insert, delete, and modify data. It's very convenient. qt Container class
The container classes provided by QT are very simple, lightweight and safe. QT also provides the Foreach keyword, which makes it easy to access the elements stored by the container class.
QT Container Class (table)
A container class can combine multiple data sets. such as: Qmap<qstring, qlist<int>>.
Style:
a container class is used to store data, while an iterator class is used to access the aggregation pattern for a particular item stored by the container class. Depending on the style of the iterator, it can be divided into Java style and STL style.
The personal sense of the Java-style iterator pattern is more convenient than the STL-style iterator pattern. Java-style iterators have two data types for each container class, namely read-only access mode and read- Write access mode .
Container |
Read-only iterator |
Read-Write iterators |
Qlist<t>,qqueue<t> |
Qlistiterator<t> |
Qmutablelistiterator<t> |
Qlinklist<t> |
Qlinklistiterator<t> |
Qmutablelinklistiterator<t> |
Qvector<t>,qstack<t> |
Qvectoriterator<t> |
Qmutablevectoriterator<t> |
Qset<t> |
Qsetiterator<t> |
Qmutablesetiterator<t> |
Qmap<key,t>,qmultimap<key,t> |
Qmapiterator<key,t> |
Qmutablemapiterator<key,t> |
Qhash<key,t>,qmultihash<key,t> |
Qhashiterator<key,t> |
Qmutablehashiterator<key,t> |
Java-style container class instances:
#include <QCoreApplication>
#include <QDebug>
#include <QList>
#include < qlistiterator>
int main (int argc, char *argv[])
{
qcoreapplication A (argc, argv);
qlist<qstring>list;
list<< "123" << "456" << "789";
Qlistiterator<qstring>m (list);
while (M.hasnext ()) {
qdebug () <<m.next ();
}
Qdebug () << "******";
while (M.hasprevious ())
{
qdebug () <<m.previous ();
}
return a.exec ();
}
Operation Result:
foreach keyword:
Accessing iterators by using the foreach keyword
Format: foreach (variable, container)
Note: Theforeach keyword is not provided by the C + + standard, but is provided by QT. Example:
#include <QCoreApplication>
#include <QDebug>
#include <QList>
#include < qlistiterator>
int main (int argc, char *argv[])
{
qcoreapplication A (argc, argv);
qlist<qstring>list;
list<< "123" << "456" << "789";
QString str;
foreach (str, list) {
qdebug () <<str;
}
return a.exec ();
}
Operation Result:
Each container class provides functions that can be obtained by viewing the QT assistant.