如何使用find查找deque中保存的结构体对象
来源:优易学  2011-12-6 11:46:08   【优易学:中国教育考试门户网】   资料下载   IT书店
  开始定义的结构体如:
  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;

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训