find_if function object 问题

快乐田伯光 2011-11-26 05:25:27

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
struct int_c
{
int i;
char c;

friend ostream & operator<<(ostream & output, int_c ic)
{
return output << ic.i << ", " << ic.c << endl;
}
};
struct func
{
bool operator()(struct int_c a, int b)
{
return (a.i == b);
}
};
bool wwww(struct int_c a, int b)
{
return (a.i == b);
}
int main(int argc, char *argv[])
{
vector<int_c> vecIC;
int_c int_c_node;

for(char c = 'a'; c <= 'z'; c++)
{
int_c_node.i = int(c - 'a');
int_c_node.c = c;
vecIC.push_back(int_c_node);
}
copy(vecIC.begin(), vecIC.end(), ostream_iterator<int_c>(cout));
vector<int_c>::iterator iter;

//iter = find_if(vecIC.begin(), vecIC.end(), bind2nd(func(), 5));
iter = find_if(vecIC.begin(), vecIC.end(), bind2nd(ptr_fun(wwww), 5));
cout << iter->i << ", " << iter->c << endl;
return 0;
}


如上代码,使用红色的为什么编译不过,而使用蓝色的代码正常编译运行。
...全文
129 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamnobody 2011-11-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 guosha 的回复:]
find_if, 不允许改
[/Quote]

你试过了不能通过编译吗?

这只是个人是否严谨的问题。而且你的函数对象一个成员变量也没有,没什么好更改的。const不const唯一的区别就是
const func fun;//这个函数对象能否调用fun()的区别。但是这是根据你的设计需求而定的。find_if以及其他库函数应该都是按值传递的,所以在库函数里面这个是一样的。

对find_if()没有影响。
mujiok2003 2011-11-26
  • 打赏
  • 举报
回复
template <class Operation, class T>
binder2nd<Operation> bind2nd (const Operation& op, const T& x)

模板参数Operation是二元函数对象,要求提供first_argument_type, second_argument_type和result_type这三个类型
快乐田伯光 2011-11-26
  • 打赏
  • 举报
回复
find_if, 不允许改
iamnobody 2011-11-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 guosha 的回复:]
引用 7 楼 mingliang1212 的回复:

如下。:
先抢分,再解释:
C/C++ code



struct func:public std::binary_function<int_c,int,bool>
{
bool operator()(struct int_c a, int b)
{
return (a.i == b);
}
};


你抢得……
[/Quote]

可以不加const的。一个函数对象的op()本来就可以改变他值。该不加时不加。
快乐田伯光 2011-11-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mingliang1212 的回复:]

如下。:
先抢分,再解释:
C/C++ code



struct func:public std::binary_function<int_c,int,bool>
{
bool operator()(struct int_c a, int b)
{
return (a.i == b);
}
};
[/Quote]

你抢得太急了,还没有完全对,


struct func:public binary_function<int_c,int,bool>
{
bool operator()(struct int_c a, int b) const
{
return (a.i == b);
}
};
iamnobody 2011-11-26
  • 打赏
  • 举报
回复
头文件:

#include<functional>
iamnobody 2011-11-26
  • 打赏
  • 举报
回复

// TEMPLATE STRUCT binary_function
template<class _Arg1,
class _Arg2,
class _Result>
struct binary_function
{ // base class for binary functions
typedef _Arg1 first_argument_type;
typedef _Arg2 second_argument_type;
typedef _Result result_type;
};


bind2nd()函数要求知道函数对象的返回值,参数的信息,否则,他不知道该返回什么类型的函数对象。
bind2nd()的函数对象参数要求该函数内有
first_argument_type;
second_argument_type;
result_type;

三个类型成员。

一般写函数对象都会直接从std::unary_function,或者std::binary_function模板类派生。
iamnobody 2011-11-26
  • 打赏
  • 举报
回复
如下。:
先抢分,再解释:



struct func:public std::binary_function<int_c,int,bool>
{
bool operator()(struct int_c a, int b)
{
return (a.i == b);
}
};
快乐田伯光 2011-11-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 taodm 的回复:]

bool operator()const (struct int_c a, int b)
[/Quote]

跟这个应该没有任何关系
qwer_boo 2011-11-26
  • 打赏
  • 举报
回复
快乐田伯光 2011-11-26
  • 打赏
  • 举报
回复
你什么编译器? 我g++不行
[Quote=引用 3 楼 taodm 的回复:]

bool operator()const (struct int_c a, int b)
[/Quote]
taodm 2011-11-26
  • 打赏
  • 举报
回复
bool operator()const (struct int_c a, int b)
快乐田伯光 2011-11-26
  • 打赏
  • 举报
回复

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
struct int_c
{
int i;
char c;
friend ostream & operator<<(ostream & output, int_c ic)
{
return output << ic.i << ", " << ic.c << endl;
}
};
struct func
{
bool operator()(struct int_c a, int b)
{
return (a.i == b);
}
};
bool wwww(struct int_c a, int b)
{
return (a.i == b);
}
int main(int argc, char *argv[])
{
vector<int_c> vecIC;
int_c int_c_node;
vector<int_c>::iterator iter;

for(char c = 'a'; c <= 'z'; c++)
{
int_c_node.i = int(c - 'a');
int_c_node.c = c;
vecIC.push_back(int_c_node);
}
copy(vecIC.begin(), vecIC.end(), ostream_iterator<int_c>(cout));

//iter = find_if(vecIC.begin(), vecIC.end(), bind2nd(func(), 5)); 这一行为什么会编译不过?
iter = find_if(vecIC.begin(), vecIC.end(), bind2nd(ptr_fun(wwww), 5));
cout << *iter << endl;
return 0;
}


重发代码
快乐田伯光 2011-11-26
  • 打赏
  • 举报
回复

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
struct int_c
{
int i;
char c;

friend ostream & operator<<(ostream & output, int_c ic)
{
return output << ic.i << ", " << ic.c << endl;
}
};
struct func
{
bool operator()(struct int_c a, int b)
{
return (a.i == b);
}
};
bool wwww(struct int_c a, int b)
{
return (a.i == b);
}
int main(int argc, char *argv[])
{
vector<int_c> vecIC;
int_c int_c_node;

for(char c = 'a'; c <= 'z'; c++)
{
int_c_node.i = int(c - 'a');
int_c_node.c = c;
vecIC.push_back(int_c_node);
}
copy(vecIC.begin(), vecIC.end(), ostream_iterator<int_c>(cout));
vector<int_c>::iterator iter;

//iter = find_if(vecIC.begin(), vecIC.end(), bind2nd(func(), 5)); //这一行调用编译不过问题在吗
iter = find_if(vecIC.begin(), vecIC.end(), bind2nd(ptr_fun(wwww), 5));
cout << iter->i << ", " << iter->c << endl;
return 0;
}


重新发下代码

64,637

社区成员

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

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