support bazel complie this project and format code.
This commit is contained in:
21
learn_class/modern_cpp_30/functionLambda/BUILD
Normal file
21
learn_class/modern_cpp_30/functionLambda/BUILD
Normal file
@@ -0,0 +1,21 @@
|
||||
# please run `bazel run //learn_class/modern_cpp_30/functionLambda:adder`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/functionLambda:function`
|
||||
# please run `bazel run //learn_class/modern_cpp_30/functionLambda:autoLambda`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "adder",
|
||||
srcs = ["adder.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "function",
|
||||
srcs = ["function.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "autoLambda",
|
||||
srcs = ["autoLambda.cpp"],
|
||||
copts = ["-std=c++17"],
|
||||
)
|
32
learn_class/modern_cpp_30/functionLambda/adder.cpp
Normal file
32
learn_class/modern_cpp_30/functionLambda/adder.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by light on 20-1-11.
|
||||
//
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct adder {
|
||||
adder(int n) : n_(n) {}
|
||||
|
||||
int operator()(int x) const { return x + n_; }
|
||||
|
||||
private:
|
||||
int n_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
auto add_2 = adder(2);
|
||||
|
||||
// x+2
|
||||
cout << add_2(3) << endl;
|
||||
|
||||
auto t = bind1st(plus<int>(), 2);
|
||||
cout << t(1) << endl;
|
||||
// 上述的C++98
|
||||
binder2nd<plus<int>> a2(plus<int>(), 2);
|
||||
cout << a2(3) << endl;
|
||||
|
||||
cout << [](int x) { return x * x; }(3) << endl;
|
||||
return 0;
|
||||
// lambda表达式默认就是constexpr函数
|
||||
}
|
58
learn_class/modern_cpp_30/functionLambda/autoLambda.cpp
Normal file
58
learn_class/modern_cpp_30/functionLambda/autoLambda.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by light on 20-1-11.
|
||||
//
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int get_count() {
|
||||
static int count = 0;
|
||||
return ++count;
|
||||
}
|
||||
|
||||
class task {
|
||||
public:
|
||||
task(int data) : data_(data) {}
|
||||
|
||||
/**
|
||||
* this 标明按引用捕获外围对象(针对 lambda
|
||||
* 表达式定义出现在一个非静态类成员内的情况); 注意默认捕获符 = 和 &
|
||||
* 号可以自动捕获 this(并且在 C++20 之前,在 = 后写 this 会导致出错)
|
||||
* 本例子两次都按照第二次输出(this_thread::sleep_for(100ms);
|
||||
* this 标明按引用捕获外围对象
|
||||
*
|
||||
*
|
||||
* *this 标明按值捕获外围对象(针对 lambda
|
||||
* 表达式定义出现在一个非静态类成员内的情况;C++17 新增语法) 本例子正常输出
|
||||
*/
|
||||
auto lazy_launch() {
|
||||
return [*this, count = get_count()]() mutable {
|
||||
ostringstream oss;
|
||||
oss << "Done work " << data_ << " (No. " << count << ") in thread "
|
||||
<< this_thread::get_id() << '\n';
|
||||
msg_ = oss.str();
|
||||
calculate();
|
||||
};
|
||||
}
|
||||
|
||||
void calculate() {
|
||||
this_thread::sleep_for(100ms);
|
||||
cout << msg_;
|
||||
}
|
||||
|
||||
private:
|
||||
int data_;
|
||||
string msg_;
|
||||
};
|
||||
|
||||
int main() {
|
||||
auto t = task{37};
|
||||
thread t1{t.lazy_launch()};
|
||||
thread t2{t.lazy_launch()};
|
||||
t1.join();
|
||||
t2.join();
|
||||
}
|
19
learn_class/modern_cpp_30/functionLambda/function.cpp
Normal file
19
learn_class/modern_cpp_30/functionLambda/function.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by light on 20-1-11.
|
||||
//
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
map<string, function<int(int, int)>> op_dict{
|
||||
{"+", [](int x, int y) { return x + y; }},
|
||||
{"-", [](int x, int y) { return x - y; }},
|
||||
{"*", [](int x, int y) { return x * y; }},
|
||||
{"/", [](int x, int y) { return x / y; }},
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user