Thread: How CList Find struct

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    26

    How CList Find struct

    Hello ,

    I try to find the structure which is in the clist [mfc]

    Code:
    Struct sentence
    { 
     int id;
     CString name;
    };
    
    CList<sentence>sentenceinfo ;
    
    sentence sRec1;
    sRec1.id = 10;
    sRec1name = "LONG"
    sentenceinfo.addTrail(sRec1);
    
    sentence sRec2;
    sRec1.id = 20;
    sRec1name = "SHORT"
    sentenceinfo.addTrail(sRec2);
    
    here I want to check whether the id and name are existing in the structure.
    
    sentence tmpRec:
    tmpRec.id = 10;
    tmpRec.name = "SHORT "
    
    For example
    Is it possible to check like this
    If(sentenceinfo.Find(tmpRec ) == NULL)
        sentenceinfo.AddTrail(tmpRec);
    else
       Already exist.
    Since I don't have system I could not check and on the way to my office. If you could suggest me on this, It will be useful.

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,714
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    26
    Quote Originally Posted by Salem View Post
    I can able to find the struct element.

    what i did was
    Code:
    sentence tmpRec;
    tmpRec.id = 10;
    
    tmpRec.name = "LONG";
    
    
     If(sentenceinfo.Find(tmpRec ) == NULL)    sentenceinfo.AddTrail(tmpRec);
    else
       Already exist.
    
    I need to include the comparison operator overloading in the structure.
    
    struct sentence
    
    {
       int id;
     CString name;
       bool operator==(const sentence Scode) const
            {
                if (nId == Scode.nId && strName == Scode.strName)
                    return TRUE;
                else
                    return NULL;
            }
    
    
    }

  4. #4
    Registered User
    Join Date
    Nov 2016
    Posts
    26
    Thanks for your reply.I can also create the CList class.I feel like for my case, it is fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct array and find max.
    By pelopidass in forum C Programming
    Replies: 2
    Last Post: 12-07-2013, 09:47 AM
  2. Can't find struct sigevent_t
    By wirmius in forum C Programming
    Replies: 4
    Last Post: 01-22-2013, 08:51 AM
  3. How find a substring in struct
    By nick048 in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 10:03 AM
  4. Using CList
    By kalle in forum C++ Programming
    Replies: 0
    Last Post: 05-12-2006, 06:34 AM
  5. How to find a string in a struct..
    By Gugge in forum C Programming
    Replies: 3
    Last Post: 03-09-2002, 05:41 PM

Tags for this Thread