- 浏览: 1449213 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (691)
- linux (207)
- shell (33)
- java (42)
- 其他 (22)
- javascript (33)
- cloud (16)
- python (33)
- c (48)
- sql (12)
- 工具 (6)
- 缓存 (16)
- ubuntu (7)
- perl (3)
- lua (2)
- 超级有用 (2)
- 服务器 (2)
- mac (22)
- nginx (34)
- php (2)
- 内核 (2)
- gdb (13)
- ICTCLAS (2)
- mac android (0)
- unix (1)
- android (1)
- vim (1)
- epoll (1)
- ios (21)
- mysql (3)
- systemtap (1)
- 算法 (2)
- 汇编 (2)
- arm (3)
- 我的数据结构 (8)
- websocket (12)
- hadoop (5)
- thrift (2)
- hbase (1)
- graphviz (1)
- redis (1)
- raspberry (2)
- qemu (31)
- opencv (4)
- socket (1)
- opengl (1)
- ibeacons (1)
- emacs (6)
- openstack (24)
- docker (1)
- webrtc (11)
- angularjs (2)
- neutron (23)
- jslinux (18)
- 网络 (13)
- tap (9)
- tensorflow (8)
- nlu (4)
- asm.js (5)
- sip (3)
- xl2tp (5)
- conda (1)
- emscripten (6)
- ffmpeg (10)
- srt (1)
- wasm (5)
- bert (3)
- kaldi (4)
- 知识图谱 (1)
最新评论
-
wahahachuang8:
我喜欢代码简洁易读,服务稳定的推送服务,前段时间研究了一下go ...
websocket的helloworld -
q114687576:
http://www.blue-zero.com/WebSoc ...
websocket的helloworld -
zhaoyanzimm:
感谢您的分享,给我提供了很大的帮助,在使用过程中发现了一个问题 ...
nginx的helloworld模块的helloworld -
haoningabc:
leebyte 写道太NB了,期待早日用上Killinux!么 ...
qemu+emacs+gdb调试内核 -
leebyte:
太NB了,期待早日用上Killinux!
qemu+emacs+gdb调试内核
参考:https://www.cnblogs.com/bencai/p/9124654.html
使用g++ -std=c++11 some.cpp 编译
c++的helloworld
function的用法
注意使用头文件<functional>
lambda
代替对象的成员函数,成员函数需要是public
结合bind的例子
placeholders的概念
关于bind的用法:
可将bind函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。
调用bind的一般形式:auto newCallable = bind(callable,arg_list);
使用g++ -std=c++11 some.cpp 编译
c++的helloworld
#include <iostream> using namespace std; int main(int argc,char **argv) { cout<<"hello world!"<<endl; auto i=10; cout<<i<<endl; return 0; }
function的用法
注意使用头文件<functional>
#include <iostream> #include <functional> using namespace std; void printA(int a) { cout << a << endl; } int main() { std::function<void(int a)> func; func = printA; func(2); //2 printf("Hello world"); puts("Hello world"); cout << "Hello world" << endl; return 0; }
lambda
#include <iostream> #include <functional> using namespace std; void printA(int a) { cout << a << endl; } int main() { std::function<void()> func_1 = [](){cout << "hello world lamda" << endl;}; func_1(); //hello world cout << "Hello world" << endl; return 0; }
代替对象的成员函数,成员函数需要是public
#include <iostream> #include <functional> using namespace std; class Foo{ public: Foo(int num) : num_(num){} void print_add(int i) const {cout << num_ + i << endl;} int num_; }; int main() { std::function<void(const Foo&,int)> f_add_display = &Foo::print_add; Foo foo(2); f_add_display(foo,1); cout << "Hello world" << endl; return 0; }
结合bind的例子
placeholders的概念
关于bind的用法:
可将bind函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。
调用bind的一般形式:auto newCallable = bind(callable,arg_list);
#include <iostream> #include <functional> using namespace std; class A { public: void fun_3(int k,int m) { cout<<k<<" "<<m<<endl; } }; void fun(int x,int y,int z) { cout<<x<<" "<<y<<" "<<z<<endl; } void fun_2(int &a,int &b) { a++; b++; cout<<a<<" "<<b<<endl; } int main(int argc, const char * argv[]) { //f1的类型为 function<void(int, int, int)> auto f1 = std::bind(fun,1,2,3); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3 f1(); //print:1 2 3 auto f2 = std::bind(fun, placeholders::_1,placeholders::_2,3); //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f2 的第一,二个参数指定 f2(1,2);//print:1 2 3 auto f3 = std::bind(fun,placeholders::_2,placeholders::_1,3); //表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数指定 //注意: f2 和 f3 的区别。 f3(1,2);//print:2 1 3 int n = 2; int m = 3; auto f4 = std::bind(fun_2, n,placeholders::_1); //表示绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。 f4(m); //print:3 4 cout<<m<<endl;//print:4 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的,如m cout<<n<<endl;//print:2 说明:bind对于预先绑定的函数参数是通过值传递的,如n A a; //f5的类型为 function<void(int, int)> auto f5 = std::bind(&A::fun_3, a,placeholders::_1,placeholders::_2); //使用auto关键字 f5(10,20);//调用a.fun_3(10,20),print:10 20 std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2); fc(10,20);//调用a.fun_3(10,20) print:10 20 return 0; }
发表评论
-
weak_ptr解决循环引用问题
2021-03-08 21:12 1083C++11引入的三种智能指 ... -
gcc链接顺序
2019-10-12 18:25 529代码在 https://github.com/killinux ... -
opengl的helloworld
2014-10-22 19:41 8651.我提供一个不需要配置环境就可运行的源码。 glut.h放在 ... -
画图板用c++实现和用js实现的websocket版本
2014-10-17 13:02 2101画图板 opencv的c++ #include <o ... -
c语言内存
2014-07-02 10:26 6681、C中内存分为五个区 栈:用来存放函数的形参和函数内的局部变 ... -
重定向stdout到文件
2014-03-05 18:37 5426把stdout重定向到文件 两种方法: 第一种方法没有恢复 ... -
通过nginx远程执行shell
2014-03-03 10:26 4998saltstack远程执行shell,远程管理等返回json已 ... -
c的urldecode
2014-02-28 18:22 1320#include <stdio.h> #in ... -
pthread的pthread_mutex_lock 的使用
2014-02-25 16:54 26070参考http://haoningabc.iteye.com/b ... -
c调用c++
2013-10-12 15:24 1133参考 http://www.cppblog.com/frank ... -
用C语言,实现接收管道输出的结果,并显示
2013-04-23 21:35 1895在shell里利用“|”管道干的事情就是io重定向,把“|”命 ... -
关于char * 与 char[]
2013-04-22 21:56 926问题引入: 在实习过程中发现了一个以前一直默认的错误,同样ch ... -
单向链表翻转
2012-12-25 23:41 988临时笔记,创建一个链表 #include <stdl ... -
trie 树 的代码
2012-12-14 23:20 1104想起搜狐老大的一句话 看代码先看h文件,擦,当初感觉他这句话很 ... -
指针函数与函数指针的区别
2012-12-14 22:44 1160一、 1、指针函数是指带指针的函数,即本质是一个函数。函数返回 ... -
指针和数组
2012-11-14 22:40 1029转载http://kan.weibo.com/con/3512 ... -
js备份
2012-10-31 23:56 1693<!DOCTYPE HTML PUBLIC " ... -
线程的helloworld
2012-10-30 21:51 1559#include<stdio.h> #inc ... -
c的书籍
2012-10-30 10:56 1095http://www.acm.uiuc.edu/webmonk ... -
深入理解计算机系统第三章笔记 gcc
2012-10-24 12:11 1494随便写个最简单程序 然后gcc -S 看汇编 在gcc -C ...
相关推荐
C++里面的function是一个非常奇妙的东西,但是你想过他是如何实现的吗?作者在深刻理解了其中奥妙之后写就的精简版本。而且可以避免STL里面的function无法和Socket编程一起使用的问题——bind会无法正确解析。
c++11引入了std::bind及std::function,实现了函数的存储和绑定,即先将可调用的对象保存起来,在需要的时候再调用。定义了SignalObject信号类和SlotObject槽类,其中信号类中的 std::function(int)> _call就是要...
主要介绍了C++ 11 std::function和std::bind使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制。之所以把这三块放在一起讲,是因为这三块之间有着非常密切的关系,通过对比学习,加深对这部分内容的理解。在开始之间...
<hello> pf = std::bind(&Test1::fun,t,2); pf(4); // return 0; } 输出的值是2,说明pf传进去的4并没有什么用,在bind的时候值已经绑定死了,但是pf()这样不给参数会报错。 如果我们想自定义参数,使用占位符,...
大家都知道C++11中增加了许多的新特性,下面在这篇文中我们就来聊一下lambda表达式,闭包,std::function以及std::bind。文中介绍的很详细,相信对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。
使用c++的function和bind实现c#中的委托和事件
主要介绍了C++ STL 内 std::{bind/tuple/function} 简单实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
较新做法,使用C++03语言规范,还有C++0x(特指std::function + std::bind),与上面不同的是采用 **基于对象的思路**,每一个任务都是一个std::function对象,std::function,std::bind真是好; ThreadPoolCpp11 ...
主要介绍了c++11 符号修饰与函数签名、函数指针、匿名函数、仿函数、std::function与std::bind,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
具体介绍见: Qt树形控件QTreeView使用1——节点的添加删除操作: ...利用C++11的function和bind功能,实现QStandardItemModel的通用遍历函数: http://blog.csdn.net/czyt1988/article/details/21093451
c++单例模式, 需要boost中的function、bind、shared_ptr支持; 很好用; 下载中含简单的测试代码; 原帖:http://blog.csdn.net/CDScan/archive/2009/11/21/4848084.aspx