前几天要用QSet作为储存一个自定义的结构体(就像下面这个程序一样),结果死活不成功。。。

后来还跑到论坛上问人了,丢脸丢大了。。。

事先说明:以下这个例子是错误的

  1. #include <QtCore>
  2. struct node
  3. {
  4. int cx, cy;
  5. bool operator < (const node &b) const
  6. {
  7. return cx < b.cx;
  8. }
  9. };
  10. int main(int argc, char *argv[])
  11. {
  12. QCoreApplication app(argc, argv);
  13. QSet<node> ss;
  14. QSet<node>::iterator iter;
  15. node temp;
  16. int i, j;
  17. for(i=0,j=100;i<101;i++,j--)
  18. {
  19. temp.cx = i;
  20. temp.cy = j;
  21. ss.insert(temp);
  22. }
  23. for(iter=ss.begin();iter!=ss.end();++iter)
  24. qDebug() << iter->cx << "  " << iter->cy;
  25. return 0;
  26. }

后来经过高手提醒,再经过自己看文档,才发现QSet和STL的set是有本质区别的,虽然它们的名字很像,前者是基于哈希表的,后者是红黑树的变种。。。。

QT文档中清楚地写着: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.

简而言之,就是:
QSet是基于哈希算法的,这就要求自定义的结构体Type必须提供:
1. bool operator == (const Type &b) const
2. 一个全局的uint qHash(Type key)函数

废话说完了,上正确的代码:

  1. #include <QtCore>
  2. struct node
  3. {
  4. int cx, cy;
  5. bool operator < (const node &b) const
  6. {
  7. return cx < b.cx;
  8. }
  9. bool operator == (const node &b) const
  10. {
  11. return (cx==b.cx && cy==b.cy);
  12. }
  13. };
  14. uint qHash(const node key)
  15. {
  16. return key.cx + key.cy;
  17. }
  18. int main(int argc, char *argv[])
  19. {
  20. QCoreApplication app(argc, argv);
  21. QSet<node> ss;
  22. QSet<node>::iterator iter;
  23. node temp;
  24. int i, j;
  25. for(i=0,j=100;i<101;i++,j--)
  26. {
  27. temp.cx = i;
  28. temp.cy = j;
  29. ss.insert(temp);
  30. }
  31. for(iter=ss.begin();iter!=ss.end();++iter)
  32. qDebug() << iter->cx << "  " << iter->cy;
  33. return 0;
  34. }

以后写代码时,一定不能想当然了啊,切记!!!

http://blog.csdn.net/small_qch/article/details/7384966

QT:用QSet储存自定义结构体的问题——QSet和STL的set是有本质区别的,QSet是基于哈希算法的,要求提供自定义==和qHash函数的更多相关文章

  1. Qt--信号槽传递自定义结构体参数

    自定义结构体参数的信号槽连接 (1) 对于自定义的结构体参数,信号槽无法识别参数,导致信号槽连接不起作用.所以需要注册结构体参数.在结构体中声明结束的地方加上结构体注册. struct DealDet ...

  2. typedef和自定义结构体类型

    在自定义结构体类型时会用到typedef关键字.大家都知道typedef是取别名的意思,在C语言中跟它容易混淆的有const,#define等,其区别不在本篇文章讨论之列. /*定义单链表结点类型*/ ...

  3. qsettings 保存自定义结构体(QVariant与自定义结构体相互转化)

    参考博文:QVariant与自定义数据类型转换的方法. 这里摘取其关键内容: 1.将自定义数据类型使用Q_DECLARE_METATYPE宏进行声明,便于编译器识别. 2.在插入对象的时候,声明QVa ...

  4. iOS自定义结构体

    一.提要 通过以官方的CGSize为例,自定义Objective-C中的结构体,并使用. 二.CGSize 1.系统定义的CGSize结构体 struct CGSize { CGFloat width ...

  5. Solidity的自定义结构体深入详解

    一.结构体定义 结构体,Solidity中的自定义类型.我们可以使用Solidity的关键字struct来进行自定义.结构体内可以包含字符串,整型等基本数据类型,以及数组,映射,结构体等复杂类型.数组 ...

  6. 用set、map等存储自定义结构体时容器内部判别各元素是否相同的注意事项

    STL作为通用模板极大地方便了C++使用者的编程,因为它可以存储任意数据类型的元素 如果我们想用set与map来存储自定义结构体时,如下 struct pp { double xx; double y ...

  7. gin中绑定表单数据至自定义结构体

    package main import "github.com/gin-gonic/gin" type StructA struct { FieldA string `form:& ...

  8. Qt 信号槽传递自定义结构体

    Qt 在信号和槽中使用自己定义的结构体

  9. [UE4]自定义结构体、类、数据表

    自定义数据表: #pragma once #include "CoreMinimal.h" #include "Engine/UserDefinedStruct.h&qu ...

随机推荐

  1. python操作redis-为元素排序

    #!/usr/bin/python #!coding:utf-8 import time import redis if __name__ == "__main__": try: ...

  2. 美国政府关于Google公司2013年度的财务报表红头文件

    请管理员移至新闻版块,谢谢! 来源:http://www.sec.gov/ 财务报表下载↓ 此文仅作参考分析. 10-K 1 goog2013123110-k.htm FORM 10-K   UNIT ...

  3. httrack,webdup,WinHTTrack,WebZip

    怎么下载摄像头游戏jabbo,并使其能离线运行?修改 1.摄像头游戏jabbo:JABBO Ultimatum by LiveMurals Interactive电脑为:windows 7 32位.试 ...

  4. bzoj1630 [Usaco2007 Demo]Ant Counting

    Description Bessie was poking around the ant hill one day watching the ants march to and fro while g ...

  5. STL中,迭代器的分类

    五类迭代器如下: 1.输入迭代器:只读,一次传递    为输入迭代器预定义实现只有istream_iterator和istreambuf_iterator,用于从一个输入流istream中读取.一个输 ...

  6. Google street、Google satellite 、百度地图Iframe切换桥接JS

    1.地图切换方法 注意:父页面访问Iframe页面JS方法需根据Iframe的名字来调用如:named "mapIfame" 即 mapIfame.window.iframeFun ...

  7. Unity 生命周期

    原文翻译:            Execution Order of Event Functions            事件函数的执行顺序                        Edit ...

  8. react redux 相关技术

    React全都是围绕着组件的, 所以React基础也就是:写组件的jsx.组件的生命周期以及组件的属性和状态.jsx,只要是用过html模板的分分钟就能写了: 所谓生命周期就是组件在创建.销毁.更新阶 ...

  9. oracle常用查询三

    查询跟索引有关的数据字典时,可以用下面这条SQL语句: SQL>select * from dictionary where instr(comments,'index')>0; 如果我们 ...

  10. [网络流最大流经典][uva 11082][矩阵解压]

    题目大意 分析 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring ...