• C++11特性-易用性改进


    1.字符串的原始字面量

            表达式:R"xxx(原始字符串)xxx"或者R"(原始字符串)",xxx要写的话,必须一样

    1. //两种申明形式
    2. string a = "aaa";//没有转义的时候就是原始字面量
    3. string b = R"aaa(bbb)aaa";//申明为R"xxx()xxx"
    4. string c = R"(ccc)";//申明为R"()"
    5. string d = R"aaaa(ddd)aaaa";//申明为R"xxx()xxx"
    6. cout << a<

            意义:避免字符串中有转义字符时出现转义的问题,解决字符串过长(字符串写在多行中)而不想使用连接字符(\)的问题

    1. //避免转义
    2. string str = "hello\word\ttt";
    3. string strR = R"(hello\word\ttt)";
    4. cout << str << endl<< strR << endl;
    5. string str2 = "hello\\word\\ttt";
    6. cout << str2 << endl;
    1. //解决字符串过长(字符串写在多行中)而不想使用连接字符(\)的问题
    2. string str = "dsfhk\
    3. dsfklhj\
    4. dsklfhjsd\
    5. \
    6. sdfkgjyhk\
    7. dsfg\
    8. dfsgf\
    9. dsrfgdrf";
    10. string strR = R"(dsfhk
    11. dsfklhj
    12. dsklfhjsd
    13. sdfkgjyhk
    14. dsfg
    15. dfsgf
    16. dsrfgdrf)";
    17. cout << str << endl << strR << endl;

    2.用户指定的字面量(自定义后缀)

            operator"" xxx

    3.指针空值类型-nullptr

            在C程序中,NULL表示(void*)0;在C++程序中,NULL表示0

    1. void func(int a) {
    2. cout << "func(int a)" << endl;
    3. }
    4. void func(char* a) {
    5. cout << "func(char* a)" << endl;
    6. }
    7. //调用
    8. func(10);
    9. func(NULL);
    10. func(nullptr);

              C++中void*不能隐式转换成其他类型的指针

    1. void* a = NULL;//NULL0或者void *
    2. int* b = NULL;//NULL0或者void *
    3. double* c = NULL;//NULL0或者void *
    4. char* d = NULL;//NULL0或者void *

             nullptr不能匹配类型;可以隐式的匹配指针类型,不能匹配类型。

    1. void* ptr = nullptr;//nullptr隐式转成void*
    2. int* test2 = nullptr;//nullptr隐式转成int*
    3. double* test3 = nullptr;//nullptr隐式转成double*
    4. char* test4 = nullptr;//nullptr隐式转成char*

    4.常量-constexpr

     c11之前的const有两个意思,修饰常量与变量只读

            constexpr修饰常量表达式

            常量变达式:多个常量(值不变)组成,且在编译过程中得到计算结果的表达式,达到提高程序执行效率的结果

            定义常量const与constexpr是等价的,们都在编译过程得到结果

    1. //定义常量时,const与constexpr是等价的
    2. const int c = 33;//常量达表式
    3. const int d = c + 1;//常量达表式
    4. constexpr int h = 45;//常量达表式
    5. constexpr int k = h + 3453;//常量达表式

            对于C++内置数据类型可以使用constexpr修饰,使用class与struct自定义的数据类型不能使用constexpr修饰

            常量表达式函数:constexpr修饰函数返回值的函数(普通函数、类成员\构造函数、模板函数),发生在编译阶段

            常量表达式函数要求:

                    1.函数必须有返回值且返回值必须是常量表达式

                    2.函数使用前必须有对应的定义语句

                    3.整个函数体,不能出现常量表达式之外的语句(return除外)不能出现如using、typedef、static_assert断言、循环等

                    4.使用前必须先定义

                    5.几乎只有一个return语句(极少数除外)

    1. //常量表达式函数
    2. constexpr int funConstexpr3() {
    3. constexpr int a = 9;//a为常量
    4. return a;
    5. }
    6. //constexpr修饰模板函数,会根据传入参数类型确定是否为常量表达式函数
    7. template <typename TT>
    8. constexpr TT display(TT t) {
    9. return t;
    10. }
    11. //constexpr修饰构造函数,函数体必须为空.初始化必须为成员初始化列表
    12. class MyClass3
    13. {
    14. public:
    15. constexpr MyClass3():h(67) {}
    16. int h;
    17. };

            在C++中建议把const与constexpr区分(即表示“只读”使用const,"常量"使用constexpr)

            常量表达式的为类的构造函数时,函数体必须为空;必须使用常量表达式来给初始化列表赋值

    5.静态断言static_assert

            C语言的assert在运行阶段,对断言进行检查。条件为真,执行程序;条件为假,执行about()

            C++的static_assert在编辑阶段对断言进行检查。

    6.noexcept修饰符

            代表函数不能抛出异常,抛出异常就报错(使用noexcept答题throw())

    1. //函数不能抛出异常,抛出异常则报错
    2. void test_noexcept1() throw() {
    3. //throw 2;
    4. }
    5. //函数不能抛出异常,抛出异常则报错
    6. //C++11用noexcept代替throw()
    7. void test_noexcept() noexcept {
    8. //throw 1;
    9. }

    7.强类型枚举

            在enum后面加struct或者class修饰,使用时必须加上作用域;并且可以指定成员变量的类型

    1. //一般枚举
    2. enum MyEnum{OK,Error};
    3. //test_enum::OK” : 重定义;以前的定义是“枚举数”
    4. //enum MyEnum1{OK,Error};
    5. //强类型枚举
    6. //在enum后面加struct或者class修饰
    7. enum class MyEnum3 { OK, Error };
    8. enum struct MyEnum4{OK,Error};
    9. //使用时必须指定哪个作用域
    10. //MyEnum3 flag = OK; //error
    11. MyEnum3 flag = MyEnum3::OK; //使用时必须加上作用域
    12. //可以指定成员变量类型
    13. enum class MyEnum3 :char{ OK, Error };
    14. enum struct MyEnum4 :int{ OK, Error };

  • 相关阅读:
    Springboot毕设项目物品捎带系统41pudjava+VUE+Mybatis+Maven+Mysql+sprnig)
    第四章 深度学习中的损失函数(工具)
    Redis之持久化RDB&AOF
    英国 AI 安全峰会前瞻:为什么是现在,为什么在英国
    springboot+vue+elementUI304springboot305springboot农机电招租赁预约平台#毕业设计
    OpenCV中的HoughLines函数和HoughLinesP函数到底有什么区别?
    带你走进Cflow (一)
    【mysql】MySQL 主从同步延迟排查
    python LeetCode 刷题记录 104
    Modbus转Profinet网关在大型自动化仓储项目应用案例
  • 原文地址:https://blog.csdn.net/weixin_44840658/article/details/128195630