QSet使用及Qt自定义类型使用QHash等算法
本文标题:QSet使用及Qt自定义类型使用QHash等算法 本文地址:http://techieliang.com/2017/12/580/
1. 介绍
Qt提供的一个单值的数学集合的快速查找容器,使用方式与QList相同,但其内元素不会有重复。详细说明见 官方文档
注意,此容器实现方式是基于哈希表,而不是红黑树,若使用自定义类必须提供对应的hash函数:
QSet‘s value data type must be an assignable data type. You cannot, for example, store a QWidget as a value; instead, store a QWidget *. In addition, the type must provide
operator==(), and there must also be a global qHash() function that returns a hash value for an argument of the key’s type. See the QHash documentation for a list of types supported by qHash().
2. 简单范例
- QSet <QString> m_set;
- m_set.insert("1");
- m_set.insert("3");
- m_set.insert("2");//注意插入顺序
- auto b_set = m_set.begin();
- qDebug()<<m_set.size();
- qDebug()<<*b_set++;
- qDebug()<<*b_set++;
- qDebug()<<*b_set++;//注意存储顺序
- m_set.insert("1");//插入重复的
- b_set = m_set.begin();
- qDebug()<<m_set.size();
- qDebug()<<*b_set++;
- qDebug()<<*b_set++;
- qDebug()<<*b_set++;
结果
- 3
- "1"
- "2"
- "3"
- 3
- "1"
- "2"
- "3"
3. 自定义类型
- #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类。
QSet使用及Qt自定义类型使用QHash等算法的更多相关文章
- Qt自定义类型使用QHash等算法(Qt已经自定义了34种类型,包括int, QString, QDate等基本数据类型)
自定义类型 #include <QCoreApplication> #include <QSet> #include <QDebug> class testCust ...
- Qt之创建自定义类型
摘要: 简述 当使用Qt创建用户界面时,特别是那些带有特殊控制和特征的界面时,开发者通常需要创建新数据类型来扩展或替换Qt现有的的值类型集合. 标准类型,比如:QSize.QColor和QString ...
- qt 如何注册自定义类型?
如何声明自定义类型 如果仅仅在 QVariant 中使用,则仅需要使用 Q_DECLARE_METATYPE 宏进行声明即可. class Custom_ : public QObject { Q_O ...
- Q_DECLARE_METATYPE(继承QObject的类都已经自动注册),注册后的类型可以作为QVariant的自定义类型
简介 这个宏用来注册一个类(含默认构造.默认析构.拷贝构造函数)为QMetaType类型 ,注册后的类型可以作为QVariant的自定义类型. 这个宏应该放在类或者结构体外面的下面,也可以放在一个非公 ...
- Qt 自定义事件详细实例(继承QEvent,然后QCoreApplication::postEvent()、sendEvent())
创建用户事件 创建一个自定义类型的事件,首先需要有一个事件号,其值通常大于QEvent::User.为了传递事件信息,因此必须编写自定义的事件类,该事件类从QEvent继承. 编写用户事件:编写用户事 ...
- Qt 自定义事件(三种方法:继承QEvent,然后Send Post就都可以了,也可以覆盖customEvent函数,也可覆盖event()函数)
Qt 自定义事件很简单,同其它类库的使用很相似,都是要继承一个类进行扩展.在 Qt 中,你需要继承的类是 QEvent. 继承QEvent类,你需要提供一个QEvent::Type类型的参数,作为自定 ...
- Qt 自定义事件
Qt 自定义事件很简单,同其它类库的使用很相似,都是要继承一个类进行扩展.在 Qt 中,你需要继承的类是 QEvent. 继承QEvent类,你需要提供一个QEvent::Type类型的参数,作为自定 ...
- 《精通C#》自定义类型转化-扩展方法-匿名类型-指针类型(11.3-11.6)
1.类型转化在C#中有很多,常用的是int类型转string等,这些都有微软给我们定义好的,我们需要的时候直接调用就是了,这是值类型中的转化,有时候我们还会需要类类型(包括结构struct)的转化,还 ...
- C#简单问题,不简单的原理:不能局部定义自定义类型(不含匿名类型)
今天在进行代码测试时发现,尝试在一个方法中定义一个委托,注意是定义一个委托,而不是声明一个委托变量,在编写的时候没有报错,VS也能智能提示,但在编译时却报语法不完整,缺少方括号,但实际查询并没有缺少, ...
随机推荐
- string函数库的原型
#ifndef __HAVE_ARCH_STRCPY /** * strcpy - Copy a %NUL terminated string * @dest: Where to copy the s ...
- etl是什么
ETL (数据仓库技术) ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).交互转换(transform).加载(load)至目的 ...
- 数据结构与算法之排序(2)选择排序 ——in dart
选择排序的算法复杂度与冒泡排序类似,其比较的时间复杂度仍然为O(N2),但减少了交换次数,交换的复杂度为O(N),相对冒泡排序提升很多.算法的核心思想是每次选出一个最小的,然后与本轮循环中的第一个进行 ...
- Redis安装——在CentOS7下的安装
参考自:https://linux.cn/article-6719-1.html 一.安装 首先通过xshell5先登陆来到字符界面(xshell通过SSH连接请参见之前随笔) 先下载redis,这里 ...
- 20155234 2016-2017-2第十周《Java学习笔记》学习总结
20155234第十周<Java学习笔记>学习总结 教材学习内容总结 网络编程 在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定 ...
- Ruby 配置vimrc
https://ruby-china.org/topics/19315 mv ~/Downloads/vim-distinguished-develop/colors/*.vim ~/.vim/col ...
- rman的基于窗口的备份保留策略学习
例如: rman>configure retention policy to recovery window of 7 days; 那么就是说,至少要使得保留下来的备份,可以支持恢复到从当前回溯 ...
- CF 1083 C. Max Mex
C. Max Mex https://codeforces.com/contest/1083/problem/C 题意: 一棵$n$个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~ ...
- P3940 分组
P3940 分组 https://www.luogu.org/problemnew/show/P3940 官方题解http://pan.baidu.com/s/1eSAMuXk 分析: 并查集. 首先 ...
- Zabbix学习之路(七)之Nginx的状态监控
1.安装nginx [root@linux-node2 ~]# yum install -y nginx [root@linux-node2 ~]# mkdir /etc/zabbix/zabbix_ ...