gpt4 book ai didi

Qt笔记-QMap自定义键(key)

转载 作者:知者 更新时间:2024-03-12 23:07:00 27 4
gpt4 key购买 nike

如下key值:

struct MyKey{

    MyKey(QString key1, int key2){

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

    QString key1;
    int key2;
};

自定义key时会出现如下问题:

需要重写左操作符号,如下:

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;
};

所有源码如下:

#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();
}

调试截图如下:

源码打包下载地址:

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com