Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Light-City committed Jan 21, 2020
1 parent 7499f70 commit 93f5580
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 26 deletions.
71 changes: 45 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,31 @@
#### 2.2 [C++2.0新特性](./c++2.0/)

- [Variadic Templates](./c++2.0/variadic)

- Spaces in Template Expressions

```cpp
vector<list<int> > //ok in each C++ version
vector<list<int>> // before c++ 11 error error: ‘>>’ should be ‘> >’ within a nested template argument list,c++11后可以正常通过
```
```cpp
vector<list<int> > //ok in each C++ version
vector<list<int>> // before c++ 11 error error: ‘>>’ should be ‘> >’ within a nested template argument list,c++11后可以正常通过
```

- [nullptr and nullptr_t](./c++2.0/nullptr.cpp)

- [Automatic Type Deduction with auto](./c++2.0/auto.cpp)

- [Uniform Initialization ](./c++2.0/uniform_initialization.cpp)

- [initializer_list](./c++2.0/initializer.cpp)

- [explicit for ctors taking more than one argument](./c++2.0/explicit.cpp)

- [range-based for statement](./c++2.0/auto.cpp)

```cpp
for(decl:col) {
statement
}
```
```cpp
for(decl:col) {
statement
}
```
- [=default,=delete](./c++2.0/default_delete.cpp)
Expand All @@ -75,24 +81,34 @@ for(decl:col) {
- [template_template.cpp](./c++2.0/template_template.cpp)
- [template template parameter](./c++2.0/template_template.cpp)
- [type alias](./c++2.0/type_alias.cpp)
- [noexcept](./c++2.0/noexcept.cpp)
- [override](./c++2.0/override.cpp)
- [final](./c++2.0/final.cpp)
- [decltype](./c++2.0/decltype.cpp)
- [lambda](./c++2.0/lambda.cpp)
- [Rvalue reference](./c++2.0/rvalue.cpp)
- [move aware class](./c++2.0/move.cpp)
- 容器-结构与分类
- (1) 序列式容器包括:array(C++2.0新引入),vector,deque,list,forward_list(C++2.0新引入)
- (2) 关联式容器包括:set/multiset,map/multimap
- (3) 无序容器(C++2.0新引入,更换原先hash_xxx为unordered_xxx)包括:unordered_map/unordered_multimap,unordered_set/unordered_multiset
- [Hash Function](./c++2.0/hash.cpp)
- [tuple](./c++2.0/tuple.cpp)
学习资料:https://www.bilibili.com/video/av51863195?from=search&seid=3610634846288253061
学习资料:https://www.bilibili.com/video/av51863195?from=search&seid=3610634846288253061
#### 2.3 [C++并发编程v1](./c++2.0/./concurrency_v1)
Expand Down Expand Up @@ -148,6 +164,7 @@ for(decl:col) {
- [译期能做些什么?一个完整的计算世界](./modern_C++_30/compilercompute)
- [SFINAE:不是错误的替换失败是怎么回事?](./modern_C++_30/SFINAE)
- [constexpr:一个常态的世界](./modern_C++_30/constexpr)
- [函数对象和lambda:进入函数式编程](./modern_C++_30/functionLambda)
### 4.拓展部分
Expand All @@ -165,31 +182,33 @@ for(decl:col) {
- [C++中如何将string类型转换为int类型?](./basic_content/extent/string_int.md)
### 5.自己写的工具
### 5.工具篇
- [容器快捷输出工具](./tool/output)
对吴老师的代码进行了修改,[点击此处直通代码](./tool/output/output_container.h)
对吴老师的代码进行了修改,[点击此处直通代码](./tool/output/output_container.h)
输入:
输入:
```cpp
map<int, int> mp{
{1, 1},
{2, 4},
{3, 9}};
cout << mp << endl;
```
```cpp
map<int, int> mp{
{1, 1},
{2, 4},
{3, 9}};
cout << mp << endl;
```

输出结果显示:
输出结果显示:

```cpp
{ 1 => 1, 2 => 4, 3 => 9 }
```
```cpp
{ 1 => 1, 2 => 4, 3 => 9 }
```
感谢吴老师的代码,原代码:
- 像Python一样简单输出
- [像Python一样玩C/C++](./tool/像Python一样玩CC++.md)
> https://github.com/adah1972/output_container/blob/master/output_container.h
- 观察编译过程变化
- [https://cppinsights.io](https://cppinsights.io/)
### 6.代码运行
Expand Down
34 changes: 34 additions & 0 deletions modern_C++_30/functionLambda/adder.cpp
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函数
}
64 changes: 64 additions & 0 deletions modern_C++_30/functionLambda/autoLambda.cpp
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();
}

32 changes: 32 additions & 0 deletions modern_C++_30/functionLambda/function.cpp
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;
}},
};
}
117 changes: 117 additions & 0 deletions tool/像Python一样玩CC++.md
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
```

0 comments on commit 93f5580

Please sign in to comment.