Qt: Container class and Iterator class, foreach keyword

Source: Internet
Author: User
Tags foreach
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.