如何使用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;
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;
相关文章推荐
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- Golang web 开发实战之 session 缓存:如何使用 redigo 将一个结构体数据保存到 redis?
- CMake如何查找链接库---find_package的使用方法
- stl 中list 或者vector正确使用find查找类对象
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 在linux系统中,如何使用【find】命令精确查找文件?
- linux如何使用find命令查找文件
- find_if和函数对象结合实现对自定义类型vector的查找 复杂结构体查询某个值
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- linux如何使用find命令查找文件
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 如何使用find命令在Linux中查找文件
- linux中如何使用find命令查找文件
- 如何查找和使用 Office 对象模型文档
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 关于如何将一个对象保存到本地文件之ObjectOutputStream、ObjectInputStream的使用小结。