QT:用QSet储存自定义结构体的问题——QSet和STL的set是有本质区别的,QSet是基于哈希算法的,要求提供自定义==和qHash函数
前几天要用QSet作为储存一个自定义的结构体(就像下面这个程序一样),结果死活不成功。。。
后来还跑到论坛上问人了,丢脸丢大了。。。
事先说明:以下这个例子是错误的
- #include <QtCore>
- struct node
- {
- int cx, cy;
- bool operator < (const node &b) const
- {
- return cx < b.cx;
- }
- };
- int main(int argc, char *argv[])
- {
- QCoreApplication app(argc, argv);
- QSet<node> ss;
- QSet<node>::iterator iter;
- node temp;
- int i, j;
- for(i=0,j=100;i<101;i++,j--)
- {
- temp.cx = i;
- temp.cy = j;
- ss.insert(temp);
- }
- for(iter=ss.begin();iter!=ss.end();++iter)
- qDebug() << iter->cx << " " << iter->cy;
- return 0;
- }
后来经过高手提醒,再经过自己看文档,才发现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)函数
废话说完了,上正确的代码:
- #include <QtCore>
- struct node
- {
- int cx, cy;
- bool operator < (const node &b) const
- {
- return cx < b.cx;
- }
- bool operator == (const node &b) const
- {
- return (cx==b.cx && cy==b.cy);
- }
- };
- uint qHash(const node key)
- {
- return key.cx + key.cy;
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication app(argc, argv);
- QSet<node> ss;
- QSet<node>::iterator iter;
- node temp;
- int i, j;
- for(i=0,j=100;i<101;i++,j--)
- {
- temp.cx = i;
- temp.cy = j;
- ss.insert(temp);
- }
- for(iter=ss.begin();iter!=ss.end();++iter)
- qDebug() << iter->cx << " " << iter->cy;
- return 0;
- }
以后写代码时,一定不能想当然了啊,切记!!!
http://blog.csdn.net/small_qch/article/details/7384966
QT:用QSet储存自定义结构体的问题——QSet和STL的set是有本质区别的,QSet是基于哈希算法的,要求提供自定义==和qHash函数的更多相关文章
- Qt--信号槽传递自定义结构体参数
自定义结构体参数的信号槽连接 (1) 对于自定义的结构体参数,信号槽无法识别参数,导致信号槽连接不起作用.所以需要注册结构体参数.在结构体中声明结束的地方加上结构体注册. struct DealDet ...
- typedef和自定义结构体类型
在自定义结构体类型时会用到typedef关键字.大家都知道typedef是取别名的意思,在C语言中跟它容易混淆的有const,#define等,其区别不在本篇文章讨论之列. /*定义单链表结点类型*/ ...
- qsettings 保存自定义结构体(QVariant与自定义结构体相互转化)
参考博文:QVariant与自定义数据类型转换的方法. 这里摘取其关键内容: 1.将自定义数据类型使用Q_DECLARE_METATYPE宏进行声明,便于编译器识别. 2.在插入对象的时候,声明QVa ...
- iOS自定义结构体
一.提要 通过以官方的CGSize为例,自定义Objective-C中的结构体,并使用. 二.CGSize 1.系统定义的CGSize结构体 struct CGSize { CGFloat width ...
- Solidity的自定义结构体深入详解
一.结构体定义 结构体,Solidity中的自定义类型.我们可以使用Solidity的关键字struct来进行自定义.结构体内可以包含字符串,整型等基本数据类型,以及数组,映射,结构体等复杂类型.数组 ...
- 用set、map等存储自定义结构体时容器内部判别各元素是否相同的注意事项
STL作为通用模板极大地方便了C++使用者的编程,因为它可以存储任意数据类型的元素 如果我们想用set与map来存储自定义结构体时,如下 struct pp { double xx; double y ...
- gin中绑定表单数据至自定义结构体
package main import "github.com/gin-gonic/gin" type StructA struct { FieldA string `form:& ...
- Qt 信号槽传递自定义结构体
Qt 在信号和槽中使用自己定义的结构体
- [UE4]自定义结构体、类、数据表
自定义数据表: #pragma once #include "CoreMinimal.h" #include "Engine/UserDefinedStruct.h&qu ...
随机推荐
- python操作redis-为元素排序
#!/usr/bin/python #!coding:utf-8 import time import redis if __name__ == "__main__": try: ...
- 美国政府关于Google公司2013年度的财务报表红头文件
请管理员移至新闻版块,谢谢! 来源:http://www.sec.gov/ 财务报表下载↓ 此文仅作参考分析. 10-K 1 goog2013123110-k.htm FORM 10-K UNIT ...
- httrack,webdup,WinHTTrack,WebZip
怎么下载摄像头游戏jabbo,并使其能离线运行?修改 1.摄像头游戏jabbo:JABBO Ultimatum by LiveMurals Interactive电脑为:windows 7 32位.试 ...
- bzoj1630 [Usaco2007 Demo]Ant Counting
Description Bessie was poking around the ant hill one day watching the ants march to and fro while g ...
- STL中,迭代器的分类
五类迭代器如下: 1.输入迭代器:只读,一次传递 为输入迭代器预定义实现只有istream_iterator和istreambuf_iterator,用于从一个输入流istream中读取.一个输 ...
- Google street、Google satellite 、百度地图Iframe切换桥接JS
1.地图切换方法 注意:父页面访问Iframe页面JS方法需根据Iframe的名字来调用如:named "mapIfame" 即 mapIfame.window.iframeFun ...
- Unity 生命周期
原文翻译: Execution Order of Event Functions 事件函数的执行顺序 Edit ...
- react redux 相关技术
React全都是围绕着组件的, 所以React基础也就是:写组件的jsx.组件的生命周期以及组件的属性和状态.jsx,只要是用过html模板的分分钟就能写了: 所谓生命周期就是组件在创建.销毁.更新阶 ...
- oracle常用查询三
查询跟索引有关的数据字典时,可以用下面这条SQL语句: SQL>select * from dictionary where instr(comments,'index')>0; 如果我们 ...
- [网络流最大流经典][uva 11082][矩阵解压]
题目大意 分析 #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring ...