自定义类型

  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. 紫书 习题 10-7 UVa 10539(long long + 素数筛)

    注意要开long long 如果int * int会炸 那么久改成long long * int #include<cstdio> #include<vector> #incl ...

  2. unity C# 获取有关文件、文件夹和驱动器的信息

    class FileSysInfo { static void Main() { // You can also use System.Environment.GetLogicalDrives to ...

  3. linux基础入门(二)命令

    原创作品,允许转载,转载时请务必声明作者信息和本声明. https://www.cnblogs.com/zhu520/p/10732334.html =[本人小白,有错指出.谢谢! 一:使用Secur ...

  4. .ashx 实现自动路由和参数填充

    在Mvc中访问控制器,参数填充和路由控制都非常方便,但之前项目用的是webFrom,和js交互的ashx页面,路由非常麻烦要根据传进来关键字来做switch,参数填充更坑,要一个一个去form中取出来 ...

  5. 协变 & 逆变

    都跟里氏替换原则有关. 协变:你可以用一个子类对象去替换相应的一个父类对象,这是完全符合里氏替换原则的,和协(谐)的变.如:用Swan替换Bird. 逆变:你可以用一个父类对象去替换相应的一个子类对象 ...

  6. 弯道超车,换一个思路,避免addEventListener为同一个元素重复赋予事件

    addEventListener可以给同一个元素赋予多次同一个事件. 执行一次,就多一次事件效果.这不是我想要的. window.onload = function(){ var box = docu ...

  7. lightoj--1008--Fibsieve`s Fantabulous Birthday(水题)

    Fibsieve`s Fantabulous Birthday Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld &a ...

  8. centos7 zabbix3.4.6显示中文乱码问题

    工具 : winscp (Linux Windows 传输文件工具) 当部署完zabbix然后显示中文会出现如下图 然后此时先去windows的文字目录如 C:\Windows\Fonts 随便托个字 ...

  9. JWT使用1

    JSON Web Token - 在Web应用间安全地传递信息 转载收藏于:http://blog.leapoahead.com/2015/09/06/understanding-jwt/ JSON ...

  10. Inter-process Communication (IPC)

    For Developers‎ > ‎Design Documents‎ > ‎ Inter-process Communication (IPC) 目录 1 Overview 1.1 I ...