https://changkun.de/modern-cpp/zh-cn/00-preface/
https://www.learncpp.com/
Install GCC compiler on Windows
It is recommended to use MinGW-w64
- download the release of w64devkit
- unzip
- add
w64devkit\binto thePATHenvironment variable
same to install CMake
C++并发
thread
- join, detach
- yield, get_id, sleep_for, sleep_until
- thread::hardware_concurrency() //硬件并发数
mutex
- once_flag, call_once
- mutex, timed_mutex, recursive_mutex, shared_mutex (用于读写模型)
- lock(), try_lock(), unlock()
- lock(locable_1, locable_2) 标准库的实现保证不会死锁
- lock_guard, unique_lock, shared_lock, scoped_lock
condition_variable
- works only with unique_lock
- check -> unlock -> wait -> lock -> check -> unlock -> wait -> lock -> ...
- check -> go on
future
- async
- packaged_task
- promise
parallel algorithm in
<algorithm>and<numeric>1
2
3sort(execution::seq, copy1.begin(), copy1.end());
sort(execution::par, copy2.begin(),copy2.end());
sort(execution::par_unseq, copy2.begin(),copy2.end());
C++内存模型
https://paul.pub/cpp-memory-model/
C++中值的类别
https://paul.pub/cpp-value-category/
Misc
polymorphism with smart opinters
1 | static_pointer_cast |
子类对象被cast成父类,成员函数调的是子类的函数
Google C++ Style
https://google.github.io/styleguide/cppguide.html
Short Tutorial
https://www.thegeekstuff.com/2016/02/c-plus-plus-11/
enum
1 | enum class CoordinateArea { FirstArea, SecondArea, ThirdArea, FourthArea}; |
lambda function
1 | [firstPart](secondPart) TypeYouReturn{ BodyOfLambda}(acctualParameters); |
[firstPart]
[] nothing
[&] reference
[=] copy
[this]
static assertion
1 | static_assert(evaluatedExpression, stringMessage); |
Move and &&
one could use rvalue as reference as well
1 | MovableClass&& operator=(MovableClass&&); |
Pointers
1 | unique_ptr<someType> suniquePtr(new someType(args)); |
Tuple
1 | auto tuple = make_tuple(“triangle”, ‘t’, 10, 15, 20); |
菜鸟教程
C
存储类
- auto:默认的存储类
- register:放寄存器,无地址
- static:
- 修饰局部变量:在函数调用之间保持局部变量的值
- 修饰全局变量:变量的作用域限制在声明它的文件内(修饰函数同理)
- extern:
- 修饰局部变量:引用全局变量
- 修饰全局变量:引用另一个文件的变量
静态数组
int a[10];- 存储在栈或者全局数据区
- 大小固定
- 生命周期取决于作用域
sizeof(a)返回占用字节数- 很多情况下,
a等价于a[0]的指针
动态数组
int *a = (int *)malloc(10 * sizeof(int));存储在堆,用
free(a);释放内存大小可变,用
realloc重新分配内存a = (int *)realloc(a, 20 * sizeof(int));
枚举
1 | enum DAY{ |
指针数组
1 | const char *names[] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali"}; |
函数指针
1 | int (*fun_ptr)(int, int) = fun; |
函数指针数组
1 | void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply}; |
函数指针可以作为函数的参数
命令行参数
argc表示传入参数的个数,没有参数时值为1argv[0]表示程序的名称
C++
类成员默认为private
构造函数,析构函数,复制构造函数
1 | classname(int x, int y): x(x), y(y) { |
友元函数,友元类
public:任何
private:自己,友元
protected:自己,友元,子类
public继承:不变
protected继承:public变protected
private继承:全变private
内联函数 inline
this:
- 指向当前对象
- 只能成员函数中使用
类的静态变量
- 在类外使用::初始化
类的静态函数
虚函数 virtual
- 告诉编译器不要静态链接
- 动态链接,后期绑定
- 允许用基类的指针来调用子类的这个函数
纯虚函数:只有函数声明没有函数定义
1 | virtual int f(int, int) = 0; |
抽象类:有纯虚函数的类,不能实例化对象
泛型
1 | template <typename T> |
explicit构造函数:禁止隐式类型转换
const成员函数:不会修改成员变量