C++标准库第二版笔记 2


微小但重要的语法提升

template表达式内的空格:

vector< list<int> >; // OK in each C++ version
vector<list<int>>; // OK since C++11

取消二异性的nullptr std::nullptr_t

void f(int);
void f(void*); f(0) // calls f(int)
f(NULL) //calls f(int) if NULL is 0, ambiguous otherwise.
f(nullptr) // calls f(void*)

注意std::nullptr_t被视为一个基础类型。

以auto完成类型自动推导

auto i; // ERROR: cant deduce the type of i
auto l = [] ( int x )-> bool {...,}; // lambda type // auto& 将尽可能保存原来的类型
const int a2 = 10;
auto &b2 = a2; // b2 is a const type int a3 [] { 1, 2, 3 };
auto b3 = a3; // b3 is a pointer
auto & b7 = a3; // b7 is an array

一致性初始化(Uniform initialization)与初始列(Initializer List)

如今,C++标准化了初始行为。它依赖实际值(actual value)而非依赖类型。若不造成精度损失,则不算为窄化。浮点数到整数永远是一种窄化。

type var { 1, 2, 3 };
type var[] { 1, 2, 3 };
vector<string> var{ "Berlin", "New York", "Shanghai" }; // 而默认行为可以强迫造成初始赋值
int j{}; // j is initialized by 0
int * q{}; // q is initialized by nullptr // 窄化(narrowing),也就是精度降低或造成数值变动丢失,对大括号不成立。
int x1(5.3); // OK
itn x2 = 5.3; // OK
int x3{ 5.0 } //ERROR
char c2{999999}; // ERROR if 99999 doesnt fit into a char
vector<int> v2 { 1, 2.3, 4, 5.6 }; // ERROR

std::initializer_list<>

void print( std::initializer_list<int> vals ) {
for (auto p = vals.begin(); p!=vals.end; ++p) {
std::cout<<*p<<"\n";
}
} print( { 1, 2, 3, 4, 5, 6, 7 } ); class p {
public:
P(int,int); // cons 1
P(std::initializer_list<int>); // cons 2
};
P p( 77, 5 ); // calls cons1
P q{ 77, 5 }; // calls cons2
P r{ 77, 5, 42 }; // calls cons2
P s = { 77, 5 }; // calls cons2

由于初始列的出现,现在explicit关键词已不局限于单个构造函数出现时的隐式转化。

class p {
public:
explicit P(int,int,int); // cons 1
P(std::initializer_list<int>); // cons 2
}; P w = { 77, 5, 42 }; // ERROR // 同下失去隐式转化能力
void fp(const P&);
fp({11,22,3}); // ERROR

C++标准库第二版笔记 2的更多相关文章

  1. C++标准库第二版笔记 3 和异常的理解 1

    C++标准库第二版笔记 3 和异常的理解 1 差错和异常(error and exception)的处理 标准异常类(exception class) 定义于 分为: 1.语言本身支持的异常 2.标准 ...

  2. C++标准库第二版笔记 2.1

    C++标准库第二版笔记 2.1 1 Range-Based for 循环 for ( decl : coll ) { statements; } // collaborate 类似C# foreach ...

  3. C++标准库第二版笔记 1

    C++标准库第二版笔记 1 C++ std历史 第一份标准化文档: C++98 & C++03 & TR1 TR1 Information Technology- Programmin ...

  4. python核心编程第二版笔记

    python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d   提供调试输出1.2 –O   生成优化的字节码(生成 ...

  5. 《C标准库》学习笔记整理

    简介 <C标准库>书中对 C 标准库中的 15 个头文件的内容进行了详细的介绍,包括各头文件设计的背景知识.头文件中的内容.头文件中定义的函数和变量的使用.实现.测试等. 我学习此书的目的 ...

  6. Python高级编程第二版--笔记

    不只是CPython Stackless Python Jython(与java集成) IronPython(与net集成) PyPy python真正出众的领域在于围绕语言打造的整个生态系统. Py ...

  7. 《C++ 标准库》读书笔记 - 第二章 Introduction to C++ and the Standard Library

    1. History of the C++ Standards 1.1 History of the C++ Standards C++98 -> C++03 -> TR1 -> C ...

  8. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(<delimited-identifiers/>、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)

    一.自定义映射的表名 1. @Entity @Table(name = "USERS") public class User implements Serializable { / ...

  9. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解

    一.简介 1.You now have three methods for distinguishing references:  Objects are identical if they occ ...

随机推荐

  1. The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation for 'show_compatibility_56'

    --从mysql5.7.6开始information_schema.global_status已经开始被舍弃,为了兼容性,此时需要打开 show_compatibility_56 mysql> ...

  2. c# 数据结构 ArrayList

    数据结构 描述数据之间的关系 行为:添加数据,删除数据,插入数据,查找数据,修改数据 追加数据:向这个结构的末尾添加一个数据 删除数据:在这个结构中删除你指定的数据 插入数据:向这个结构中某一个位置插 ...

  3. MYSQL SQL语句技巧初探(一)

    MYSQL SQL语句技巧初探(一) 本文是我最近了解到的sql某些方法()组合实现一些功能的总结以后还会更新: rand与rand(n)实现提取随机行及order by原理的探讨. Bit_and, ...

  4. oracle体系结构理解

    体系结构相关内容每次看遍书,过段时间就忘了..无奈用自己理解的方式记录之. 1.commit与写盘与否没有关系,也就是说修改数据(insert update delete)后并提交数据,这条被修改的数 ...

  5. day29单例模式的4种实现模式

    单例模式的四种实现模式单例模式实现方式一: import settings class MySQL:  __instance=None  def __init__(self, ip, port):   ...

  6. 游戏中转盘概率的算法---python实现

    加入转盘的内容及概率如下 转盘倍数 0.5 0.6 0.7 0.8 1 1.2 1.5 1.8 2 机率 0.2 0.15 0.15 0.2 0.2 0.1 0.1 0.05 0.05 下面来实现转盘 ...

  7. 插件: Hammer.js

    官网: http://hammerjs.github.io/  hammer.js 官网 http://hammerjs.github.io/api/ 官网API(官网写的实在太简了!不好用.注意里面 ...

  8. spring boot 之 spring security 配置

    Spring Security简介 之前项目都是用shiro,但是时过境迁,spring security变得越来越流行.spring security的前身是Acegi, acegi 我也玩过,那都 ...

  9. CentOS 7 无法yum安装解决方法

    1)下载repo文件 wget http://mirrors.aliyun.com/repo/Centos-7.repo 2)备份并替换系统的repo文件 .repo /etc/yum.repos.d ...

  10. openvas安装和基本使用

    OpenVAS是开放式漏洞评估系统,也可以说它是一个包含着相关工具的网络扫描器. OpenVAS是开放式漏洞评估系统,也可以说它是一个包含着相关工具的网络扫描器.其核心部件是一个服务器,包括一套网络漏 ...