STL中equal_to仿函数的使用问题

larryt 2010-12-24 01:15:42
在STL中用find_if寻找vector中的自定义的结构体时,使用bind2nd将第二个参数绑定到equal_to仿函数时,该重载哪个函数啊?

#include "stdafx.h"
#include <iostream>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;

struct ISOMSG
{
int idx;
int content;

ISOMSG()
{
}

ISOMSG(int nIdx)
{
idx = nIdx;
content = 0;
}

ISOMSG(int nIdx, const int &nContent)
{
idx = nIdx;
content = nContent;
}
};

int main(int argc, char* argv[])
{
list<ISOMSG> m_msg;
ISOMSG tmp_msg;
for (int i = 0; i < 15; i++)
{
memset(&tmp_msg, 0, sizeof(ISOMSG));
tmp_msg.idx = i;
tmp_msg.content = i+3;
m_msg.push_back(tmp_msg);
}

// 下面这条语句编译有问题的
list<ISOMSG>::const_iterator it = find_if(m_msg.begin(), m_msg.end(), bind2nd(equal_to<ISOMSG>(), 10));
if (it != m_msg.end())
{
cout<<it->content<<endl;
}

return 0;
}


请教我该在ISOMSG里重载什么函数啊,或者如何才能这样使用?
...全文
507 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
larryt 2010-12-27
  • 打赏
  • 举报
回复
试了一下,必须在类的外面实现这个bool operator==(const ISOMSG &a, int rhs);不能实现为ISOMSG的成员函数。谢谢朋友的回答!
PG 2010-12-24
  • 打赏
  • 举报
回复
bind2nd(equal_to<ISOMSG>(), 10)

您把bool operator==(const ISOMSG &a, int rhs);实现下
larryt 2010-12-24
  • 打赏
  • 举报
回复
那么这个ISOMSG里的operator==应该怎么写呢?
bool operator==(const ISOMSG &a);
or:
bool operator==(const in fld_idx);
or:
bool operator==(const ISOMSG &a, const ISOMSG &b);

我试过,这几种都无法编译通过的。
PG 2010-12-24
  • 打赏
  • 举报
回复
The objects of type Type must be equality comparable. This requires that the operator== defined on the set of objects satisfies the mathematical properties of an equivalence relation All the built-in numeric and pointer types satisfy this requirement.

65,208

社区成员

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

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