C++ 用例



用例1: std::bind, std::for_each, std::vector, std::less

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
    
class personal_info {
    std::string name_;
    std::string surname_;
    unsigned int age_;
    
public:
    personal_info(
        const std::string& n,
        const std::string& s,
        unsigned int age):name_(n),surname_(s),age_(age) {}
    
    std::string name() const {
        return name_;
    }
    
    std::string surname() const {
        return surname_;
    }
    
    unsigned int age() const {
        return age_;
    }
    
    void print const {
        std::count << name_ << " " << surname_ << " " << age_ << "\n";
    }
    
}

int main() {
    std::vector<personal_info> vec;
    vec.push_back(personal_info("Little","John",30));
    vec.push_back(personal_info("Friar","Tuck",50));
    vec.push_back(personal_info("Robin","Hood",40));
    
    std::for_each(vec.begin(), vec.end(), std::bind(&personal_info::print, std::placeholders::_1));
    
    #按照年龄排序
    std::sort(
        vec.begin(), 
        vec.end(), 
        std::bind(
            std::less<unsigned int>(),
            std::bind(&personal_info::age, std::placeholders::_1),   //main binder's function call operator's first argument (_1)
            std::bind(&personal_info::age, std::placeholders::_2)));  //main binder's function call operator's second argument (_2).  
    
    std::for_each(vec.begin(), vec.end(), std::bind(&personal_info::print, std::placeholders::_1));
    
    #按照姓氏排序
    std::sort(
      vec.begin(),
      vec.end(),
      boost::bind(
        std::less<std::string>(),
        boost::bind(&personal_info::surname,_1),
        boost::bind(&personal_info::surname,_2)));
    
    std::for_each(vec.begin(), vec.end(), std::bind(&personal_info::print, std::placeholders::_1));    
}


用例2. std::bind, std::count_if, std::find_if

//使用bind从已有函数构建新函数 (Functional Composition)
int main() {
    std::vector<int> ints;

    ints.push_back(7);
    ints.push_back(4);
    ints.push_back(12);
    ints.push_back(10);

    //联合逻辑与,大于,小于三个函数构建一个新函数,新函数应用于count_if
    int count=std::count_if(
              ints.begin(),
              ints.end(),
              boost::bind(
                std::logical_and<bool>(),
                boost::bind(std::greater<int>(),_1,5),
                boost::bind(std::less_equal<int>(),_1,10)));

    std::cout << count << '\n';
    
    //联合逻辑与,大于,小于三个函数构建一个新函数,新函数应用于find_if
    std::vector<int>::iterator int_it=std::find_if(
                              ints.begin(),
                              ints.end(),
                              boost::bind(
                                std::logical_and<bool>(),
                                boost::bind(std::greater<int>(),_1,5),
                                boost::bind(std::less_equal<int>(),_1,10)));

    if (int_it!=ints.end()) {
      std::cout << *int_it << '\n';
    }
                  
}  


用例3.std::transform

//使用bind从已有函数构建新函数 (Functional Composition2)
int main() {                  
    std::list<double> values;
    values.push_back(10.0);
    values.push_back(100.0);
    values.push_back(1000.0);

    std::transform(//transform用于实行容器元素的变换操作, 
              //arg1: input_iterator_first, arg2:input_iterator_last, arg3:output_iterator_first, arg4:operator
              //这里的功能其实也可以用for_each来实现
      values.begin(),
      values.end(),
      values.begin(),
      boost::bind(
         std::multiplies<double>(),
         0.90,
         boost::bind<double>(  //总是最里层的binder先运行然后才是外层的binder
              std::multiplies<double>(),
              _1,
              1.10)));               
                  
}  
相关文章
相关标签/搜索