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-7 UVa 10539(long long + 素数筛)
注意要开long long 如果int * int会炸 那么久改成long long * int #include<cstdio> #include<vector> #incl ...
- unity C# 获取有关文件、文件夹和驱动器的信息
class FileSysInfo { static void Main() { // You can also use System.Environment.GetLogicalDrives to ...
- linux基础入门(二)命令
原创作品,允许转载,转载时请务必声明作者信息和本声明. https://www.cnblogs.com/zhu520/p/10732334.html =[本人小白,有错指出.谢谢! 一:使用Secur ...
- .ashx 实现自动路由和参数填充
在Mvc中访问控制器,参数填充和路由控制都非常方便,但之前项目用的是webFrom,和js交互的ashx页面,路由非常麻烦要根据传进来关键字来做switch,参数填充更坑,要一个一个去form中取出来 ...
- 协变 & 逆变
都跟里氏替换原则有关. 协变:你可以用一个子类对象去替换相应的一个父类对象,这是完全符合里氏替换原则的,和协(谐)的变.如:用Swan替换Bird. 逆变:你可以用一个父类对象去替换相应的一个子类对象 ...
- 弯道超车,换一个思路,避免addEventListener为同一个元素重复赋予事件
addEventListener可以给同一个元素赋予多次同一个事件. 执行一次,就多一次事件效果.这不是我想要的. window.onload = function(){ var box = docu ...
- lightoj--1008--Fibsieve`s Fantabulous Birthday(水题)
Fibsieve`s Fantabulous Birthday Time Limit: 500MS Memory Limit: 32768KB 64bit IO Format: %lld &a ...
- centos7 zabbix3.4.6显示中文乱码问题
工具 : winscp (Linux Windows 传输文件工具) 当部署完zabbix然后显示中文会出现如下图 然后此时先去windows的文字目录如 C:\Windows\Fonts 随便托个字 ...
- JWT使用1
JSON Web Token - 在Web应用间安全地传递信息 转载收藏于:http://blog.leapoahead.com/2015/09/06/understanding-jwt/ JSON ...
- Inter-process Communication (IPC)
For Developers > Design Documents > Inter-process Communication (IPC) 目录 1 Overview 1.1 I ...