C++11新特性

时间:2021-08-05
本文章向大家介绍C++11新特性,主要包括C++11新特性使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

C++的发展

C++11的新特性

auto

可以从初始化表达式中推断出变量的类型,属于编译器特性,不影响最终的机器码

    auto a = 10;
    auto str = "hello";
    auto p = new Person();
    p->run();

decltype

可以获取变量的类型和OC中的typeof一样

    int a = 10;
    decltype(a) b = 20;

nullptr

主要解决NULL的二义性问题

void func(int v) {
    cout << "func(int v)" << endl;
}

void func(int *v) {
    cout << "func(int *v)" << endl;
}

int main(){
    func(NULL); //无法分辨是哪个函数
    return 0;
}

快速遍历

    int array[] = {11,2,33,44,55};
    for(int i : array) {
        cout << i << endl;
    }

Lambda 表达式

Lambda表达式
有点类似于JavaScript中的闭包、iOS中的Block,本质就是函数
完整结构: [capture list] (params list) mutable exception-> return type { function body }
✓ capture list:捕获外部变量列表
✓ params list:形参列表,不能使用默认参数,不能省略参数名
✓ mutable:用来说用是否可以修改捕获的变量
✓ exception:异常设定
✓ return type:返回值类型
✓ function body:函数体
有时可以省略部分结构
✓ [capture list] (params list) -> return type {function body}
✓ [capture list] (params list) {function body}
✓ [capture list] {function body}

原文地址:https://www.cnblogs.com/mengqingxiang/p/15104222.html