Qt自定义类型使用QHash等算法(Qt已经自定义了34种类型,包括int, QString, QDate等基本数据类型)
自定义类型
- #include <QCoreApplication>
- #include <QSet>
- #include <QDebug>
- class testCustomTypeByQSet {
- public:
- testCustomTypeByQSet(int v):m_value(v){};
- int value() const{
- return m_value;
- }
- bool operator == (const testCustomTypeByQSet &t) const {
- return (m_value==t.value());
- }
- private:
- int m_value;
- };
- uint qHash(const testCustomTypeByQSet &key, uint seed = 0) {
- return key.value();
- }
- int main(int argc, char *argv[]) {
- QCoreApplication a(argc, argv);
- QSet<testCustomTypeByQSet> m_set;
- m_set.insert(testCustomTypeByQSet(1));
- m_set.insert(testCustomTypeByQSet(3));
- m_set.insert(testCustomTypeByQSet(2));
- m_set.insert(testCustomTypeByQSet(7));
- m_set.insert(testCustomTypeByQSet(-1));
- auto b_set = m_set.begin();
- qDebug()<<m_set.size();
- qDebug()<<(*b_set++).value();
- qDebug()<<(*b_set++).value();
- qDebug()<<(*b_set++).value();
- qDebug()<<(*b_set++).value();
- qDebug()<<(*b_set++).value();
- return 0;
- }
结果
- 5
- -1
- 1
- 2
- 3
- 7
qt自身的类已经实现了对应的qHash,存储在QHash类中,详见官方文档:
- uint qHash(const QXmlNodeModelIndex &index)
- uint qHash(const QUrl &url, uint seed = 0)
- uint qHash(const QDateTime &key, uint seed = 0)
- uint qHash(const QDate &key, uint seed = 0)
- uint qHash(const QTime &key, uint seed = 0)
- uint qHash(const QPair<T1, T2> &key, uint seed = 0)
- uint qHash(const std::pair<T1, T2> &key, uint seed = 0)
- uint qHash(char key, uint seed = 0)
- uint qHash(uchar key, uint seed = 0)
- uint qHash(signed char key, uint seed = 0)
- uint qHash(ushort key, uint seed = 0)
- uint qHash(short key, uint seed = 0)
- uint qHash(uint key, uint seed = 0)
- uint qHash(int key, uint seed = 0)
- uint qHash(ulong key, uint seed = 0)
- uint qHash(long key, uint seed = 0)
- uint qHash(quint64 key, uint seed = 0)
- uint qHash(qint64 key, uint seed = 0)
- uint qHash(float key, uint seed = 0)
- uint qHash(double key, uint seed = 0)
- uint qHash(const QChar key, uint seed = 0)
- uint qHash(const QByteArray &key, uint seed = 0)
- uint qHash(const QBitArray &key, uint seed = 0)
- uint qHash(const QString &key, uint seed = 0)
- uint qHash(const QStringRef &key, uint seed = 0)
- uint qHash(QLatin1String key, uint seed = 0)
- uint qHash(const T *key, uint seed = 0)
- uint qHash(const QHash<Key, T> &key, uint seed = 0)
- uint qHash(const QSet<T> &key, uint seed = 0)
- uint qHash(const QVersionNumber &key, uint seed = 0)
- uint qHash(const QSslCertificate &key, uint seed = 0)
- uint qHash(QSslEllipticCurve curve, uint seed = 0)
- uint qHash(const QSslError &key, uint seed = 0)
- uint qHash(const QGeoCoordinate &coordinate, uint seed = 0)
同时也在对应类中做了“==”的重载操作符,比如QString类。
http://techieliang.com/2017/12/580/
Qt自定义类型使用QHash等算法(Qt已经自定义了34种类型,包括int, QString, QDate等基本数据类型)的更多相关文章
- QSet使用及Qt自定义类型使用QHash等算法
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSet使用及Qt自定义类型使用QHash等算法 本文地址:http://techie ...
- Python 打包的现状:包的三种类型
英文 | The state of Python Packaging[1] 原作 | BERNAT GABOR 译者 | 豌豆花下猫 声明 :本文获得原作者授权翻译,转载请保留原文出处,请勿用于商业或 ...
- Qt 学习之路 2(53):自定义拖放数据
Qt 学习之路 2(53):自定义拖放数据 豆子 2013年5月26日 Qt 学习之路 2 13条评论上一章中,我们的例子使用系统提供的拖放对象QMimeData进行拖放数据的存储.比如使用QM ...
- Qt 学习之路 2(50):自定义可编辑模型
Home / Qt 学习之路 2 / Qt 学习之路 2(50):自定义可编辑模型 Qt 学习之路 2(50):自定义可编辑模型 豆子 2013年5月13日 Qt 学习之路 2 13条评论 上一章我们 ...
- Qt 学习之路 2(49):自定义只读模型
Qt 学习之路 2(49):自定义只读模型 豆子 2013年5月5日 Qt 学习之路 2 18条评论 model/view 模型将数据与视图分割开来,也就是说,我们可以为不同的视图,QListView ...
- Qt 学习之路 2(23):自定义事件
Qt 学习之路 2(23):自定义事件 豆子 2012年10月23日 Qt 学习之路 2 21条评论 尽管 Qt 已经提供了很多事件,但对于更加千变万化的需求来说,有限的事件都是不够的.例如, ...
- DES加解密算法Qt实现
算法解密qt加密table64bit [声明] (1) 本文源码 大部分源码来自:DES算法代码.在此基础上,利用Qt编程进行了改写,实现了DES加解密算法,并添加了文件加解密功能.在此对署名为b ...
- Qt 学习之路 2(5):自定义信号槽
Home / Qt 学习之路 2 / Qt 学习之路 2(5):自定义信号槽 Qt 学习之路 2(5):自定义信号槽 豆子 2012年8月24日 Qt 学习之路 2 131条评论 上一节我们详 ...
- 常见的几个Qt编程问题的处理(转自QT中文论坛)(挺实用的)
1.如何在窗体关闭前自行判断是否可关闭答:重新实现这个窗体的closeEvent()函数,加入判断操作 void MainWindow::closeEvent(QCloseEvent*event){i ...
随机推荐
- stl之set集合容器应用基础
set集合容器使用一种称为红黑树(Red-Black Tree) 的平衡二叉检索树的数据结构,来组织泛化的元素数据.每一个节点包括一个取值红色或黑色的颜色域.以利于进行树的平衡处理.作为节点键值的元素 ...
- 封装html代码块到js函数中
有时候想把公共的html封装起来,怎么处理呢? 好多页面都用到,不可能每个页面都写,这样就会有冗余,并且不好统一处理. 那就用js来重构html吧. 代码案例如下: <footer class= ...
- P1824 进击的奶牛
题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000 ...
- Codeforces 701A. Cards(水)
A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- C#初学者使用file.creat()创建文件后,显示正由另一进程使用
string sourcePhotoPath = this.GetUserSelectedPhoto(); if(sourcePhotoPath == null) { return; } string ...
- 动态调用WebService-获取天气
string url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; string[] args ...
- JavaScript进阶之执行上下文和执行栈
js引擎的执行过程 执行上下文和执行栈属于js引擎的执行过程的预编译阶段. 执行上下文(Execution Context) 执行上下文是当前 JavaScript 代码被解析和执行时所在环境的抽象概 ...
- Mirai僵尸网络重出江湖
近年数度感染数十万台路由器的僵尸网络程序Mirai,虽然原创者已经落网判刑,但是Mirai余孽却在网络上持续变种,现在安全人员发现,Mirai已经将焦点转向Linux服务器了. 安全公司Netcout ...
- Mybatis使用注解进行增删改查
// 增public interface StudentMapper{ @Insert("insert into student (stud_id, name, email, addr_id ...
- java8新增特性(一)---Lambda表达式
Lambda表达式也成为闭包,是java语言层次上的改变,Lambda同意把函数作为一个方法的參数(函数作为參数传递进方法中),或者把代码看成数据.函数式程序猿对这一概念非常熟悉. 在JVM平台上有非 ...