zoukankan      html  css  js  c++  java
  • c++ primer读书笔记之c++11(一)

    1 新的关键词nullptr

    c++11引入新的关键词nullptr,用于表示空指针,用于替换之前c提供的NULL(最初NULL是定义在stdlib中的宏定义,通常是0)。

    2 新的别名定义机制 alias declaration

    c++提供了使用typedef定义的别名机制,但是有时候很难理解。为了更好的使用别名机制,c++11提供了新的别名定义机制,类似代码如下:

      // alias declarations
      using VInt = int; // type alias
      using PInt = int *; // pointer alias
      using RInt = int &; // reference alias
      using MyFunc = int (*)(void); // function pointer alias
      
      VInt i = 99;
      PInt pi = &i;
      RInt ri = i;
      MyFunc func = nullptr;

    3 自动类型推断关键字auto

    c++11扩展了auto的函数,编译器可按照实际的初始化参数推断需要的参数类型,并初始化之。其用法类似下面代码:

    // item初始化为val1+val2的结果
    auto item = val1 + val2;

    需要注意的一点是,auto、const以及引用类型一起使用的时候其语法可能不是简单的类型推断,编译器推断类型的机制可能与我们能想到的简单推断不太一致。也就是说如果需要定义auto引用、auto常量时需要明确指定,否则直接使用auto编译器会简单的使用对应的类型,而直接忽略const和引用的限制。相关简单示例代码如下:

      /* auto specifier */
      // a_i is int, a_pi is a pointer to int, a_ri is a reference to int
      auto a_i = 0, *a_pi = &a_i, &a_ri = a_i;
      // error like this, for a_f is float while a_d is double
      //auto a_f = 0.0f, a_d = 0.0;

    4 自动类型推断关键字decltype

    c++11提供了新的类型推断关键字decltype,主要用于自动推断表达式类型,但不需要初始化的情况(auto提供了推断类型+初始化)。

    // result is any type that expr has.
    decltype(expr) result;

    与auto自动类型推断相比,decltype的自动类型推断很直接,expr是什么类型,通常decltype返回也是一样的类型。用法如下:

      /*decltype specifier*/
      const int ci = 0, &cr = ci, *cp = &ci;
      decltype(ci) x = 0;// x has type const int
      decltype(cr) y = x;// y has type const int &
      decltype(cr) z;// error,z is a reference and must be initialized
    
      decltype(cr+0) b;// b has type int
      decltype(*cp) c;// error, c is a reference and must be initialized
      
      int i = 100;
      decltype(i) d;// d has type int
      decltype((i)) e;// error ,e is a reference and must be initialized

    需要区分两点,对于有括号的情况、以及指针取值的情况,decltype返回的是左值引用(上面代码中的后两个error)。

    5 新的for循环语法,range for

    c++本身的for循环需要三个语句,初始化、条件、增量,对于有些操作需要额外写很多代码。针对需要遍历STL容器中的每个元素的情况,可以考虑使用range for。其具体语法如下:

    for(declaration:expression)
        statement;

    比如我们把std::string的字母全部变成小写字母可以这么写:

      /* range for */
      using std::string;
      string str("UPPER test");
      for (auto &c : str)
          c = tolower(c);

    尤其是配合auto和decltype使用,更可以节省很多代码(相信大家都有使用STL迭代器的经验,代码很长,但功能就是为了遍历)。

    需要注意一点,range for在使用时不能向容器中插入或删除元素,只能修改。

    6 列表初始化机制 list initialization

    结合c++11,变量初始化有以下几种方式:

    int x = 0;
    int x = {0}; // list initialization
    int x(0);
    int x{0}; // list initialization

    针对STL模板定义也提供了新的形势,示例代码如下:

        /* vector */
        vector<vector<int>> ivec;
        
        // list initialization
        vector<string> strvec{"a", "bc", "def"};
        vector<string> v2{"123"};  
        vector<string> v3{5};
        vector<string> v4{5, "s"};  

    使用大括号的通常是列表初始化,但是某些情况也不是,比如上例中v3、v4实际调用的vector的构造函数(这是编译器做的处理,在初始化列表中的类型和模板类型不匹配时)。

     附加说明

    本文内容主要是关于 c++ primer 第五版的前4章中涉及c++11内容的整理。

    所有代码都在gcc v4.8.1的版本中编译测试过,相关源码可以从我的git下载,url如下:https://git.oschina.net/Tocy/SampleCode.git,位于c++11目录下的cpp_primer_test1.cpp文件中。

  • 相关阅读:
    git环境搭建、git详细使用教程、快速上手git
    数据一致性解决方案实践
    锁的使用
    数据库连接池优化
    多级缓存优化实践
    服务端调优与JVM调优
    Sentinel 流量防卫兵
    Spring Cloud Gateway微服务网关
    OpenFeign与负载均衡
    Nacos config原理
  • 原文地址:https://www.cnblogs.com/tocy/p/cpp-primer-std11-new-traits-1.html
Copyright © 2011-2022 走看看