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<<str<<i<<endl;
}int main()
{int a[] = { 1, 2, 3, 4};vector<int> v(a, a+sizeof(a)/sizeof(int));for_each(v.begin(), v.end(), bind2nd(ptr_fun(fun), "Element:"));
}
#include <iostream>
#include <vector>template<class T>
struct plus2{void operator()(T& x){x += 2;}
};//template <class T>
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::vector<int>iv(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<int>());
//    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::vector<int>arr(ia,ia+(sizeof (ia)/sizeof (int)));//移除所有小于100的元素//bind2nd(判断条件,标准)  判断条件 标准//bind1nd(判断条件,标准)  标准 判断条件/** std::bind1nd(std::less<int>(),100))   100 < x* std::bind2nd(std::less<int>(),100))   x < 100*/
//    arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind2nd(std::less<int>(),100)),arr.end()); //100 200
//    arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind1st(std::greater<int>(),100)),arr.end());//    arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind2nd(std::greater<int>(),100)),arr.end());
//    arr.erase(std::remove_if(arr.begin(),arr.end(),std::bind1st(std::less<int>(),100)),arr.end()); //1 2 100//删除小于等于100的元素arr.erase(std::remove_if(arr.begin(),arr.end(),std::not1(std::bind2nd(std::greater<int>(),100))),arr.end());for(auto i : arr){std::cout << i << " ";}
}

ptr_fun

  • C++11弃用 被更通用的std::function 和 std::ref替代
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>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<char>(std::cout),std::not1(std::ptr_fun(isvowel)));
}//Hll wrld!

std::function函数

  • std::function - cppreference.com
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>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 functionstd::function<void(int)>f_display = print_num;f_display(-9);//strore a lambdastd::function<void()> f_display_42 = [](){ print_num(43);};f_display_42();//store the result of a call to std::bindstd::function<void()>f_display_31337 = std::bind(print_num,31337);f_display_31337();//store a call to a member functionstd::function<void(const Foo&,int)>f_add_display = &Foo::print_add;const Foo foo(300000);f_add_display(foo,1);//store a call to a member function and objectusing std::placeholders::_1;std::function<void(int)>f_add_display2 = std::bind(&Foo::print_add,foo,_1);f_add_display2(2);//store a call to a member function and object ptrstd::function<void(int)>f_add_display3 = std::bind(&Foo::print_add,&foo,_1);f_add_display3(3);//store a call to a function objectstd::function<void(int)>f_display_obj = print_Num();f_display_obj(18);
}

参考链接

  • bind1st bind2nd的使用_simahao的专栏-CSDN博客_bind2nd
  • C++ STL std::ptr_fun() 函数说明 - 简书
  • C++中string::npos的一些用法总结_竭尽全力的专栏-CSDN博客

C++for_each| bind1st | ptr_fun | std::function的用法相关推荐

  1. C++11 std::bind std::function 高级用法

    C++11 std::bind std::function 高级用法 (c++11的新特性) 原文:https://blog.csdn.net/yangjie6898862/article/detai ...

  2. std::function和std::bind用法

    std::bind() std::bind 主要用于绑定生成目标函数,一般用于生成的回调函数,cocos的回退函数都是通过std::bind和std::function实现的.两个点要明白: 1.绑定 ...

  3. 【C++】C++11的std::function和std::bind用法详解

    在设计回调函数的时候,无可避免地会接触到可回调对象.在C++11中,提供了std::function和std::bind两个方法来对可回调对象进行统一和封装. 可调用对象 C++中有如下几种可调用对象 ...

  4. std::function用法详解

    std::function用法详解 代码在:VCCommon/functionDemo std::function 简介 类模板 std :: function 是一个通用的多态函数包装器. std ...

  5. C++11 std::function, std::bind, std::ref, std::cref

    C++11 std::function, std::bind, std::ref, std::cref 转自:http://www.jellythink.com/ std::function 看看这段 ...

  6. C++——包装器std::function与绑定器std::bind

    C++--包装器std::function与绑定器std::bind 1.可调用对象的包装器 std::function是可调用对象的包装器.它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可 ...

  7. C++中std::function和std::bind

    1.可调用对象 可调用对象有一下几种定义: 是一个函数指针,参考 C++ 函数指针和函数类型: 是一个具有operator()成员函数的类的对象: 可被转换成函数指针的类对象: 一个类成员函数指针: ...

  8. lambda 和 std::function

    1. Lambda函数的用处 lambda 表达式可以方便地构造匿名函数,如果你的代码里面存在大量的小函数,而这些函数一般只被调用一次,那么不妨将他们重构成 lambda 表达式. C++11 的 l ...

  9. C++11中的std::function

    文章转载自:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard ...

最新文章

  1. 命令模式的优点?_一篇目录复习完设计模式
  2. Java设计模式菜鸟系列(十三)建模和实现状态模式
  3. safe_mode 开启后linux下影响
  4. QT获取本地网络信息
  5. Nagios 请检查HTTP服务器关于该CGI的访问权限设置
  6. [css] 手写一个满屏品字布局的方案
  7. mysql都有哪些数据库日志_MySQL数据库之MySQL都有哪些日志?分别都代表什么
  8. 【java笔记】异常处理
  9. DNS劫持和HTTP劫持有何区别
  10. 在EF4.1的DBContext中实现事务处理(BeginTransaction)和直接执行SQL语句的示例
  11. 中文文本标注工具调研以及BRAT安装使用
  12. VS2012安装部署教程
  13. linux终端下打开pdf文件,如何从终端打开PDF文件?
  14. 2019年中总结之说走就走
  15. 舞蹈模特欣欣(六)棚拍私房 大家看看像小龙女(李若彤)吗?
  16. solid works定义样条曲线
  17. 二叉树练习:最小函数值
  18. python3笔记5--循环语句
  19. VB-Word添加页眉页脚
  20. 失传千年AE特效真经(四)

热门文章

  1. python读取图像数据流_浅谈TensorFlow中读取图像数据的三种方式
  2. html5 webview,HTML5+学习历程之webview经典案例
  3. aix linux运维,运维老司机分享的八个AIX日常运维经验及案例
  4. mysql的util_JDBC连接mysql工具类Util供大家参考
  5. hadoop namenode启动不了_集群版hadoop安装,写给大忙人看的
  6. C#通用类Helper整理
  7. linux系统管理Linux系统实验,实验三 linux系统管理
  8. 数据结构与算法 - 递归回溯(迷宫问题)
  9. 【ZOJ - 1163】The Staircases(dp)
  10. 【POJ - 2378】Tree Cutting(树形dp,树的重心变形)