c++11之std::bind和function

  • 基本测试代码
      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. }
       





posted on 2015-08-19 22:18  云东  阅读(433)  评论(0编辑  收藏  举报