2020-02-21 C++ 1

# Uniform Initialization in c++ 11 by common syntax {}

```c++11

int i; //undefined value

int* p; //undefined value

int j{}; //initialized by 0

int* q{}; //initialized by nullptr

int values[] {1,2,3};

std::vector v {4, 5, 6};

```

# What's the Lambda

+ 函数对象 A lambda expression is a mechanism for specifying a function object.

+ 匿名函数

+ 功能集 A lambda is a definition of unctionality that can be defined inside statesments and expressions.


+ definition

    + [...] {...}

    + [...] (...) mutable throwSpec -> returnType {...}

        + !! mutable and throwSpec is optional

+ [&] 类似参数的引用传递,

+ [=] 类似参数的拷贝传递, 传递lambda作用域外的所有变量,以 const 形式

+ mutable 与[=]类似,但可以修改lambda 域外的变量

# How to use it

+ **use it as inline function**

+ use it as return type of a function

# other

+ you can use decltype to use the type of Lambda

+ you can use std::function<> class template get functional type

# example

```c++

[] { std::cout<<"hello Lambda";} //minimal definition

[] { std::cout<<"hello Lambda";} (); //call it directly

auto myFunction= [] { std::cout<<"hello Lambda";};

myFunction();

```

```c++

auto fun = [] (const std::string& s){

std::cout<

};

fun("hello Lambda");

```

```c

[] { return 42;}  //Is it ok?

[] () -> double {

return 42;

}

```

```c

int x=0;

int y=42;

auto ff=[x,&y]{

cout<<"x: "<< x <

cout<<"y: "<< y <

y++;

};

x=y=77;

ff();

ff();

cout<<"final y: "<< y <

```

```c

vector v = {50, -10, 20, -30};

std::sort(v.begin(), v.end()); //the default sort { -30, -10, 20, 50 }

// sort by absolute value:

std::sort(v.begin(), v.end(), [](int a, int b) { return abs(a)

```

# Smart Pointers

## shared_ptr

+ 共享指针,多个指针可同时指向一个对象,最后一个共享指针负责delete对象。

+ 注意: default deleter is "delete", Not "delete[]" (assuming object is createdy by new.)

+ 不支持隐式转换

+ you can use function make_shared() to handle this

+ 声明之后,再赋值要用 reset(), 不能直接用 =

+ use_count 可以返回其他指向相同对象的shared_ptr指针个数

### 自定义Deleter

```c++

shared_ptr sp(new string("jane"),

[](string* p)){

cout<<"delete "<<*sp<

delete p;

}

);

```

### 处理数组

```c++

shared_ptr sp(new int[10],

[](int* p)){

delete[] p;

}

);

```

```c++

shared_ptr sp(new int[10],

default_delete()    //provided by unique_ptr

);

```

## unique_ptr

+ unique_ptr 指向的对象,具有排他性,这个对象和相关资源只能被unique_ptr管理,当指针不在指向这个对象的时候,这个对象就会被销毁,其资源也会被释放。

+ 可以有效防止因异常中断引起的内存泄漏

+ 有了unique_ptr, 甚至不用再写 delete 和 catch 语句

+ reset(), = nullptr

+ release(), bool()

+ Avoid 用一个对象初始化两个unique_ptr! compile is pass, while fail run time.

+ use std::move() 可以在两个unique_ptr之间传递对象

### How to use

#### Source & Sink

+ source: use unique_ptr in function as return type

    + automatically called std::move() by compiler

+ sink: use unique_ptr in function as parameter type

    + should use std::move() to pass parameter

#### as class Members

Avoid resource leaks caused by exceptions thrown during the initialization of an object.

#### Dealing with Arrays

```c++

unique_ptr up(new string[10]) //Note!! run time error

unique_ptr up(new string[10]) //OK

cout<<*up;  //error

cout<

```

你可能感兴趣的:(2020-02-21 C++ 1 )