zoukankan      html  css  js  c++  java
  • 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. }
         





  • 相关阅读:
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
    Swift
    C++生产和使用的临时对象
    RecyclerView0基于使用
    Docker创建MySQL集装箱
  • 原文地址:https://www.cnblogs.com/dongdongweiwu/p/4743656.html
Copyright © 2011-2022 走看看