开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
3 Star 10 Fork 6

secondtonone1/cpplearn

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
cpplearn
/
src
/
lambda_.cpp
cpplearn
/
src
/
lambda_.cpp
lambda_.cpp 6.98 KB
一键复制 编辑 原始数据 按行查看 历史
secondtonone1 提交于 2022年06月30日 09:55 +08:00 . master
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#include <iostream>
#include <functional>
#include <vector>
#include <list>
#include "../inc/lambda_.h"
#include <thread>
using namespace std;
typedef void (*P_NameFunc)(string name);
vector<function<void(string)>> vec_Funcs;
void use_lambda_old()
{
// 1 匿名调用
[](string name)
{
cout << "this is anonymous" << endl;
cout << "hello " << name << endl;
}("zack");
// 2 通过auto赋值
auto fname = [](string name) -> string
{
cout << "this is auto " << endl;
cout << "hello " << name << endl;
return "hello world!!";
};
fname("Rolin");
// 3 函数指针
P_NameFunc fname2 = [](string name)
{
cout << "this is P_NameFunc " << endl;
cout << "hello " << name << endl;
};
fname2("Vivo");
// 4 function
function<void(string)> funcName;
funcName = [](string name)
{
cout << "this is function " << endl;
cout << "hello " << name << endl;
};
funcName("Uncle Wang");
}
void use_lambda2()
{
int age = 33;
string name = "zack";
int score = 100;
string job = "softengineer";
//值捕获
[age, name](string name_)
{
cout << "age is " << age << " name is " << name << " self-name is " << name_ << endl;
}("Novia");
//引用捕获
[&age, &name](string name_)
{
cout << "age is " << age << " name is " << name << " self-name is " << name_ << endl;
name = "Xiao Li";
age = 18;
}("Novia");
cout << "name is " << name << " age is " << age << endl;
vec_Funcs.push_back([age, name](string name_)
{ cout << "this is value catch " << endl;
cout << "age is " << age << " name is " << name << " self-name is " << name_ << endl; });
//危险,不要捕获局部变量的引用
// vec_Funcs.push_back([&age, &name](string name_)
// { cout << "this is referenc catch" << endl;
// cout << "age is " << age << " name is " << name << " self-name is " << name_ << endl; });
//全部用值捕获, name 用引用捕获
[=, &name]()
{
cout << "age is " << age << " name is " << name << " score is " << score << " job is " << job << endl;
name = "Cui Hua";
}();
//全部用引用捕获,只有name用值捕获
[&, name]()
{
cout << "age is " << age << " name is " << name << " score is " << score << " job is " << job << endl;
}();
}
void use_lambda3()
{
for (auto f : vec_Funcs)
{
f("zack");
}
}
class FuncObj
{
public:
void operator()(string str)
{
cout << "this is func obj " << str << endl;
}
};
void globalFun(string str)
{
cout << "this is global " << str << endl;
}
void globalFun2(string name, int age, int score, string job)
{
cout << "name is " << name << " age is " << age
<< " score is " << score << " job is " << job << endl;
}
void use_function()
{
list<function<void(string)>> list_Funcs;
//存储函数对象
list_Funcs.push_back(FuncObj());
//存储lambda表达式
list_Funcs.push_back([](string str)
{ cout << "this is lambda call " << str << endl; });
//存储全局函数
list_Funcs.push_back(globalFun);
for (const auto &f : list_Funcs)
{
f("hello zack");
}
}
void BindTestClass::StaticFun(const string &str, int age)
{
cout << "this is static function" << endl;
cout << "name is " << str << endl;
cout << "age is " << age << endl;
}
void BindTestClass::MemberFun(const string &job, int score)
{
cout << "this is member function" << endl;
cout << "name is " << name << endl;
cout << "age is " << num << endl;
cout << "job is " << job << endl;
cout << "score is " << score << endl;
}
void use_bind()
{
//绑定全局函数
auto newfun1 = bind(globalFun2, placeholders::_1, placeholders::_2, 98, "worker");
//相当于调用globalFun2("Lily",22, 98,"worker");
newfun1("Lily", 22);
//多传参数没有用,相当于调用globalFun2("Lucy",28, 98,"worker");
newfun1("Lucy", 28, 100, "doctor");
auto newfun2 = bind(globalFun2, "zack", placeholders::_1, 100, placeholders::_2);
//相当于调用globalFun2("zack",33,100,"engineer");
newfun2(33, "engineer");
auto newfun3 = bind(globalFun2, "zack", placeholders::_2, 100, placeholders::_1);
newfun3("coder", 33);
//绑定类的静态成员函数,加不加&都可以
// auto staticbind = bind(BindTestClass::StaticFun, placeholders::_1, 33);
auto staticbind = bind(&BindTestClass::StaticFun, placeholders::_1, 33);
staticbind("zack");
BindTestClass bindTestClass(33, "zack");
// 绑定类的成员函数,一定要传递对象给bind的第二个参数,可以是类对象,也可以是类对象的指针
// 如果要修改类成员,必须传递类对象的指针
auto memberbind = bind(BindTestClass::MemberFun, &bindTestClass, placeholders::_1, placeholders::_2);
memberbind("coder", 100);
auto memberbind2 = bind(BindTestClass::MemberFun, placeholders::_3, placeholders::_1, placeholders::_2);
memberbind2("coder", 100, &bindTestClass);
//对象必须取地址
auto numbind = bind(&BindTestClass::num, placeholders::_1);
std::cout << numbind(bindTestClass) << endl;
// function接受bind返回的函数
function<void(int, string)> funcbind = bind(globalFun2, "zack", placeholders::_1, 100, placeholders::_2);
funcbind(33, "engineer");
// function接受bind 成员函数
function<void(string, int)> funcbind2 = bind(BindTestClass::MemberFun, &bindTestClass, placeholders::_1, placeholders::_2);
funcbind2("docker", 100);
function<void(string, int, BindTestClass *)> funcbind3 = bind(BindTestClass::MemberFun, placeholders::_3, placeholders::_1, placeholders::_2);
funcbind3("driver", 100, &bindTestClass);
// function 直接接受成员函数,function的模板列表里第一个参数是类对象引用
function<void(BindTestClass &, const string &, int)> functomem = BindTestClass::MemberFun;
functomem(bindTestClass, "functomem", 88);
// function 绑定类的静态成员函数
function<void(const string &)> funbindstatic = bind(&BindTestClass::StaticFun, placeholders::_1, 33);
funbindstatic("Rolis");
}
void modify_name(std::string &name, int age)
{
name = "zack";
age = 34;
}
void thread_ref(int &a)
{
a = 1024;
}
void use_ref()
{
std::string name = "rolin";
int age = 25;
auto bind_value = bind(modify_name, name, age);
bind_value();
cout << "name is " << name << " age is "
<< age << endl;
function<void(void)> bind_ref = bind(modify_name, ref(name), ref(age));
bind_ref();
cout << "name is " << name << " age is "
<< age << endl;
//必须用ref传递,否则编译失败
// thread trf(thread_ref, age);
// trf.join();
// cout << "age is " << age << endl;
thread trf2(thread_ref, ref(age));
trf2.join();
cout << "age is " << age << endl;
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

cpp 学习笔记,项目源码
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/secondtonone1/cpplearn.git
git@gitee.com:secondtonone1/cpplearn.git
secondtonone1
cpplearn
cpplearn
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /