容器中的元素是结构体的问题

whn 2003-08-18 07:09:53
list<string> mylist;
plistnode=find(mylist.begin(),mylist.end(),"hannes");
这样去查找没有问题;

但我的列表中放的是一个结构体比如
typedef struct{
char name[256];
char company[256];
}UserInfo;

list<UserInfo> userlist;
UserInfo ui;
strcpy(ui.name,"hannes");
strcpy(ui.company,"microsoft");
plistnode=find(userlist.begin(),userlist.end(),ui);
这样就不行了,编译出错了,为什么?
还有一些在容器里放自己定义结构都有一些这样的问题,如何处理呢?
...全文
293 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
oopig 2003-08-19
  • 打赏
  • 举报
回复
排序的例子:
#include <vector>
#include <algorithm>
#include <memory.h>
using namespace std;

struct Record
{
char name[16];
};

bool Greater(const Record &item1, const Record &item2)
{
return ::memcmp(item1.name, item2.name, 16) > 0;
}

void main( )
{
vector<Record> v1;
// ... push record into vector ...
sort(v1.begin( ), v1.end( ), Greater);
}
whn 2003-08-19
  • 打赏
  • 举报
回复
谢谢各位,先给分!

容器中的元素的元素必须满足三个条件
1、必须支持等于操作
2、必须支持小于操作
3、必须有支持一个缺省值(对于类类型,指缺省构造函数)

我想2、必须支持小于操作是为了对容器的元素进行排序吧,那么我怎么对我上面的所说的结构体元素容器排序呢?也就是我如何写UserInfo的operator<啊,请指点!另给分!
oopig 2003-08-19
  • 打赏
  • 举报
回复
不好意思,有行代码写错了:
>>return ::memcmp(name, item.name, 16);
应该改成:
return ::memcmp(name, item.name, 16) == 0;
oopig 2003-08-19
  • 打赏
  • 举报
回复
以下程序通过编译:
#include <algorithm>
#include <list>
#include <memory.h>

using namespace std;
//---------------------------------------------------------------------------
#define BOOL int
//---------------------------------------------------------------------------
struct Record
{
char name[16];
BOOL operator==(const Record &item) const
{
return ::memcmp(name, item.name, 16);
}
};

int main()
{
list<Record> _list;
//... push record into list ...
Record item;
::strcpy(item.name, "steve");
std::find(_list.begin(), _list.end(), item);
return 0;
}
//---------------------------------------------------------------------------
oopig 2003-08-19
  • 打赏
  • 举报
回复
UserInfo需要提供一个类似这样的==操作符重载函数:
#include <memory.h>
BOOL UserInfo::operator==(const UserInfo &s) const
{
return ::memcmp(name, s.name, 256) &&
::memcmp(company, s.company, 256);
}
qqchen79 2003-08-19
  • 打赏
  • 举报
回复
If you ONLY put those struct instances in vector in an unsorted manner, defining an operator== for the struct would be enough. Algorithm find should work fine.

But if you plan to put them in a sorted container, such as map or set, or you want to sort them in a vector, you will have to define an opertor< for the struct. Algorithms like binary_search and sort require a sorted container.
bm1408 2003-08-18
  • 打赏
  • 举报
回复
同意楼上的!

aflyinghorse 2003-08-18
  • 打赏
  • 举报
回复
容器中的元素的元素必须满足三个条件
1、必须支持等于操作
2、必须支持小于操作
3、必须有支持一个缺省值(对于类类型,指缺省构造函数)
zcchm 2003-08-18
  • 打赏
  • 举报
回复
如果自定义结构体含有指针,就会出问题,因为往list里面push_back元素时,进去的是元素的copy。

有两种解决方法:
1 自定义结构体的拷贝构造函数;
struct UserInfo{
char name[256];
char company[256];
UserInfo(const UserInfo &rhs){...}//拷贝构造函数
};

//typedef struct...是C的风格,C++不需如此。
2 不要把元素直接放在容器里,而是把指向元素的指针放进容器里。
list<UserInfo*>userlist;
leasun 2003-08-18
  • 打赏
  • 举报
回复
我看你需要一个operator<

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧