2013-04-17 23 views
0

創建結構如下詢問欄列表查找功能MFC

struct DoubleListDataNode 
{ 
INT   nSequenceID; 
DOUBLE   fTemp; 
DOUBLE   fHumi; 
INT   nTimeHour; 
INT   nTimeMiin; 
INT   nTS1; 
INT   nTS2; 
}; 

和我創建一個CLIST

typedef CList<DoubleListDataNode *, DoubleListDataNode *> listDoubleListDataNode; 

和創建公共變量

listDoubleListDataNode gDoubleListDataNode; 
DoubleListDataNode  *pDoubleListDataNode; 

現在,我有數據在列表中

1 1.00 2.00 001H01M 1 2 
2 1.00 2.00 002H01M 1 2 
3 3.00 4.00 003H02M 3 4 
4 3.00 4.00 004H02M 3 4 
5 5.00 6.00 005H03M 5 6 
6 5.00 6.00 006H03M 5 5 

如何在CList中使用查找功能來查找nSequenceID = 1或5?的

沒有findindex(0)和findindex(5)

我嘗試gDoubleListDataNode(1,...),但

感謝

回答

0

查找的實施,它是不行的()方法CList類是它使用==運算符比較元素。在你的情況下,元素是一個指向結構體的指針。 Find()只會試圖將元素的地址與傳遞給它的參數相匹配。

這意味着使用當前設計,即使您爲結構定義了相同的operator==,也不能在不將每個元素的地址存儲在gDoubleListDataNode中某個位置的單獨集合中的情況下找到該元素。

您有兩種選擇。

  1. 您可以將您的列表定義爲typedef CList<DoubleListDataNode, DoubleListDataNode&> listDoubleListDataNode;。當你這樣做時,你將不得不使用元素來填充它,而不是指針。但是,對於您的struct DoubleListDataNode,您必須定義operator=operator==。當你定義後者時,你可以實現它將使用nSequenceID

  2. 而不是使用CList,你可以使用CMap。將其定義爲typedef CMap<int, int&, DoubleListDataNode *, DoubleListDataNode *> mapDoubleListDataNode;通過這種方式,您將能夠使用MFC哈希表的強大功能在映射中快速定位所需的元素。但請注意:地圖中的值將是唯一的

+0

感謝您的回覆, – user1753112