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. 检查python以及django是否安装配置成功

    首先说明下,我使用pycharm作为开发的IDE,在第一次创建django项目的时候,会自动安装django包的.(网上也有很多单独安装的方法),环境变量配置成功后,就是用下面的方法检测安装成功与否. ...

  2. DockerSwarm 微服务部署

    一.简介 之前<服务Docker化>中,使用 docker-compose.yml 来一次配置启动多个容器,在 Swarm 集群中也可以使用 compose 文件 (docker-comp ...

  3. SQL语句中的having和where的区别

    --首先,两个都是用来进行筛选的: --区别在于 1.当分组筛选的时候使用having eg: 在emp中,查出最低工资小于1000的部门号 select deptno from emp group ...

  4. bootstrapTable的数据后端分页排序

    数据后端分页排序,其实就是sql语句中oeder by做一些限制. 之前在写sql语句中的order by是写死,既然要写活,就要传参数到后台. 之前讲到bootstrapTable的queryPar ...

  5. C# 执行可执行文件

    可以用C#脚本执行可执行文件,一般可以用C# IO流写出.bat脚本,然后顺带执行脚本,然后滑稽.三连... Process proc = null; try { proc = new Process ...

  6. HTML5 参数传递

    页面显示效果,如下图: 主页面代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  7. Android商城开发系列(十)—— 首页活动广告布局实现

    在上一篇博客当中,我们讲了频道布局的实现,接下来我们讲解一下活动广告布局的实现,效果如下图: 这个是用viewpager去实现的,新建一个act_item.xml,代码如下所示: <?xml v ...

  8. SEO人士一定要了解的搜索引擎惩罚原则

       SEO人士一定要了解的搜索引擎惩罚原则 SEO 的人一般都知道SEO分为白帽,黑帽,甚至还有灰帽.简单说,如果你熟读过谷歌网站质量指南,就可以了解,符合搜索引擎质量规范并且符合用户体验的SEO ...

  9. Object类和String类

    Object类 Object类是Java语言中的根类,即所有类的父类. equals方法 返回值类型为:boolean类型 用于比较两个对象是否相同,它其实就是使用两个对象的内存地址在比较. 例子: ...

  10. maven手动导入jar包到本地仓库

    一.cmd进入maven的bin目录下(我的目录是E:\cloud_cms\apache-maven-3.5.4\bin) cd E:\cloud_cms\apache-maven-3.5.4\bin ...