使用find查找deque中保存的結(jié)構(gòu)體對象

字號:

開始定義的結(jié)構(gòu)體如:
    typedef struct student
    {
    int id;
    char name[20];
    char sex[20];
    char birthday[50];
    int score;
    char description[100];
    void* pWnd;
    }STUDENT,*PSTUDENT;
    定義一個deque來保存這個結(jié)構(gòu)體的對象 dequem_vecst; 考試,大提示有一個結(jié)構(gòu)體對象,想查找是否在這個deque中,考試,大提示開始是這樣做的:
    STUDENT TT;
    deque::iterator iter = find(m_vecst.begin(),m_vecst.end(),TT);
    if( iter != m_vecst.end())
    {
    STUDENT DD = (STUDENT)(*iter);
    char* name = DD.name;
    }
    但是程序編譯不過去,報的錯誤為:
    **********\include\algorithm(43) : error C2678: binary '==' : no operator defined which takes a left-hand operand of type 'struct student' (or there is no acceptable conversion)
    **********.cpp(549) : see reference to function template instantiation 'class std::deque >::iter
    ator __cdecl std::find(class std::deque >::iterator,class std::deque >::iterator,const struct student &)' being compiled
    Error executing cl.exe.
    之所有這個錯誤,主要是我們沒有定義在查找結(jié)構(gòu)體STUDENT時,不知道怎么樣判斷結(jié)構(gòu)體是相等的.因此我們必須重載結(jié)構(gòu)體的==符號。.如下:
    typedef struct student
    {
    int id;
    char name[20];
    char sex[20];
    char birthday[50];
    int score;
    char description[100];
    void* pWnd;
    bool operator == (const student &A) const
    {
    if (id == A.id) return true;
    return false;
    }
    }STUDENT,*PSTUDENT;