zoukankan      html  css  js  c++  java
  • std::bind(callable, arg_list)

    std::bind(callable, arg_list)

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <functional>   //std::bind(callable, arg_list)
    
    using namespace std;
    using namespace std::placeholders;
    
    bool check_size(const string &s, string::size_type sz){
        return s.size() >= sz;
    }
    bool isShorter(char a, char b){
        return a < b;
    }
    ostream &print(ostream &os, char c, char s){
        return os << s << c;
    }
    int main(int argc, char* argv[]){
        string s = "hello";
        check_size(s, 6);
        //参数绑定
        auto check = bind(check_size, _1, 6);
        check(s);
        auto check2 = bind(check_size, _2, _1);
        check2(6,s);
        //
        sort(s.begin(), s.end(), isShorter);
        cout << s << endl;
        sort(s.begin(), s.end(), bind(isShorter,_2,_1));
        cout << s << endl;
    
        ostream &os = cout;
        for_each(s.begin(), s.end(),bind(print, ref(os), _1, ' '));//需要使用标准库的ref()进行绑定引用参数
        cout << endl;
        for_each(s.begin(),s.end(),[&os,s](char c) mutable {s=" "; os << s << c;});
        cout << endl;
        auto print2 = [&os,s](char c) mutable {s=" "; os << s << c;};
        for_each(s.begin(),s.end(),bind(print2 , _1));
        return 0;
    }
    
  • 相关阅读:
    可序列化serializable的作用是什么
    HelloWorld编译正常运行报noclassdeffounderror
    dtd对xml没有起到约束作用
    Ajax发送XML请求案例
    Ajax发送GET和POST请求案例
    Ajax发送简单请求案例
    初识Ajax
    数据库设计
    数据库和实例的区别
    Flask
  • 原文地址:https://www.cnblogs.com/chmod/p/15489929.html
Copyright © 2011-2022 走看看