栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++for

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++for

c++ for_each 用法_小键233-CSDN博客

传入参数
  • 要传入参数给global function ,需要使用 ptr_fun() 这个 function adapter 将global function 转成function object , 然后再用bind2nd() 将参数bind成一个function object。(这句话好拗口)
void fun(int i, const char* str)
{
    cout< v(a, a+sizeof(a)/sizeof(int));
    for_each(v.begin(), v.end(), bind2nd(ptr_fun(fun), "Element:"));
}
#include 
#include 

template
struct plus2{
    void operator()(T& x){
        x += 2;
    }
};

//template 
void plus3_fun(int& x){
    x += 3;
//    std::cout << x << " ";
}

void fun(int i,const char* str){
    std::cout << str << "->" << i << std::endl;
}

int main(){
//    int ia[] = {22,30,20,34,45,64,34,56,75,34};
//    std::vectoriv(ia,ia+(sizeof (ia) / sizeof (int)));
//    for (int i : iv) {
//        std::cout << i << ' ';
//    }
//    std::cout << std::endl;
//    std::cout << iv.size() << ' ';
//    std::cout << std::endl;
//
    std::for_each(iv.begin(),iv.end(),plus2());
//    std::for_each(iv.begin(),iv.end(), std::bind2nd(std::ptr_fun(fun),"Hello world"));
//    for (int i : iv) {
//        std::cout << i << ' ';
//    }

    int ia[] = {1,2,100,200};
    std::vectorarr(ia,ia+(sizeof (ia)/sizeof (int)));
    //移除所有小于100的元素
    //bind2nd(判断条件,标准)  判断条件 标准
    //bind1nd(判断条件,标准)  标准 判断条件
    
//    arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind2nd(std::less(),100)),arr.end()); //100 200
//    arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind1st(std::greater(),100)),arr.end());

//    arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind2nd(std::greater(),100)),arr.end());
//    arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind1st(std::less(),100)),arr.end()); //1 2 100

    //删除小于等于100的元素
    arr.erase(std::remove_if(arr.begin(),arr.end(),std::not1(std::bind2nd(std::greater(),100))),arr.end());

    for(auto i : arr){
        std::cout << i << " ";
    }
}
ptr_fun
  • C++11弃用 被更通用的std::function 和 std::ref替代
#include 
#include 
#include 
#include 

bool isvowel(char c){
    return std::string("aeiouAEIOU").find(c) != std::string::npos;
}

int main(){
    std::string s = "Hello world!";
    std::copy_if(s.begin(),s.end(),std::ostreambuf_iterator(std::cout),
            std::not1(std::ptr_fun(isvowel)));
}

//Hll wrld!
std::function函数
  • std::function - cppreference.com 
#include 
#include 
#include 
#include 

struct Foo{
    Foo(int num):num_(num){};
    void print_add(int i)const {
        std::cout << num_ + i << std::endl;
    }
    int num_;
};
void print_num(int i){
    std::cout << i << 'n';
}
struct print_Num{
    void operator()(int i){
        std::cout << i << 'n';
    }
};


int main(){
    //store a free function
    std::functionf_display = print_num;
    f_display(-9);

    //strore a lambda
    std::function f_display_42 = [](){ print_num(43);};
    f_display_42();

    //store the result of a call to std::bind
    std::functionf_display_31337 = std::bind(print_num,31337);
    f_display_31337();

    //store a call to a member function
    std::functionf_add_display = &Foo::print_add;
    const Foo foo(300000);
    f_add_display(foo,1);

    //store a call to a member function and object
    using std::placeholders::_1;
    std::functionf_add_display2 = std::bind(&Foo::print_add,foo,_1);
    f_add_display2(2);

    //store a call to a member function and object ptr
    std::functionf_add_display3 = std::bind(&Foo::print_add,&foo,_1);
    f_add_display3(3);

    //store a call to a function object
    std::functionf_display_obj = print_Num();
    f_display_obj(18);
}

参考链接

  • bind1st bind2nd的使用_simahao的专栏-CSDN博客_bind2nd
  • C++ STL std::ptr_fun() 函数说明 - 简书
  • C++中string::npos的一些用法总结_竭尽全力的专栏-CSDN博客
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/604917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号