forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7499f70
commit 93f5580
Showing
5 changed files
with
292 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// 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函数 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// 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(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Created by light on 20-1-11. | ||
// | ||
|
||
#include <map> | ||
#include <iostream> | ||
#include <functional> | ||
|
||
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; | ||
}}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# 像Python一样玩C/C++ | ||
|
||
在Python中我们可以使用`Jupyter Notebook`直接看到结果,例如: | ||
|
||
```c | ||
l = [1,2] | ||
l | ||
``` | ||
|
||
直接输出: | ||
|
||
``` | ||
[1,2] | ||
``` | ||
|
||
那当使用C++的时候,例如: | ||
|
||
```cpp | ||
map<string, int> mp{ | ||
{"one", 1}, | ||
{"two", 2}, | ||
{"three", 3}, | ||
{"four", 4} | ||
}; | ||
``` | ||
|
||
如果要输出,就得循环遍历,可否直接输出结果呢? | ||
|
||
so easy!!! `Jupyter Notebook`可以解决一切问题,哈哈~ | ||
|
||
## 如何在Jupyter中玩C++? | ||
|
||
在github上有一个仓库,如下所示: | ||
|
||
> https://github.com/QuantStack/xeus-cling | ||
`xeus-cling` 是一个用于C++的Jupyter内核,基于C++解释器和Jupyter协议xeus的原生实现。 | ||
|
||
目前,支持Mac与Linux,但不支持Windows。 | ||
|
||
安装也是非常简单,首先安装好Anaconda,在里面创建一个虚拟环境: | ||
|
||
``` | ||
conda create -n cling | ||
``` | ||
|
||
切换进去: | ||
|
||
``` | ||
conda activate cling | ||
``` | ||
|
||
给新环境安装`jupyter`和`notebook` | ||
|
||
``` | ||
conda install jupyter notebook | ||
``` | ||
|
||
使用`conda-forge`安装`xeus-cling` | ||
|
||
``` | ||
conda install xeus-cling -c conda-forge | ||
``` | ||
|
||
为了加速安装,请记得给Anaconda配置源! | ||
|
||
检查是否安装好了内核(kernel): | ||
|
||
``` | ||
jupyter kernelspec list | ||
``` | ||
|
||
输出: | ||
|
||
```cpp | ||
python3 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/python3 | ||
xcpp11 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp11 | ||
xcpp14 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp14 | ||
xcpp17 /home/xxx/anaconda3/envs/cling/share/jupyter/kernels/xcpp17 | ||
``` | ||
|
||
打开`Jupyter Notebook`,就可以看到看到kernel了。 | ||
|
||
启动`Jupyter Notebook`: | ||
|
||
``` | ||
jupyter-notebook | ||
``` | ||
|
||
## 如何在Jupyter中玩C? | ||
|
||
只需要安装c kernel即可! | ||
|
||
可以直接在当前环境中创建c kernel,也可以新开一个环境安装,下面是在当前环境中直接安装。 | ||
|
||
``` | ||
pip install jupyter-c-kernel | ||
install_c_kernel | ||
jupyter kernelspec list | ||
``` | ||
|
||
此时,就输出: | ||
|
||
```cpp | ||
c /home/light/anaconda3/envs/cling/share/jupyter/kernels/c | ||
python3 /home/light/anaconda3/envs/cling/share/jupyter/kernels/python3 | ||
xcpp11 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp11 | ||
xcpp14 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp14 | ||
xcpp17 /home/light/anaconda3/envs/cling/share/jupyter/kernels/xcpp17 | ||
``` | ||
|
||
启动`Jupyter Notebook`: | ||
|
||
``` | ||
jupyter-notebook | ||
``` | ||
|