免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3752 | 回复: 6
打印 上一主题 下一主题

[C++] 使用std::bind的时候,使用_2编译出错。_1就没有问题。为什么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-01-22 16:43 |只看该作者 |倒序浏览
10可用积分
本帖最后由 sampher 于 2014-01-23 09:08 编辑

我有下面的代码,查找一个数组里面比8大的数的个数和比8小的数的个数:

  1. #include<algorithm>
  2. #include<functional>
  3. #include<iostream>
  4. using namespace std;
  5. using namespace placeholders;
  6. int main()
  7. {
  8.     int buf[]={4,2,6,7,15};
  9.     cout<<count_if(begin(buf),end(buf),bind(less<int>(),8,_1))<<endl;//找比8大的数的数量,ok,打印1
  10.     cout<<count_if(begin(buf),end(buf),bind(less<int>(),8,_2))<<endl;//编译错误
  11.     return 0;
  12. }
复制代码
在VC下面的编译错误如下。GCC下面的编译错误更多,不贴了。我的代码应该怎么修改呢?
还请大虾指教.

  1. 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm(109): error C2451: conditional expression of type 'std::_Do_call_ret<_Forced,_Ret,_Funx,_Btuple,_Ftuple>::type' is illegal
  2. 1>          with
  3. 1>          [
  4. 1>              _Forced=false,
  5. 1>              _Ret=void,
  6. 1>              _Funx=std::less<int>,
  7. 1>              _Btuple=std::tuple<int,std::_Ph<2>>,
  8. 1>              _Ftuple=std::tuple<int &,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>
  9. 1>          ]
  10. 1>          Expressions of type void cannot be converted to other types
  11. 1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\algorithm(121) : see reference to function template instantiation 'std::iterator_traits<_Iter>::difference_type std::_Count_if<_Iter,_Pr>(_InIt,_InIt,_Pr)' being compiled
  12. 1>          with
  13. 1>          [
  14. 1>              _Iter=int *,
  15. 1>              _Pr=std::_Bind<false,void,std::less<int>,int,std::_Ph<2> &,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
  16. 1>              _InIt=int *
  17. 1>          ]
  18. 1>          d:\documents\visual studio 2012\projects\consoleapplication1\consoleapplication2\consoleapplication2.cpp(19) : see reference to function template instantiation 'std::iterator_traits<_Iter>::difference_type std::count_if<_Ty*,std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>>(_InIt,_InIt,_Pr)' being compiled
  19. 1>          with
  20. 1>          [
  21. 1>              _Iter=int *,
  22. 1>              _Ty=int,
  23. 1>              _Forced=false,
  24. 1>              _Ret=void,
  25. 1>              _Fun=std::less<int>,
  26. 1>              _V0_t=int,
  27. 1>              _V1_t=std::_Ph<2> &,
  28. 1>              _V2_t=std::_Nil,
  29. 1>              _V3_t=std::_Nil,
  30. 1>              _V4_t=std::_Nil,
  31. 1>              _V5_t=std::_Nil,
  32. 1>              <unnamed-symbol>=std::_Nil,
  33. 1>              _InIt=int *,
  34. 1>              _Pr=std::_Bind<false,void,std::less<int>,int,std::_Ph<2> &,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>
  35. 1>          ]
复制代码

论坛徽章:
17
处女座
日期:2013-08-27 09:59:352015亚冠之柏太阳神
日期:2015-07-30 10:16:402015亚冠之萨济拖拉机
日期:2015-07-29 18:58:182015年亚洲杯之巴勒斯坦
日期:2015-03-06 17:38:17摩羯座
日期:2014-12-11 21:31:34戌狗
日期:2014-07-20 20:57:32子鼠
日期:2014-05-15 16:25:21亥猪
日期:2014-02-11 17:32:05丑牛
日期:2014-01-20 15:45:51丑牛
日期:2013-10-22 11:12:56双子座
日期:2013-10-18 16:28:17白羊座
日期:2013-10-18 10:50:45
2 [报告]
发表于 2014-01-22 16:43 |只看该作者
  1. //第二个你写的肯定有问题,_2虽然表示了绑定less后调用的第二个参数的占位。而count_if的pred是这样调用的pred(*first),也就是说bind后对象只有一个参数而不是两个参数,所以应该依然应该使用_1
  2. cout<<count_if(begin(buf),end(buf),bind(less<int>(),_1,8))<<endl
  3. //像上面这样写就对了。
复制代码

论坛徽章:
769
金牛座
日期:2014-02-26 17:49:58水瓶座
日期:2014-02-26 18:10:15白羊座
日期:2014-04-15 19:29:52寅虎
日期:2014-04-17 19:43:21酉鸡
日期:2014-04-19 21:24:10子鼠
日期:2014-04-22 13:55:24卯兔
日期:2014-04-22 14:20:58亥猪
日期:2014-04-22 16:13:09狮子座
日期:2014-05-05 22:31:17摩羯座
日期:2014-05-06 10:32:53处女座
日期:2014-05-12 09:23:11子鼠
日期:2014-05-21 18:21:27
3 [报告]
发表于 2014-01-22 17:02 |只看该作者
为什么用gcc编译呢,好象是C++代码啊。

论坛徽章:
44
15-16赛季CBA联赛之浙江
日期:2021-10-11 02:03:59程序设计版块每日发帖之星
日期:2016-07-02 06:20:0015-16赛季CBA联赛之新疆
日期:2016-04-25 10:55:452016科比退役纪念章
日期:2016-04-23 00:51:2315-16赛季CBA联赛之山东
日期:2016-04-17 12:00:2815-16赛季CBA联赛之福建
日期:2016-04-12 15:21:2915-16赛季CBA联赛之辽宁
日期:2016-03-24 21:38:2715-16赛季CBA联赛之福建
日期:2016-03-18 12:13:4015-16赛季CBA联赛之佛山
日期:2016-02-05 00:55:2015-16赛季CBA联赛之佛山
日期:2016-02-04 21:11:3615-16赛季CBA联赛之天津
日期:2016-11-02 00:33:1215-16赛季CBA联赛之浙江
日期:2017-01-13 01:31:49
4 [报告]
发表于 2014-01-22 20:20 |只看该作者
本帖最后由 windoze 于 2014-01-22 20:21 编辑

_2代表第二个参数的占位符,但bind(less(), 8, ...)相当于less_than_8(...),这是个一元函数,哪来的第二个参数?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2014-01-22 21:34 |只看该作者
如楼上所说,一元函数绑定第二个参数肯定出错。
而且应该尽量用 lambda 而不是 bind+placeholder

count_if(begin(buf),end(buf),[](int x){return x > 8;});

count_if(begin(buf),end(buf),[](int x){return 8 > x;});

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2014-01-23 08:56 |只看该作者
不得不承认,C++的语法很垃圾。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP