mfcclist

Ask for CList Find feature in MFC


I create struct as follow

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

and I create a clist

typedef CList<DoubleListDataNode *, DoubleListDataNode *> listDoubleListDataNode;

and I create public variable

listDoubleListDataNode  gDoubleListDataNode;
DoubleListDataNode      *pDoubleListDataNode;

for now, I have data in list

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

How to I use Find feature in CList, to find the nSequenceID = 1 or 5 ?

without findindex(0) and findindex(5)

I try gDoubleListDataNode(1, ...), but it not work

thanks


Solution

  • The implementation of Find() method of CList class is that it compares the elements using == operator. In your case the element is a pointer to a struct. The Find() will be merely trying to match the address of the element with the parameter you are passing to it.

    What it means that with the current design you will not be able to locate the element without storing the addresses of each element in the gDoubleListDataNode somewhere in a separate collection, even if you define the equality operator== for your struct.

    You have two options.

    1. You can define your list as typedef CList<DoubleListDataNode, DoubleListDataNode&> listDoubleListDataNode;. When you do this you will have to populate it with the elements, not the pointers. For your struct DoubleListDataNode, however, you will have to define operator= and operator==. When you define the latter you can implement it that it will be using nSequenceID

    2. Instead of using CList, you can use CMap. Define it as typedef CMap<int, int&, DoubleListDataNode *, DoubleListDataNode *> mapDoubleListDataNode; This way you will be able to use the power of MFC's hash tables to quickly locate required element in the map. One note though: The values in the map will be unique