1. auto (page107)
auto 推断会忽略const
 
const int ci = i, & cr = ci; auto b = ci; // b is an int (top-level const in ci is dropped) auto c = cr; // c is an int (cr is an alias for ci whose const is top-level) auto d = & i; // d is an int*(& of an int object is int*) auto e = & ci; // e is const int*(& of a const object is low-level const)
 
如果需要推断const,需要明确加上const
const auto f = ci; // deduced type of ci is int; f has type const int
 
We can also specify that we want a reference to the auto-deduced type. Normal
initialization rules still apply:
 
; ; // ok: we can bind a const reference to a literal
 
When we ask for a reference to an auto-deduced type, top-level consts in the
initializer are not ignored(当我们需要引用来推断类型,top-level const在初始化时没有被忽略). As usual, consts are not top-level when we bind a reference to an initializer.
(ps:We use the term top-level const to indicate that the pointer itself is a const. When a pointer can point to a const object, we refer to that const as a low-level const.)
 
When we define several variables in the same statement, it is important to
remember that a reference or pointer is part of a particular declarator and not part of
the base type for the declaration. As usual, the initializers must provide consistent
auto-deduced types:
 
auto k = ci, &l = i; // k is int; l is int& auto &m = ci, *p = &ci; // m is a const int&;p is a pointer to const int // error: type deduced from i is int; type deduced from &ci is const int auto &n = i, *p2 = &ci;
 
********************************************************************************************

需要注意的是,auto不能用来声明函数的返回值。但如果函数有一个尾随的返回类型时,auto是可以出现在函数声明中返回值位置。这种情况下,auto并不是告诉编译器去推断返回类型,而是指引编译器去函数的末端寻找返回值类型。在下面这个例子中,函数的返回值类型就是operator+操作符作用在T1、T2类型变量上的返回值类型。

1
2
3
4
5
6
template <typename T1, typename T2>
auto compose(T1 t1, T2 t2) -> decltype(t1 + t2)
{
   return t1+t2;
}
auto v = compose(2, 3.14); // v's type is double
 
********************************************************************************************
2.decltype(page109)

返回值 decltype(表达式),表达式是不会运行的

[返回值的类型是表达式参数的类型]

 
decltype(f()) sum = x; // sum has whatever type f returns
 
 
decltype不会忽略 top-level const
, ; // x has type const int decltype(cj) y = x; // y has type const int& and is bound to x decltype(cj) z; // error: z is a reference and must be initialized
 
Some expressions willcause decltype to yield a reference type. Generally speaking, decltype returns a reference type for expressions that yield objects that can stand on the left-hand side of the assignment:(有时候表达式会导致decltype返回引用类型。一般而言,decltype返回表达式左值的引用类型)
 
, ) b; // ok: addition yields an int; b is an (uninitialized) int decltype(*p) c; // error: c is int& and must be initialized
 
r是引用类型,decltype(r)也是引用类型,但是(r+0)这个表达式返回的左值类型不是引用类型。岁b是 int类型
 
On the other hand, the dereference operator is an example of an expression for
which decltype returns a reference. As we’ve seen, when we dereference a pointer,
we get the object to which the pointer points. Moreover, we can assign to that object.
Thus, the type deduced by decltype(*p) is int&, not plain int. (大概意思,*操作符修饰表达式,decltype返回表达式(*p)的lvalue(参考(4)lvalue rvalue的解释),所以最后decltype(*p)是int&,而不是int)
 
另一个auto和decltype推断类型不同的地方是decltype还依赖给予的表达式。将变量放入小括号中将会影响decltype的返回类型。将变量放val入小括号,decltype把(val)当做表达式,这种表达式的lvalue。
 
//decltype of a parenthesized variable is always a reference decltype((i)) d; // error: d is int& and must be initialized,because (i) is an lvalue decltype(i) e; // ok: e is an (uninitialized) int
 
 
3.Range-Based  for Loops(page)
语法格式:
for (declaration : expression)
    statemen
expression是一个包含顺序元素的类型
 
遍历一个string
string str("some string"); // print the characters in str one character to a line for (auto c : str) // for every char in str cout << c << endl; // print the current character followed by a newline
 
 
string s(; // count the number of punctuation characters in s for (auto c : s) // for every char in s if (ispunct(c)) // if the character is punctuation ++punct_cnt; // increment the punctuation counter cout << punct_cnt<< " punctuation characters in " << s << endl;
这个地方似乎就感觉到了decltype的方便性了。
 
如果需要修改元素,需要用引用,auto& c,输出结果:HELLO WORLD!!!
string s( "Hello World!!!" ); // convert s to uppercase for ( auto & c : s) // for every char in s (note: c is a reference) c = toupper(c); // c is a reference, so the assignment changes the char in s; cout << s << endl;
 
 
Warning
The body of a range for must not change the size of the sequence over
which it is iterating.
 
4.模板右边括号(page142)
在C++ 98中,vector<vector<int>> vctTemp是一个非法的表达式,编译器会认为右边的>>是一个移位操作符,因此必须修改为vector<vector<int> > vctTemp,即在右边的两个>中间添加一个空格。在C++ 11中,这将不再是一个问题,编译器将能够识别出右边的双括号是两个模板参数列表的结尾。
 
Warning
Some compilers may require the old-style declarations for a vector of
vectors, for example, vector<vector<int> >
 
5.初始化列表(page143)
在引入C++ 11之前,只有数组能使用初始化列表。在C++ 11中,vector、list等各种容器以及string都可以使用初始化列表了。初始化列表对应的类为initializer_list,vector、list等各种容器以及string之所以可以使用初始化列表,是因为它们重载了参数类型为initializer_list的构造函数(称为初始化列表构造函数)和赋值函数(称为初始化列表赋值函数)。
 
vector , , , , }; map , }); Print({ , , , , });
getchar(); ; }
 
vector); }; , ); , }; // v4 has two elements with values 10 and 1
 

 
vector}; , "hi"}; // v8 has ten elements with value "hi"

虽然v5,v7,v8都有括号,但只有v5是初始化列表。我们不能用int初始化string,编译器会根据给予的值选择另一种方式初始化。
 
 
 
constexptr,begin(),end(),cbegin,cend,using 的typedef用法
参考
(1)C++ primer 第五版
(3)http://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html C++11 新特性
(4)http://blog.chinaunix.net/uid-7471615-id-83794.html rvalue lvalue的解释

C++ 11 从C++ primer第五版的学习笔记的更多相关文章

  1. C#入门经典(第五版)学习笔记(二)

    ---------------函数---------------参数数组:可指定一个特定的参数,必须是最后一个参数,可使用个数不定的参数调用函数,用params关键字定义它们 例如: static i ...

  2. C#入门经典(第五版)学习笔记(四)

    ---------------集合.比较和转换--------------- C#中的数组是作为System.Array类的实例实现的,它们是集合类(Collection Classes)中的一种类型 ...

  3. C#入门经典(第五版)学习笔记(三)

    ---------------面向对象编程简介--------------- UML表示方法: 1)方框上中下三分 2)上框写类名 3)中框写属性和字段,例如:+Description:string  ...

  4. C#入门经典(第五版)学习笔记(一)

    ---------------变量和表达式---------------赋值运算符:+=:-=:*=:/=:%=例如:i+=j 相当于 i=i+j i-=j 相当于 i=i-j以此类推 按位运算符:& ...

  5. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

  6. C++PRIMER第五版练习题答案第一章

    C++PRIMER第五版练习题答案第一章 应该有很多小伙伴和我一样,闲来无事买了本C++的书自己啃,课后的练习题做的很揪心,这里我分享下我写的答案,希望能帮助到你,提供源码,就不跑了哈,毕竟现在是第一 ...

  7. 实验楼课程管理程序-深入学习《C++ Primer第五版》实验报告&学习笔记1

    本片博客为实验楼的训练营课程深入学习<C++ Primer第五版>的实验报告和学习笔记. 原课程地址为:https://www.shiyanlou.com/courses/405# 原文出 ...

  8. 精通ASP.Net MVC 3 框架(第三版)学习笔记

    精通ASP.Net MVC 3 框架(第三版)学习笔记 代码才是王道. http://pan.baidu.com/s/1pJyL1cn

  9. 毕业设计 之 五 PHP语法学习笔记

    毕业设计 之 四 PHP语法学习笔记 作者:20135216 平台:windows10 软件:XAMPP,DreamWeaver 说明:该笔记是对网站编程语言的详细学习 一.PHP基础 0. 关于环境 ...

随机推荐

  1. Struts2 源码分析-----Hello world

    今天第一天学习struts2,没学过怎么办,那当然是helloworld.感觉嘛,学习的基本流程都差不多,就是helloworld,开发环境,然后就是逐个按照知识点打demo,打着打着你就会发现str ...

  2. 【干货】JavaScript DOM编程艺术学习笔记4-6

    四.案例研究:JavaScript图片库 js: function showPic(whichpic){ //取得链接 var source=whichpic.getAttribute("h ...

  3. Activiti20180624

    1.工作流介绍 工作流(WorkFlow),是对工作流程及其各操作步骤之间业务规则的抽象.概括.描述.工作建模,即将工作流程中的工作如何前后组织在一起的逻辑和规则,在计算机中以恰当的模型进行表示并对其 ...

  4. spring的struts简单介绍

    之前一段时间学习了springmvc+mybatis+spring框架,突然对之前的struts东西有点陌生, 所以这里简单记录下温故而知新的东西吧. 1.  首先建立一个Dynamic Web Pr ...

  5. Cocos2d-x v3.1 安装图文教程(二)

       Cocos2d-x v3.1 安装图文教程(二) 如果我们需要在Android平台上运行就必须安装android的SDK,如果我们只想在window上运行就只需要安装Cocos2d-x就行了.当 ...

  6. python 多线程 生产者消费者

    import threading import time import logging import random import Queue logging.basicConfig(level=log ...

  7. web调试的一些小技巧

    1.不带缓存的刷新,用于刷新css或者js:Ctrl+F5 待续...

  8. pta 编程题10 Root of AVL Tree

    其它pta数据结构编程题请参见:pta 这道题考察平衡二叉查找树的插入. 为了保证二叉查找树的平衡,当一个结点的左右子树的高度差大于1时就要进行调整. 分为以下四种情况: 插入新节点后,以及旋转之后, ...

  9. c++树的表示方法

    c++树的节点的表示方法: typedef struct Node *Tree; struct Node { int data; Node *left; Node *right; int flag; ...

  10. 2018.10.05 TOPOI提高组模拟赛 解题报告

    得分: \(100+5+100=205\)(真的是出乎意料) \(T1\):抵制克苏恩(点此看题面) 原题: [BZOJ4832][Lydsy1704月赛] 抵制克苏恩 应该还是一个比较简单的\(DP ...