自定义类型

  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. stl之set集合容器应用基础

    set集合容器使用一种称为红黑树(Red-Black Tree) 的平衡二叉检索树的数据结构,来组织泛化的元素数据.每一个节点包括一个取值红色或黑色的颜色域.以利于进行树的平衡处理.作为节点键值的元素 ...

  2. 封装html代码块到js函数中

    有时候想把公共的html封装起来,怎么处理呢? 好多页面都用到,不可能每个页面都写,这样就会有冗余,并且不好统一处理. 那就用js来重构html吧. 代码案例如下: <footer class= ...

  3. P1824 进击的奶牛

    题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000 ...

  4. Codeforces 701A. Cards(水)

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. C#初学者使用file.creat()创建文件后,显示正由另一进程使用

    string sourcePhotoPath = this.GetUserSelectedPhoto(); if(sourcePhotoPath == null) { return; } string ...

  6. 动态调用WebService-获取天气

    string url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; string[] args ...

  7. JavaScript进阶之执行上下文和执行栈

    js引擎的执行过程 执行上下文和执行栈属于js引擎的执行过程的预编译阶段. 执行上下文(Execution Context) 执行上下文是当前 JavaScript 代码被解析和执行时所在环境的抽象概 ...

  8. Mirai僵尸网络重出江湖

    近年数度感染数十万台路由器的僵尸网络程序Mirai,虽然原创者已经落网判刑,但是Mirai余孽却在网络上持续变种,现在安全人员发现,Mirai已经将焦点转向Linux服务器了. 安全公司Netcout ...

  9. Mybatis使用注解进行增删改查

    // 增public interface StudentMapper{ @Insert("insert into student (stud_id, name, email, addr_id ...

  10. java8新增特性(一)---Lambda表达式

    Lambda表达式也成为闭包,是java语言层次上的改变,Lambda同意把函数作为一个方法的參数(函数作为參数传递进方法中),或者把代码看成数据.函数式程序猿对这一概念非常熟悉. 在JVM平台上有非 ...