美文网首页
c++11新特性

c++11新特性

作者: geofer | 来源:发表于2016-12-22 17:14 被阅读0次

    统一样式的初始化

    int arr[] {1,2,3}; //没有等号
    vector<int> vi{1,2,3};
    map<int, string> m{ {1,"a"}, {2, "b"} };
    string str{"hello"};
    int *p = new int[20]{1,2,3};
    
    struct A {
     int i,j;
     A(int m, int n):i(m), j(n) {}
    };
    A func(int m, int n){return {m,n};}
    
    A *p = new A{3,7};
    

    成员变量默认初始值

    class B{
    public:
    int m = 123;
    int n;
    }
    

    auto 关键字

    auto a = 100; // a 为 int
    auto p = new A(); // p 是 A *
    auto k = 23LL; // k为long long
    
    

    decltype求表达式类型

    int i;
    double d;
    
    decltype(i) c; // c为 int类型
    decltype(d) f; // f为 double类型
    

    智能指针 shard_ptr

    需要include<memory>
    见图片

    空指针 nullptr

    见图三

    基于范围的for循环

    无序map, 哈希表

    需要include<unordered_map>

    正则表达式

    需要引入 include<regex>

    labmda 表达式

    强制类型转换

    static_cast, const_cast, interpret_cast, dynamic_cast

    try-catch异常处理

    try{
     if(..)throw -1;
    else if () throw 1.0
    }
    catch(int i){
    }
    catch(double d){
    }
    catch(...){
    // 捕获其他异常, ... 统配
    }
    
    

    相关文章

      网友评论

          本文标题:c++11新特性

          本文链接:https://www.haomeiwen.com/subject/iilhvttx.html