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 ...
随机推荐
- 紫书 例题 10-12 UVa 1637(概率计算)
以9元组来代表当前状态,每一元是每一堆剩下的牌数 枚举当前状态所有可以拿掉牌的情况,然后递归下去求 概率,当牌拿完的时候概率为1 那么这里的实现非常的秀,用到了vector来代表9元组 然后还用到了m ...
- 【Oracle错误集锦】:ORA-00119 & ORA-00132
有时候老天就是爱和你开玩笑,昨天好不easy配置好Oracle.可以用PL/SQL正常登录使用,今天突然就不行了.而且错误十分诡异,没有提示什么错误代码.输入usernamepassword,点击登录 ...
- Scala具体解释---------Scala是什么?可伸展的语言!
Scala是什么 Scala语言的名称来自于"可伸展的语言". 之所以这样命名,是由于他被设计成随着使用者的需求而成长.你能够把Scala应用在非常大范围的编程任务上.从写个小脚本 ...
- android CoordinatorLayout使用
一.CoordinatorLayout有什么作用 CoordinatorLayout作为“super-powered FrameLayout”基本实现两个功能: 1.作为顶层布局 2.调度协调子布局 ...
- quartz 添加监听器listener
全局注册,所有Job都会起作用 JobCountListener listener = new JobCountListener(); sched.getListenerManager().addJo ...
- P2038 无线网络发射器选址
题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129 条东西向街道和129 条南北向街道所形成的网格状,并且相邻 ...
- Python(八) 正则表达式与JSON
一.初识正则表达式 正则表达式 是一个特殊的字符序列,一个字符串是否与我们所设定的这样的字符序列,相匹配 快速检索文本.实现替换文本的操作 json(xml) 轻量级 web 数据交换格式 impor ...
- javaScript学习之正则表达式初探
正则表达式 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式通常被用来检索.替换那些符 ...
- BZOJ2636: crisis(可持久化线段树)
传送门: 解题思路: 题目描述是一大坑点,cancel后面是直接加ask或者redo的. 那么就可以愉快地可持久化了. 注意需要支持区间修改,那么就只需要在再次更新这个点的时候将标记储存在新的儿子中. ...
- lsblk---列出所有可用块设备的信息,
lsblk命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息.块设备有硬盘,闪存盘,cd-ROM等等.lsblk命令包含在util-linux-ng包中,现 ...