C++11 std::function 和 std::bind

1 std::bind std::bind 可以用来绑定一个函数 std::placeholders; 定义有_1、_2、_3 … 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #include <functional> using namespace std; using namespace std::placeholders; int f(int, char, double); int main() { // 翻转参数顺序 auto frev = bind(f, _3, _2, _1); int x = frev(1.2, 'w', 7); cout<<x<<endl; return 0; } int f(int a, char b , double c) { cout<<"a=="<< a <<",b==" <<b << ",c=="<<c <<endl; return 0; } 2 std::function std::function 可以用来定义一个函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 int add(int a, int b){ return a+b; } auto mod=[](int a, int b){return a%b;}; struct divide{ int operator()(int m, int n){ return m/n; } }; int main() { function<int(int,int)> func1= add; function<int(int,int)> func2= mod; function<int(int,int)> func3= divide(); cout<<func1(5, 6)<<endl; cout<<func2(5, 6)<<endl; cout<<func3(5, 6)<<endl; return 0; }……

阅读全文

「Silicon Valley 4 」观后感

看了硅谷4 前5集,男主角属于那种典型的技术男,不想在视屏聊天软件上继续发展,想基于自己创造的压缩算法来构建一个互联网络。印度人搞出来的视频聊天App由于小于12岁的用户太多,此时正好huli的CEO,Gavin有收购的想法,正好阴差阳错把产品转给了Gavin,进而导致Gavin被赶下台。 此时男主角 发现自己想要构建的Piper网络却被Gavin注册了专利,他晚上直接去找Gavin,遭遇闭门羹,峰回路转Gavin却提出要和男主角合作。后来却因为哺血仔的欺骗,对人生心灰意冷把专利无偿转给了Richar……

阅读全文

《偷影子的人》读后感

preface 《偷影子的人》作者marc·levy,这本书是在2015-09-17日京东搞活动买的,差不多16年3月份才开始看,每天晚上看一点,断断续续差不读2周看完。整本书240页左右。 brief synopsis of story 一个老是受班上同学欺负的瘦弱小男孩,因为拥有一种特殊能力而强大:他能「偷别人的影子」,因而能看见他人心事,听见人们心中不愿意说出口的秘密。他开始成为需要帮助者的心灵伙伴,为每个偷来的影子点亮生命的小小光芒。 某年灿烂的夏天,他在海边邂逅一位又聋又哑的女孩克雷尔。他该如何用自己的能力帮助她?他将如何信守与她共许的承诺?……

阅读全文

[多线程]1114-按序打印

1 题目描述 我们提供了一个类: public class Foo { public void one() { print(“one”); } public void two() { print(“two”); } public void three() { print(“three”); } } 三个不同的线程将会共用一个 Foo 实例。 线程 A 将会调用 one() 方法 线程 B 将会调用 two() 方法 线程 C 将会调用 three() 方法 请设计修改程序,以确保 two() 方法在 one() 方法之后被执行,three() 方法在 two() 方法之后被执行。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/print-in-order 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 1114. 按序打印 2 考点 着重考察多线程的并发控制。 3 Solution 下……

阅读全文

单链表的递归算法+变体

Preface 单链表的反转,按K反转等各种考点变体。 处理单链表时, 尽量采用递归实现: 1 因为递归代码简洁+优美,节省时间,并且不容易出错,是面试优选策略。 2 在面试时间比较宝贵的情况下,尽量预留时间展示自己的才能。 1 反转单链表(基础) 单链表结构定义如下: 1 2 3 4 5 6 // Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; 方法一: 递归实现(尾插法) 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution { public: //尾插法 ListNode* reverseList(ListNode* head) { if(head == NULL || head->next == NULL) { return head; } auto tail = reverseList(head->next); head->next->next = head; head->next = NULL; return tail; // became new head } }; 方法二: 循环实现(头插法) 双指针,头插法: 1 2 3 4 5 6 7 8 9 10 11 ListNode* reverseList(ListNode *head) { ListNode *pNewHead……

阅读全文

Redis 入门

1 登陆 redis-cli 远程访问: $ redis-cli -h host -p port -a password 2 常用命令 redis 127.0.0.1:6379> COMMAND KEY_NAME (1) 查找key的类型 type key 查找 keys *oob* 列出所有包含oob的key hash 集合 HMSET sqkey name “redis tutorial” description “redis basic commands for caching” likes 20 visitors 23000……

阅读全文

talk with Yuanyuan

1 来北京怎么样? 还习惯吗? 2 老家哪的? 3 和杭州相比怎么样? 4 工作还习惯吗? 5 还说家乡话?……

阅读全文

libuv入门

libuv 定时器timer 使用: g++ -o sunquan main.cpp -luv 执行 ./sunquan 可以看到每隔1秒打印一次 count的值。 uv_timer_start(&timer, timer_cb, timeout, repeat); 其中timeout是首次触发等待的时间毫秒值,之后每隔repeat毫秒触发一次,如果repeat=0表示首次触发之后不再触发。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 //main.cpp #include <cstdlib> #include <uv.h> #include <assert.h> #include <time.h> #include <iostream> using namespace std; void timer_cb(uv_timer_t *handlei) { static int count=1; cout<<"count=="<<count++<<endl; } int main() { int r; uv_timer_t timer; r = uv_timer_init(uv_default_loop(),&timer); assert(r==0); // 0 cout<<uv_is_active((uv_handle_t*) &timer)<<endl; // 0 cout<< uv_is_closing((uv_handle_t*) &timer)<<endl; cout<<"start "<<time(NULL)<<endl; r = uv_timer_start(&timer, timer_cb, 5000, 1000); r = uv_run(uv_default_loop(),UV_RUN_DEFAULT); cout<<"r="<<r<<endl; return 0; }……

阅读全文

C++11 特性:成员函数引用限定符(Reference qualifier)

1 引用限定符 学了这么多年C++,今天拜读了Scott Meyes的《more effective cpp》,第一次看到这种写法… 引用限定可以让成员函数只能被左值对象调用或者只能被右值对象调用。 下面举例说明: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 namespace left_value { class Hello { void show() & { std::cout << "just for left-value\n"; } }; inline void run() { Hello t; t.show(); // ok Hello{}.show(); // compile error: passing 'left_value::Hello' as 'this' argument discards qualifiers [-fpermissive] } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 namespace right_value { struct Test { void show() && { std::cout << " just for right value\n"; } }; inline void run() { Test t; t.show(); //compile error: passing 'right_value::Test' as 'this' argument discards qualifiers [-fpermissive] Test{}.show(); //ok } } 换句话说,引用限定所限定的就是*this,它可以让一些函数只被左值this调用或者右值……

阅读全文

C++11 explicit关键字

C++中的explicit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 class Test1 { public: Test1(int n) { num=n; }//普通构造函数 private: int num; }; class Test2 { public: explicit Test2(int n) { num=n; }//explicit(显式)构造函数 private: int num; }; int main() { Test1 t1=12; //隐式调用其构造函数,成功 Test2 t2=12; //编译错误,不能隐式调用其构造函数 Test2 t2(12); //显式调用成功 return 0; }……

阅读全文

最近文章

分类

友情链接

标签

-Wall(1) 2017(1) 2023(1) about(1) AC自动机(1) algorithm(3) atomic(1) BigData(1) busy(1) C++11(3) cache(3) chrome(1) cluster(1) CMake(1) cmd(1) Code Review(1) communication(1) core(1) CPA(1) CPC(1) CPM(2) CPP(15) CPS(1) CPT(1) CPU(1) CR(1) CS(4) Diary(3) Docker(1) DP(1) duck-type(1) echarts(1) epoll(1) etcd(1) Eureka(1) event(1) eventfd(1) Feeling(1) future(2) Gerrit(1) git(6) go(3) go-cmp(1) Golang(8) hardware(1) Hundsun(2) intersection(1) iPhone(1) Java(2) js(1) kafka(2) lambda(1) Languages(2) LeetCode(4) libuv(1) Life(12) LinkList(1) Linux(2) LogReplay(1) lua(3) MacOS(1) MySQL(1) mysqldump(1) narrow cast(1) nullptr(1) OKR(1) oneof(1) OpenTelemetry(1) owners(1) pkg(2) plan(1) plugin(2) plugins(1) poll(1) postman(1) promise(1) proto3(1) Protobuf(1) rb-tree(1) Reactive(1) ready_future(1) rebase(1) recommend(2) recursive(2) Redis(1) reflection(3) Registry Center(1) Release(1) resume(1) rpm(1) seastar(4) select(2) set(1) shared_ptr(1) SIGABRT(1) Simulate Location(1) sql(2) std::thread(1) syscall(1) tcp(1) timeout(1) TodoList(1) Tools(3) tracing(1) Travel(1) unique_ptr(1) unwound stack(1) weak_ptr(1) Web(2) Wireshark(4) Work(9) zeromq(2) zookeeper(2) zsh(1) 个人旅游(1) 企微机器人(1) 优点(1) 全麻(1) 动态规划(1) 在线广告(1) 多模匹配(1) 工作总结(1) 广告(1) 开源工具(1) 开源库(4) 总结(2) 扔鸡蛋问题(1) 文本消息指令(1) 智齿(1) 流量录制回放(1) 用户标签(1) 缺点(1) 群收款(1) 背包问题(1) 读书笔记(8) 香港签注(1) 高可用(2) 鼻炎(1)

其它