A tour of C++ Day3

A tour of C++ Book Note Day3

Just for share my note when I reading the book. If you want to discuss what interests you, you can write in the comments section. If there is a dispute, you can corret it!

1.4 Types Variables and Arithmetic

Types/type quote16

  • 每一个变量名和表达式都有一个类型,而该类型决定了其可能被执行的操作
    • 或者说,类型决定了该类型一系列可用的操作

Every name and every expression has a type that determines the operations that may be performed on it. ^quote16

Types/declaration ^quote17

  • 声明是将一个实体引入进程序和指定其类型的语句

A declaration is a statement that introduces an entity into the program and specifies its type ^quote17

Types/value quote18

  • 对象是包含(保存)某种类型值的内存空间
    • 其实通俗点说就是实例化(对象)是一堆值的集合
  • value是根据类型对一系列二进制集合的解释
    • 也就是说,信息不仅仅只有表示,还有解释
    • 带解释的信息被称为值 -> value, 而类型其实就是一种表示

An object is some memory that holds a value of some type.
A value is a set of bits interpreted according to a type. ^quote18

Variables/type quote19

  • 类型的大小是由实现定义的
    • 因此,不同机器上对其的实现不同,其表现的类型大小也不尽相同

The size of a type is implementation-defined (i.e., it can vary among different machines)^quote19

Variables/tip quote20

  • 为了使长字节文字更利于阅读,我们可以使用单引号(')左右一个数字分隔符
    • 同时,0x的前缀表示十六进制数,0前缀表示八进制数,0b前缀表示二进制数

To make long literals more readable for humans, we can use a single quote () as a digit separator. ^quote20

double x = 3.1415'9265'4646;  
double x1 = 0x7546'ddde'45;  
double x2 = 04657'123;  
double x3 = 0b0101'1100'1101;

1.4.1 Arithmetic

Arithmetic/trans1 quote21

  • 因为历史优化的原因,其他表达式(例如 f ( x ) + g ( y ) f(x)+g(y) f(x)+g(y))和函数参数(例如 h ( f ( x ) , g ( y ) ) h(f(x), g(y)) h(f(x),g(y)))的运算顺序很不幸地是未定义的
    • 这也就说明了,为什么谭教授的小红书害人不浅

For historical reasons realated to optimization, the order of evaluation of other expressions (e.g., f(x)+g(y)) and of function arguments (e.g.,h(f(x),g(y))) is unfortunately unspecified. ^quote21

// for exmaple, if we write a code
i = 5;
printf("%d%d%d", i++, ++i, i++);

// the code's output is unsoecified, maybe it was 8 8 8, or 5 5 8...
// in fact, it depends on the platform such as gcc, msvc or clang...

1.4.2 Initialization

Initialization/initializer_lists quote22

  • =赋值操作符是传统的且来源于C的,但是如果你对值的类型不确定,使用常规的{}初始化列表可以避免这个问题,除此之外,它还能帮你避免隐式转换所导致的信息丢失
    • 初始化列表规定了必须显示的初始化一个变量(对象) -> 保证了数据的一致性,且减少了隐式类型转换的发生

The = form is traditional and dates back to C, but if in doubt, use the general {}-list form. If nothing else, it saves you from conversions that lose information ^quote22

int i = 7.54;  // in this statement, the i of value is 7, loss the float-point
int i {7.54};  // in this statement, it was error, can't pass the compile

Initialization/constant quote23

  • 常量不能是未初始化的,且一个变量只有在极少数条件下才不会被初始化
    • 对变量(甚至是常量)初始化是一个好习惯,尽管你可能当时没有使用

A constant cannot be left uninitialized and a variable should only be left uninitialized in extremely rare circumstances. ^quote23

Initialization/type quote24

  • 对于内置类型(例如string、vector…)能够在定义的时候隐式的初始化
    • 实际上就是在实例化时,C++默认调用了其默认构造函数

User-defined types (such as string, vector, Matrix, Motor_controller, and Orc_warrior) can be defined to be implicitly initialized ^quote24

Initialization/auto quote25

  • 我们会在不需要在特殊情况下清晰的指明类型的时候使用auto,比如:
    • 定义的类型范围很大,我们需要让阅读代码的人清晰的理解该类型
    • 初始化类型不明确的时候
    • 我们需要明确说明变量的范围或者精度的时候(例如,double 而非 float)

[!question] why when we need explicited variable to use auto rather that specific type?

because compiler is well to use that

We use auto where we don’t have a specific reason to mention the type explicitly. “Specific reasons” include:

The definition is in a large scope where we want to make the type clearly visible to readers of our code.
The type of the initializer isn’t obvious.
We want to be explicit about a variable’s range or precision (e.g., double rather than float). ^quote25

Initialization/auto_use quote26

  • 这在范型编程中尤为重要,因为程序员很难知道一个对象的确切类型,而且这个对象的类型名可能很长

This is especially important in generic programming where the exact type of an object can be hard for the programmer to know and the type names can be quite long ^quote26

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
“a tour of c” 可以理解为C语言的一次巡回之旅。C语言是一种广泛应用于计算机硬件和软件开发的编程语言。它由贝尔实验室的丹尼斯·里奇于1972年开发,并在接下来的几年中逐渐流行起来。以下是关于C语言巡回之旅的一些重要内容: 首先,C语言具有简洁、高效的特点。它使用了一些基本的语法结构和关键字,使得程序代码更加简洁,易于理解和维护。同时,C语言的编译过程相对快速,生成的可执行文件也具有较高的执行效率。 其次,C语言具有广泛的应用领域。它被用于开发系统级软件,如操作系统和编译器,也被广泛应用于游戏开发、嵌入式系统、网络编程等领域。通过一次C语言的巡回之旅,可以深入了解C语言在不同领域中的应用和实践。 另外,C语言还具有丰富的库函数和标准函数供开发人员使用。这些库函数可以提供各种功能,如文件操作、字符串处理、内存管理等。通过学习和了解这些库函数,可以提高编程效率,并加快程序的开发速度。 最后,C语言也是学习其他高级编程语言的基础。许多其他编程语言,如C++、Java、Python等,都是在C语言基础上发展起来的。因此,通过一次C语言的巡回之旅,可以掌握一些基本的编程概念和思维方式,为学习其他编程语言打下坚实的基础。 总之,C语言巡回之旅是一个了解C语言的机会,通过深入学习和实践,可以了解C语言的特点、应用及其在计算机科学领域的重要性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值