用QSet存储一个自定义结构体

用QSet作为储存一个自定义的结构体


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

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


后来经过高手提醒,再经过自己看文档,才发现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)函数


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

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

以后写代码时,一定不能想当然了啊,切记!!!
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个Qt编写的DFS路径搜索算法的示例代码: ```cpp #include <QtWidgets> class GraphWidget : public QGraphicsView { public: GraphWidget(QWidget *parent = nullptr) : QGraphicsView(parent) { QGraphicsScene *scene = new QGraphicsScene(this); setScene(scene); // Add nodes to the scene for (int i = 0; i < 5; ++i) { QGraphicsEllipseItem *node = new QGraphicsEllipseItem(-25, -25, 50, 50); node->setPos(QPointF(qrand() % 400, qrand() % 400)); scene->addItem(node); m_nodes.append(node); } // Add edges to the scene for (int i = 0; i < m_nodes.size(); ++i) { for (int j = i + 1; j < m_nodes.size(); ++j) { QGraphicsLineItem *edge = new QGraphicsLineItem(m_nodes[i]->pos(), m_nodes[j]->pos()); scene->addItem(edge); m_edges.append(edge); } } // Set up the timer for the DFS algorithm m_timer.setInterval(500); connect(&m_timer, &QTimer::timeout, this, &GraphWidget::dfs); m_timer.start(); } private: QList<QGraphicsEllipseItem *> m_nodes; QList<QGraphicsLineItem *> m_edges; QSet<QGraphicsEllipseItem *> m_visited; QTimer m_timer; void dfs() { QGraphicsEllipseItem *node = nullptr; // Find an unvisited node for (QGraphicsEllipseItem *n : m_nodes) { if (!m_visited.contains(n)) { node = n; break; } } if (!node) { // All nodes have been visited m_timer.stop(); return; } // Mark the node as visited m_visited.insert(node); node->setBrush(Qt::green); // Visit the node's neighbors for (QGraphicsLineItem *edge : m_edges) { if (edge->line().p1() == node->pos()) { QGraphicsEllipseItem *neighbor = nullptr; for (QGraphicsEllipseItem *n : m_nodes) { if (n->pos() == edge->line().p2()) { neighbor = n; break; } } if (neighbor && !m_visited.contains(neighbor)) { dfs(); } } } } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); GraphWidget widget; widget.show(); return app.exec(); } ``` 这个示例代码演示了如何使用Qt绘制一个简单的图形,并使用DFS算法搜索图形中的所有节点。在这个示例中,我们使用QGraphicsScene和QGraphicsView来绘制图形,使用QGraphicsEllipseItem和QGraphicsLineItem来表示节点和边,使用QTimer来控制DFS算法的执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值