• Qt自定义类型使用QHash等算法(Qt已经自定义了34种类型,包括int, QString, QDate等基本数据类型)


    自定义类型

    1. #include <QCoreApplication>
    2. #include <QSet>
    3. #include <QDebug>
    4. class testCustomTypeByQSet {
    5. public:
    6. testCustomTypeByQSet(int v):m_value(v){};
    7. int value() const{
    8. return m_value;
    9. }
    10. bool operator == (const testCustomTypeByQSet &t) const {
    11. return (m_value==t.value());
    12. }
    13. private:
    14. int m_value;
    15. };
    16. uint qHash(const testCustomTypeByQSet &key, uint seed = 0) {
    17. return key.value();
    18. }
    19. int main(int argc, char *argv[]) {
    20. QCoreApplication a(argc, argv);
    21. QSet<testCustomTypeByQSet> m_set;
    22. m_set.insert(testCustomTypeByQSet(1));
    23. m_set.insert(testCustomTypeByQSet(3));
    24. m_set.insert(testCustomTypeByQSet(2));
    25. m_set.insert(testCustomTypeByQSet(7));
    26. m_set.insert(testCustomTypeByQSet(-1));
    27. auto b_set = m_set.begin();
    28. qDebug()<<m_set.size();
    29. qDebug()<<(*b_set++).value();
    30. qDebug()<<(*b_set++).value();
    31. qDebug()<<(*b_set++).value();
    32. qDebug()<<(*b_set++).value();
    33. qDebug()<<(*b_set++).value();
    34. return 0;
    35. }

    结果

    1. 5
    2. -1
    3. 1
    4. 2
    5. 3
    6. 7

    qt自身的类已经实现了对应的qHash,存储在QHash类中,详见官方文档

    1. uint qHash(const QXmlNodeModelIndex &index)
    2. uint qHash(const QUrl &url, uint seed = 0)
    3. uint qHash(const QDateTime &key, uint seed = 0)
    4. uint qHash(const QDate &key, uint seed = 0)
    5. uint qHash(const QTime &key, uint seed = 0)
    6. uint qHash(const QPair<T1, T2> &key, uint seed = 0)
    7. uint qHash(const std::pair<T1, T2> &key, uint seed = 0)
    8. uint qHash(char key, uint seed = 0)
    9. uint qHash(uchar key, uint seed = 0)
    10. uint qHash(signed char key, uint seed = 0)
    11. uint qHash(ushort key, uint seed = 0)
    12. uint qHash(short key, uint seed = 0)
    13. uint qHash(uint key, uint seed = 0)
    14. uint qHash(int key, uint seed = 0)
    15. uint qHash(ulong key, uint seed = 0)
    16. uint qHash(long key, uint seed = 0)
    17. uint qHash(quint64 key, uint seed = 0)
    18. uint qHash(qint64 key, uint seed = 0)
    19. uint qHash(float key, uint seed = 0)
    20. uint qHash(double key, uint seed = 0)
    21. uint qHash(const QChar key, uint seed = 0)
    22. uint qHash(const QByteArray &key, uint seed = 0)
    23. uint qHash(const QBitArray &key, uint seed = 0)
    24. uint qHash(const QString &key, uint seed = 0)
    25. uint qHash(const QStringRef &key, uint seed = 0)
    26. uint qHash(QLatin1String key, uint seed = 0)
    27. uint qHash(const T *key, uint seed = 0)
    28. uint qHash(const QHash<Key, T> &key, uint seed = 0)
    29. uint qHash(const QSet<T> &key, uint seed = 0)
    30. uint qHash(const QVersionNumber &key, uint seed = 0)
    31. uint qHash(const QSslCertificate &key, uint seed = 0)
    32. uint qHash(QSslEllipticCurve curve, uint seed = 0)
    33. uint qHash(const QSslError &key, uint seed = 0)
    34. uint qHash(const QGeoCoordinate &coordinate, uint seed = 0)

    同时也在对应类中做了“==”的重载操作符,比如QString类

    http://techieliang.com/2017/12/580/

  • 相关阅读:
    洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX 解题报告
    牛客练习赛 小D的剑阵 解题报告
    牛客练习赛 小A与最大子段和 解题报告
    牛客练习赛 小D的Lemon 解题报告
    牛客练习赛 小A与任务 解题报告
    洛谷 P1452 Beauty Contest 解题报告
    洛谷 P4100 [HEOI2013]钙铁锌硒维生素 解题报告
    【模板】矩阵求逆
    洛谷 P4097 [HEOI2013]Segment 解题报告
    连接数据库的增删改查
  • 原文地址:https://www.cnblogs.com/findumars/p/9256258.html
Copyright © 2020-2023  润新知