STL中为什么没有ptr_fun_ref?

anrxhzh 2002-10-27 11:37:32
#include <algorithm>
#include <functional>
#include <iostream>

bool not_equal_to(int l,int r){return l!=r;}
bool not_equal_to_ref(const int& l,const int& r){return l!=r;}

int main()
{
using namespace std;
const int r[]={1,2,3,0};
//The following code fragment finds the first integer
//in a range that is equal to zero.
std::cout << *find_if(r, r+sizeof(r)/sizeof(*r),not1(bind2nd(ptr_fun(::not_equal_to),0)));
}
//output:0

//这个示例程序是正确的。但是如果我想用not_equal_to_ref来代替not_equal_to,
//应该怎样做呢?
...全文
72 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
anrxhzh 2002-11-06
  • 打赏
  • 举报
回复
小红帽,我想这是两个不同的问题。

曾见到一些老外讨论过这个问题。大致有两个解决方法,一种是spanzhang(关闭) 的思路,另一种是boost的思路。我还试图用boost的functional & bind来解决这个问题,折腾了半天也没折腾出来。转念一想,就算我折腾出来了,一些编译器也不一定能折腾出来,就算编译器折腾出来了,看我代码的人也不一定能折腾出来,就算大家都折腾出来了,保不齐日后维护这段代码的时候又有人被折腾进去了,既然这样我还是不折腾了。
ckacka 2002-11-06
  • 打赏
  • 举报
回复
这是前段时间讨论的一篇帖子,你不是写出了现成的么,你看看:
http://expert.csdn.net/Expert/topic/1104/1104185.xml?temp=.2220728
张友邦 2002-10-27
  • 打赏
  • 举报
回复
这是我自己写的一个摸班函数,没有测试过。
张友邦 2002-10-27
  • 打赏
  • 举报
回复
template <typename A1, typename A2, typename R>
pointer_to_binary_function<A1&, A2&, R> ptr_fun_ref(R (*X)(A1&, A2&))
{
return (pointer_to_binary_function<A1&, A2&, R>(X));
}

24,860

社区成员

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

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