QMap中的insert和insertMulti的区别

QMap中的insert和insertMulti的区别

  • insert对相同key会重写value
#include <QCoreApplication>
#include <QMap>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QMap<int, QString> make;
    make.insert(1, "11");
    make.insert(2, "22");
    make.insert(2, "2222");
    make.insert(3, "33");
    foreach (QString item, make)
    {
        qDebug() << item;
    }
    return a.exec();
}

  • insertMulti对相同key不会重写value,而是重新创建一项
#include <QCoreApplication>
#include <QMap>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QMap<int, QString> make;
    make.insertMulti(1, "11");
    make.insertMulti(2, "22");
    make.insertMulti(2, "2222");
    make.insertMulti(3, "33");
    foreach (QString item, make)
    {
        qDebug() << item;
    }
    return a.exec();
}

posted @ 2020-06-24 14:26  不知云深处  阅读(2254)  评论(0编辑  收藏  举报