《Accelerated C++》导读(Chapter 2)

发布于:2023-01-21 ⋅ 阅读:(429) ⋅ 点赞:(0)

Code:[ZachxPKU/AcceleratedCPlusPlus]

Chapter 2 Looping and counting

2.1 The problem

2.2 Overall structure

2.3 Writing an unknown number of rows

2.3.1 The while statement

  • while statement while语句
  • while body while循环体
  • condition 条件
  • inequality operator 不等号
  • bool 布尔类型
    • true
    • false
  • increment operator 累加操作符
    • r++
    • ++r

Either the statement is indeed just a statement, or it is a block, which is a sequence of zero or more statements enclosed in { }. If the statement is just an ordinary statement, it will end with a semicolon of its own, so there’s no need for another one.
语句既可以是单独的一条语句,也可以是一个快语句。只有普通语句后面要加分号,块语句末尾则不需要。

2.3.2 Designing a while statement

  • loop invariant 循环不变式

本节主要介绍loop invariant 循环不变式技术,其主要作用是帮助理解循环。我个人觉得这个技术的实际意义不是很大。或者是因为目前遇到的循环理解起来并不费劲,不需要借助这个框架来理解。

2.4 Writing a row

std::string::size_type cols = greeting.size() + pad * 2 + 2;

需要理解上面代码中使用size_type的考虑,这本书再一次较早的介绍一些其他入门书籍中不会介绍,或者在后期介绍的一些概念。所以,我认为这本书对于完全不熟悉任何一本编程语言,甚至完全没有接触过C(C++)的人,作为入门书,学习曲线还是比较陡峭的。

2.4.1 Writing border characters

2.4.1.1 if statement

2.4.1.2 Logical Operators

  • equality == 等于
  • assignment = 赋值
  • logical-or || 或
  • precedence 优先级
  • arithmetic 算数
  • relational 关系
  • short-circuit evaluation 短路求值

短路求值会产生一些非常有趣的现象,大家需要注意。这些有趣现象产生的原因是因为语句中的部分表达式可能不会被执行,而这些表达式可能是赋值语句等。

2.4.2 Writing nonborder characters

  • logical-and && 与
  • compound-assignment 复合复制

2.5 The complete framing program

2.5.1 Abbreviating repeated uses of std::

  • using-declaration using声明

Logically enough, such a declaration is called a using-declaration. The name that it mentions behaves similarly to other names. For example, if a using-declaration appears within braces, the name that it defines has its given meaning from where it is defined to the closing brace.(花括号里面的using声明,只管该scope,具体可以参考exercises中的2-10。终于,不用疯狂写std::了,哈哈哈)

2.5.2 Using for statements for compactness

  • off-the-end value 越界值
  • half-open range 半开区间
  • for statement for语句
  • for header for循环头
  • for body for循环体

需要注意的是,书中for循环的格式:

for(init-statement condition; expression)
	statement

并没有错误,因为init-statement是带分号的,expression是不带分号的。这种展现形式和国内很多教材都不一样。我觉得各有好处。

{
	init-statement
	while(condition){
		statement
		expression;
	}
}

for循环等价于上面的结构,需要注意while的外面还有一层{}。这层{}有什么作用?保证for里面定义的变量,在for循环结束后能全部销毁。

2.5.3 Collapsing tests

书中的代码:

if(r == pad + 1 && c == pad + 1) {  
    cout << greeting;  
    c += greeting.size();  
} else{  
    if(r ==0 || r == rows - 1 || c == 0 || c == cols - 1)  
        cout << "*";  
    else        
	    cout << " ";  
	    ++c;  
}

++c这一句的缩进并没有错误,因为,c++并不是像python一样依靠缩进控制。但是改为下面的格式会更加直观:

if(r == pad + 1 && c == pad + 1) {  
    cout << greeting;  
    c += greeting.size();  
} else{  
    if(r ==0 || r == rows - 1 || c == 0 || c == cols - 1)  
        cout << "*";  
    else        
	    cout << " ";  
    ++c;  
}

2.5.4 The complete framing program

2.6 Counting

解释为什么循环计数从0开始,以及为什么是半开区间等问题。这节值得一读。

2.7 Details

  • x / y Quotient of x and y. If both operands are integers, the implementation chooses whether to round toward zero or - ∞ 整数除法的结果,和编译器的实现有关。
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到