自定义类型

  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/

Qt自定义类型使用QHash等算法(Qt已经自定义了34种类型,包括int, QString, QDate等基本数据类型)的更多相关文章

  1. QSet使用及Qt自定义类型使用QHash等算法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSet使用及Qt自定义类型使用QHash等算法     本文地址:http://techie ...

  2. Python 打包的现状:包的三种类型

    英文 | The state of Python Packaging[1] 原作 | BERNAT GABOR 译者 | 豌豆花下猫 声明 :本文获得原作者授权翻译,转载请保留原文出处,请勿用于商业或 ...

  3. Qt 学习之路 2(53):自定义拖放数据

    Qt 学习之路 2(53):自定义拖放数据 豆子  2013年5月26日  Qt 学习之路 2  13条评论上一章中,我们的例子使用系统提供的拖放对象QMimeData进行拖放数据的存储.比如使用QM ...

  4. Qt 学习之路 2(50):自定义可编辑模型

    Home / Qt 学习之路 2 / Qt 学习之路 2(50):自定义可编辑模型 Qt 学习之路 2(50):自定义可编辑模型 豆子 2013年5月13日 Qt 学习之路 2 13条评论 上一章我们 ...

  5. Qt 学习之路 2(49):自定义只读模型

    Qt 学习之路 2(49):自定义只读模型 豆子 2013年5月5日 Qt 学习之路 2 18条评论 model/view 模型将数据与视图分割开来,也就是说,我们可以为不同的视图,QListView ...

  6. Qt 学习之路 2(23):自定义事件

    Qt 学习之路 2(23):自定义事件  豆子  2012年10月23日  Qt 学习之路 2  21条评论 尽管 Qt 已经提供了很多事件,但对于更加千变万化的需求来说,有限的事件都是不够的.例如, ...

  7. DES加解密算法Qt实现

      算法解密qt加密table64bit [声明] (1) 本文源码 大部分源码来自:DES算法代码.在此基础上,利用Qt编程进行了改写,实现了DES加解密算法,并添加了文件加解密功能.在此对署名为b ...

  8. Qt 学习之路 2(5):自定义信号槽

    Home / Qt 学习之路 2 / Qt 学习之路 2(5):自定义信号槽 Qt 学习之路 2(5):自定义信号槽  豆子  2012年8月24日  Qt 学习之路 2  131条评论 上一节我们详 ...

  9. 常见的几个Qt编程问题的处理(转自QT中文论坛)(挺实用的)

    1.如何在窗体关闭前自行判断是否可关闭答:重新实现这个窗体的closeEvent()函数,加入判断操作 void MainWindow::closeEvent(QCloseEvent*event){i ...

随机推荐

  1. caioj 1070 动态规划入门(二维一边推3:字符距离)(最长公共子序列拓展)

    复制上一题总结 caioj 1069到1071 都是最长公共字序列的拓展,我总结出了一个模型,屡试不爽    (1) 字符串下标从1开始,因为0用来表示字符为空的情况,而不是第一个字符     (2) ...

  2. 高性能网络编程 - select系统调用

         IO复用使得程序可以同一时候监听多个文件描写叙述符,比方client须要同一时候处理用户输入和网络连接,server端须要同一时候处理监听套接字和连接套接字,select系统调用可以使得我们 ...

  3. X的追求道路

    X的追求道路 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 X在大家的帮助下最终找到了一个妹纸,于是開始了漫漫的追求之路,那 ...

  4. swift具体解释之八---------------下标脚本

    swift具体解释之八-----下标脚本 下标脚本 能够定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中.能够觉得是訪问对象.集合或序列的快捷方式.不须要 ...

  5. 关于APP上架制作二维码相关

    1.安卓版本APP上架并生成二维码问题:安卓版本上架国内市场,这个情况比较复杂一些,比如百度,网址是以上传APP生成的一个编号来进行的,每次升级更新后都发生了变化,也就相当于每次升级后网址发生改变(比 ...

  6. 洛谷P3807 【模板】卢卡斯定理exgcd

    题目背景 这是一道模板题. 题目描述 给定n,m,p(1\le n,m,p\le 10^51≤n,m,p≤105 ) 求 C_{n+m}^{m}\ mod\ pCn+mm​ mod p 保证P为pri ...

  7. OpenSUSE42.3 leap 软件源设置

    一.OpenSUSE软件源介绍: 1.默认已经加入了官方的软件源,不过我们自己也可以根据需要添加很多非官方软件源,添加软件源时要注意: 非官方源可能包含一些试验中的不稳定的软件包    不同的软件源之 ...

  8. phantomjs 设置代理

    phantomjs 可通过以下设置代理ip #coding=utf-8import osimport reimport timeimport requestsfrom scrapy.selector ...

  9. Node测试文章收藏

    1.Nodejs实战—测试Node程序 讲解了TDD与BDD,TDD的基本原则,单元测试常用框架及使用,例如assert断言库,  chai断言库,  should.js断言库,断言库搭配测试框架(m ...

  10. 客户端本地存储(cookie、web Storage、vuex)选择

    一.cookie .localStorage .sessionStorage .vuex 比较 cookie   4K    有时效性    可服务器传递 cookie是由服务器产生,存储在客户端的一 ...