当前位置:网站首页>Qt Notes - qmap Custom key

Qt Notes - qmap Custom key

2022-06-22 16:58:00 It1995

Comme suitkeyValeur:

struct MyKey{

    MyKey(QString key1, int key2){

        this->key1 = key1;
        this->key2 = key2;
    }

    QString key1;
    int key2;
};

PersonnalisationkeyLes problèmes suivants se posent:

Le symbole d'action gauche doit être dépassé,Comme suit:

struct MyKey{

    MyKey(QString key1, int key2){

        this->key1 = key1;
        this->key2 = key2;
    }

    bool operator < (const MyKey &key) const{

		return std::tie(key1, key2) < std::tie(key.key1, key.key2);
    }

    QString key1;
    int key2;
};

Toutes les sources sont les suivantes::

#include <QCoreApplication>
#include <QMap>
#include <QDebug>

struct MyKey{

    MyKey(QString key1, int key2){

        this->key1 = key1;
        this->key2 = key2;
    }

    bool operator < (const MyKey &key) const{

		return std::tie(key1, key2) < std::tie(key.key1, key.key2);
    }

    QString key1;
    int key2;
};

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

    QMap<MyKey, QString> map;
    map.insert(MyKey("10086", 0), "value1");
    map.insert(MyKey("10086", 1), "value2");
    map.insert(MyKey("10086", 2), "value3");
    map.insert(MyKey("10010", 1), "value4");
    map.insert(MyKey("10010", 2), "value5");


    return a.exec();
}

Voici une capture d'écran de débogage:

Code source package download address:

Qt/QMapKeyCustom at master · fengfanchen/Qt · GitHubThe knowledge point of Qt. Contribute to fengfanchen/Qt development by creating an account on GitHub.https://github.com/fengfanchen/Qt/tree/master/QMapKeyCustom

 

原网站

版权声明
本文为[It1995]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221534365741.html