您的位置:首页 > 产品设计 > UI/UE

如何使用find查找deque中保存的结构体对象

2008-11-01 20:13 507 查看
开始定义的结构体如:

typedef struct student

{

int id;

char name[20];

char sex[20];

char birthday[50];

int score;

char description[100];

void* pWnd;

}STUDENT,*PSTUDENT;

定义一个deque来保存这个结构体的对象 deque<STUDENT>m_vecst; 现在我有一个结构体对象,想查找是否在这个deque中,我开始是这样做的:

STUDENT TT;

deque<STUDENT>::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<struct student,class std::allocator<struct student> >::iter
ator __cdecl std::find(class std::deque<struct student,class std::allocator<struct student> >::iterator,class std::deque<struct student,class std::allocator<struct student> >::iterator,const struct student &)' being compiled
Error executing cl.exe.

之所有这个错误,主要是我们没有定义在查找结构体STUDENT时,不知道怎么样判断结构体是相等的.因此我们必须重载结构体的==符号。.如下:

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐