Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » QT-C++ » Example of QMap

QT-C++ Tutorials, Examples, Questions & Projects

Main.cpp


#include <QTextStream>
#include <QMap>

int main(void) {

    QTextStream out(stdout);
    QMap<int, QString> items = { {1, "USA"}, {2, "Mexico"} };

    //inserting new element in map
    items.insert(3, "India");

    QList<int> map_keys = items.keys();

    out << "List of all Keys:" << endl;

    for (int key : map_keys) {
        out << key << endl;
    }

    QList<QString> map_values = items.values();

    out << "
List of all value:" << endl;
    //iterate using foreach loop
    for (QString val : map_values) {
        out << val << endl;
    }

    //exampe of Iterator
    QMapIterator<int, QString> it(items);

    out << "
List of Pairs:" << endl;

    while (it.hasNext()) {
        it.next();
        out << it.key() << ": " << it.value() << endl;
    }

    return 0;
}

Project view in QT Creator

Project file

Output of the program