新手请教关于C++中find的问题

jft56578 2012-09-09 11:10:15
代码如下:

#include <iostream>
#include <list>
#include <string>
#include <algorithm> //这里是用于查找的函数头文件

using namespace std;

typedef struct Node {
char name[1024];
char src[1024];
}node;

typedef list<node>like;


int main(void)

{
like link;
like::iterator j;
like::iterator k;
int i = 0;
char name[1024];
char src[1024];
node a;
for (i = 0; i < 10; i++)
{
printf("please input name:");
scanf("%s", name);
printf("please input src:");
scanf("%s", src);
strncpy(a.name, name, sizeof(name));
strncpy(a.src, src, sizeof(src));
link.push_back(a);

k = find (link.begin(), link.end(), a.name);//问题出在这里

if (k != link.end())
{
strncpy(a.src , src, sizeof(src));
}
else
{
link.push_back(a);
}
}

for (j = link.begin(); j != link.end(); j++)
{
cout << j->name ;
cout << j->src ;
}
return 0;
}
错误提示:
list.cpp
d:\vc6\vc98\include\algorithm(43) : error C2676: binary '==' : 'struct Node' does not define this operator or a conversion to a type acceptable to the predefined operator
F:\编程例子\list.cpp(36) : see reference to function template instantiation 'class std::list<struct Node,class std::allocator<struct Node> >::iterator __cdecl std::find(class std::list<struct Node,class std::allocator<struct Node> >::iterato
r,class std::list<struct Node,class std::allocator<struct Node> >::iterator,const char (&)[1024])' being compiled
执行 cl.exe 时出错.

我百度来的用法,可是为什么一直在报错。请高手指点,谢谢!
...全文
177 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
漫步者、 2012-09-09
  • 打赏
  • 举报
回复
[code=C/C++]
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
你定义一个 ==重载吧!
[code]
wangjianbo1123 2012-09-09
  • 打赏
  • 举报
回复
在一个list中找指定的item,实际就是一个比较的过程,拿你指定Item与list第一个item开始找与指定item相等的项,因为list中是你自定义数据类型,默认没有相等等操作,所以你需要重载这个运算符,让编译器知道怎么样才认为是相等
冷月清晖 2012-09-09
  • 打赏
  • 举报
回复
默认自定义结构,无法直接使用 = == + - * / 等运算符,需要重载运算符。
例如:
http://hoeven.blogbus.com/logs/37324287.html
jft56578 2012-09-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
Node 没有重载运算符。
[/Quote]
能麻烦您说详细一点么?我不懂C++。谢谢!
冷月清晖 2012-09-09
  • 打赏
  • 举报
回复
Node 没有重载运算符。
jft56578 2012-09-09
  • 打赏
  • 举报
回复
谢谢兄弟们,我在研究研究

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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