0%

C++ Basics

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

  1. download the release of w64devkit
  2. unzip
  3. add w64devkit\bin to the PATH environment 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
    3
    sort(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
2
static_pointer_cast
dynamic_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
2
3
4
enum class CoordinateArea { FirstArea, SecondArea, ThirdArea, FourthArea};

CoordinateArea caOne = CoordinateArea::FirstArea;
CoordinateArea caSome= CoordinateArea::FourhtArea;

lambda function

1
2
3
4
[firstPart](secondPart) TypeYouReturn{ BodyOfLambda}(acctualParameters);

vectror<int> iVector;
for_each( begin(iVector), end(iVector), [](int n){if(n%2==0)cout<<n<<end;});

[firstPart]

  • [] nothing

  • [&] reference

  • [=] copy

  • [this]

static assertion

1
2
3
static_assert(evaluatedExpression, stringMessage);

static_assert(sizeof(long long int)>=16;”This is unexpected”);

Move and &&

one could use rvalue as reference as well

1
MovableClass&& operator=(MovableClass&&); 

Pointers

1
2
3
4
5
6
7
unique_ptr<someType> suniquePtr(new someType(args));
...
uniquePtr.release();

shared_ptr<someType> somePtr(new someType(args));

weak_ptr<someType> weakPtr= somePtr;

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
2
3
4
enum DAY{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY day = WED;

指针数组

1
const char *names[] = {"Zara Ali","Hina Ali","Nuha Ali","Sara Ali"};

函数指针

1
2
int (*fun_ptr)(int, int) = fun;
fun_ptr(x, y);

函数指针数组

1
void (*fun_ptr_arr[])(int, int) = {add, subtract, multiply};

函数指针可以作为函数的参数

命令行参数

  • argc 表示传入参数的个数,没有参数时值为1

  • argv[0] 表示程序的名称

C++

类成员默认为private

构造函数,析构函数,复制构造函数

1
2
3
4
5
6
7
8
9
classname(int x, int y): x(x), y(y) {
...
}
~classname() {
...
}
classname (const classname &obj) {
...
}

友元函数,友元类

public:任何

private:自己,友元

protected:自己,友元,子类

public继承:不变

protected继承:public变protected

private继承:全变private

内联函数 inline

this:

  • 指向当前对象
  • 只能成员函数中使用

类的静态变量

  • 在类外使用::初始化

类的静态函数

虚函数 virtual

  • 告诉编译器不要静态链接
  • 动态链接,后期绑定
  • 允许用基类的指针来调用子类的这个函数

纯虚函数:只有函数声明没有函数定义

1
virtual int f(int, int) = 0;

抽象类:有纯虚函数的类,不能实例化对象

泛型

1
2
template <typename T>
template <class T>

explicit构造函数:禁止隐式类型转换

const成员函数:不会修改成员变量