码迷,mamicode.com
首页 > 编程语言 > 详细

c++11之std::bind和function

时间:2015-08-19 23:37:03      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

  • 基本测试代码
      1. #include<iostream>
      2. #include<functional>
      3. void func(void)
      4. {
      5. std::cout << __FUNCTION__ << std::endl;
      6. }
      7. void callback(std::function<int(int,char*)> fr)
      8. {
      9. fr(1,"gdg");
      10. }
      11. int strlength(int n,constchar* str)
      12. {
      13. return n + strlen(str);
      14. }
      15. void outPut(int x,int y)
      16. {
      17. std::cout << x <<" "<< y << std::endl;
      18. }
      19. int main()
      20. {
      21. //测试bind
      22. auto fr = std::bind(strlength, std::placeholders::_1, std::placeholders::_2);
      23. //function作为函数参数
      24. callback(fr);
      25. std::bind(strlength,1,"hhha")();
      26. std::bind(strlength, std::placeholders::_1,"hha")(45);//第一个参数从外面传入,第二个参数已经设置好了
      27. std::bind(strlength,23, std::placeholders::_1)("lallaa");//bind里按照函数的参数顺序来
      28. std::bind(strlength, std::placeholders::_2, std::placeholders::_1)("hhafdsf",25);//第一个参数用传入的第二个参数,第二参数用传入的第一个参数
      29. std::bind(strlength, std::placeholders::_1, std::placeholders::_3)(12,45,"fhsafdf");//对,第二个参数没用到
      30. }
       





c++11之std::bind和function

标签:

原文地址:http://www.cnblogs.com/dongdongweiwu/p/4743656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!