C++标准库第二版笔记 2
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的更多相关文章
- C++标准库第二版笔记 3 和异常的理解 1
C++标准库第二版笔记 3 和异常的理解 1 差错和异常(error and exception)的处理 标准异常类(exception class) 定义于 分为: 1.语言本身支持的异常 2.标准 ...
- C++标准库第二版笔记 2.1
C++标准库第二版笔记 2.1 1 Range-Based for 循环 for ( decl : coll ) { statements; } // collaborate 类似C# foreach ...
- C++标准库第二版笔记 1
C++标准库第二版笔记 1 C++ std历史 第一份标准化文档: C++98 & C++03 & TR1 TR1 Information Technology- Programmin ...
- python核心编程第二版笔记
python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d 提供调试输出1.2 –O 生成优化的字节码(生成 ...
- 《C标准库》学习笔记整理
简介 <C标准库>书中对 C 标准库中的 15 个头文件的内容进行了详细的介绍,包括各头文件设计的背景知识.头文件中的内容.头文件中定义的函数和变量的使用.实现.测试等. 我学习此书的目的 ...
- Python高级编程第二版--笔记
不只是CPython Stackless Python Jython(与java集成) IronPython(与net集成) PyPy python真正出众的领域在于围绕语言打造的整个生态系统. Py ...
- 《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 ...
- JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(<delimited-identifiers/>、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)
一.自定义映射的表名 1. @Entity @Table(name = "USERS") public class User implements Serializable { / ...
- JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-002identity详解
一.简介 1.You now have three methods for distinguishing references: Objects are identical if they occ ...
随机推荐
- Pycharm2018的激活方法或破解方法(必须加host)
修改hosts文件将0.0.0.0 account.jetbrains.com添加到hosts文件最后,注意hosts文件无后缀,如果遇到无法修改或权限问题,可以采用覆盖的方法去替换hosts文件 修 ...
- CSS的background
.block{ width: 200px; height: 200px; padding: 25px; background-image:linear-gradient(#58a,#58a) ,lin ...
- Codeforces Round #551 (Div. 2) A-E
A. Serval and Bus 算出每辆车会在什么时候上车, 取min即可 #include<cstdio> #include<algorithm> #include< ...
- 装饰器 -- 函数装饰器(tornado异常响应装饰器)
# 值可变,每次使用需要重新赋值 ERR_RESP_TEMPLATE = {"state": "FAILED", "error": None ...
- 0初识Linux
今天三八妇女节,Linux就该这么学,开课第一天.信心满满,激动,期待,要努力了.(博客为预习写的,今天又做了更新.) Linux第一印象就是黑色背景屏幕,上面还有好多代码,敲的一手好的命令操控着 ...
- win7频繁提示资源管理器已停止工作解决办法
方法一,重新启动资源管理器,右键点击桌面下方的“任务栏”空白区,在弹出的菜单栏中选择“任务管理器”. 进入任务管理器,点击上方的“文件”,选择新建任务. 在弹出的对话框中,输入explorer ...
- Linux的Namespace与Cgroups介绍
Namespace 的概念 Linux Namespace 是kernel 的一个功能,它可以隔离一系列系统的资源,比如PID(Process ID),User ID, Network等等.一般看到这 ...
- SSM商城项目(九)
1. 学习计划 1.Activemq整合springMQ的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. ...
- office 安装
在 gaobo百度云下载安装包. 自定义安装,并在自定义界面选择安装路径. 破解:
- EL表达式与标签库
https://blog.csdn.net/panhaigang123/article/details/78428567