版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
本文标题: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. 简单范例

  1. QSet <QString> m_set;
  2. m_set.insert("1");
  3. m_set.insert("3");
  4. m_set.insert("2");//注意插入顺序
  5. auto b_set = m_set.begin();
  6. qDebug()<<m_set.size();
  7. qDebug()<<*b_set++;
  8. qDebug()<<*b_set++;
  9. qDebug()<<*b_set++;//注意存储顺序
  10. m_set.insert("1");//插入重复的
  11. b_set = m_set.begin();
  12. qDebug()<<m_set.size();
  13. qDebug()<<*b_set++;
  14. qDebug()<<*b_set++;
  15. qDebug()<<*b_set++;

结果

  1. 3
  2. "1"
  3. "2"
  4. "3"
  5. 3
  6. "1"
  7. "2"
  8. "3"

3. 自定义类型

  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类

转载请以链接形式标明本文标题和地址:Techie亮博客 » QSet使用及Qt自定义类型使用QHash等算法

QSet使用及Qt自定义类型使用QHash等算法的更多相关文章

  1. Qt自定义类型使用QHash等算法(Qt已经自定义了34种类型,包括int, QString, QDate等基本数据类型)

    自定义类型 #include <QCoreApplication> #include <QSet> #include <QDebug> class testCust ...

  2. Qt之创建自定义类型

    摘要: 简述 当使用Qt创建用户界面时,特别是那些带有特殊控制和特征的界面时,开发者通常需要创建新数据类型来扩展或替换Qt现有的的值类型集合. 标准类型,比如:QSize.QColor和QString ...

  3. qt 如何注册自定义类型?

    如何声明自定义类型 如果仅仅在 QVariant 中使用,则仅需要使用 Q_DECLARE_METATYPE 宏进行声明即可. class Custom_ : public QObject { Q_O ...

  4. Q_DECLARE_METATYPE(继承QObject的类都已经自动注册),注册后的类型可以作为QVariant的自定义类型

    简介 这个宏用来注册一个类(含默认构造.默认析构.拷贝构造函数)为QMetaType类型 ,注册后的类型可以作为QVariant的自定义类型. 这个宏应该放在类或者结构体外面的下面,也可以放在一个非公 ...

  5. Qt 自定义事件详细实例(继承QEvent,然后QCoreApplication::postEvent()、sendEvent())

    创建用户事件 创建一个自定义类型的事件,首先需要有一个事件号,其值通常大于QEvent::User.为了传递事件信息,因此必须编写自定义的事件类,该事件类从QEvent继承. 编写用户事件:编写用户事 ...

  6. Qt 自定义事件(三种方法:继承QEvent,然后Send Post就都可以了,也可以覆盖customEvent函数,也可覆盖event()函数)

    Qt 自定义事件很简单,同其它类库的使用很相似,都是要继承一个类进行扩展.在 Qt 中,你需要继承的类是 QEvent. 继承QEvent类,你需要提供一个QEvent::Type类型的参数,作为自定 ...

  7. Qt 自定义事件

    Qt 自定义事件很简单,同其它类库的使用很相似,都是要继承一个类进行扩展.在 Qt 中,你需要继承的类是 QEvent. 继承QEvent类,你需要提供一个QEvent::Type类型的参数,作为自定 ...

  8. 《精通C#》自定义类型转化-扩展方法-匿名类型-指针类型(11.3-11.6)

    1.类型转化在C#中有很多,常用的是int类型转string等,这些都有微软给我们定义好的,我们需要的时候直接调用就是了,这是值类型中的转化,有时候我们还会需要类类型(包括结构struct)的转化,还 ...

  9. C#简单问题,不简单的原理:不能局部定义自定义类型(不含匿名类型)

    今天在进行代码测试时发现,尝试在一个方法中定义一个委托,注意是定义一个委托,而不是声明一个委托变量,在编写的时候没有报错,VS也能智能提示,但在编译时却报语法不完整,缺少方括号,但实际查询并没有缺少, ...

随机推荐

  1. 解决Stm32出现error: #20: identifier "GPIO_InitTypeDef" is undefined异常

    该错误是我在移植sd卡程序时出现的,错误如下: error:#20,查看错误,可以发现,这些变量都是系统定义过的,没有修改过.并且该变量也能成功跳转被找到.那么到底是什么原因呢?逛了一些帖子,尝试了好 ...

  2. 论 Python Opencv 中文路径及中文文件名图像文件读取的两种方式

    python 2中对于中文字符的处理可谓是诟病已久,虽然python 3 使用统一编码解决了中文字符串的问题,但在使用opencv中imread函数读取中文路径图像文件时仍会报错. 1) 借助nump ...

  3. BrightScript 3D test - Roku (4)

    My initial attempt to port over an old Actionscript program, here it goes in main.brs. Library " ...

  4. 20155325 2016-2017-2 《Java程序设计》第4周学习总结

    教材学习内容总结 封装就是将数据与相关行为包装在一起以实现信息就隐藏. 多态是指不同的类对象调用同一个签名的成员方法时将执行不同代码的现象.多态是面向对象程序设计的灵活性和可扩展性的基础. 以封装为基 ...

  5. nohub用法

    在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/mysql/bin/my ...

  6. WPF 窗口句柄获取和设置

    原文:WPF 窗口句柄获取和设置 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83347163 ...

  7. 成都Uber优步司机奖励政策(4月14日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. [POI2011]MET-Meteors

    题面 题解 首先我们尝试暴力,那么就对每个点二分一下即可. 我们发现单独二分复杂度太高,而且有些地方很浪费,如求前缀和等. 那么我们就想,能否将它们合并在一起二分呢? 于是就有了整体二分 整体二分即可 ...

  9. 【LG3231】[HNOI2013]消毒

    题面 洛谷 题解 代码 \(100pts\) #include<iostream> #include<cstdio> #include<cstdlib> #incl ...

  10. IAR里面STM32工程使用printf

    1. 首先打开工程的options设置 2. 设置编译器的预宏定义,添加宏定义_DLIB_FILE_DESCRIPTOR 3. 修改文件DLib_Defaults.h DLib_Defaults.h ...