Code:[ZachxPKU/AcceleratedCPlusPlus]
Accelerated C++
Code:[ZachxPKU/AcceleratedCPlusPlus]
Chapter 0 Getting Started
这一章,掌握理解以下英文单词所指即可。
//a small C++ program
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
0.1 Commnets
- comment 注释
0.2 #include
- core language 核心语言
- standard library 标准库
- angle brackets 尖括号
- standard header 标准头文件
0.3 The main function
- function 函数
- call 调用
0.4 Curly braces
- curly braces 大括号
- statements 语句
0.5 Using the standard library for output
- output operator 输出操作符
- namespace 命名空间
- standard output stream 标准输出流
0.6 The return statement
0.7 A slightly deeper look
- expression 表达式
- result 结果
- side effects 副作用
- operator 操作符
- <<
- operand 操作数
- std::cout
- “Hello, World!”
- type 类型
- left-associative 左结合
- manipulator 控制器
- scope 作用域
- qualified name 限定名称
- scope operator ::
0.8 Details
Tips
Tip 0-1
自己组织语言,解释以下语句,尽可能多的使用上面所列出的英文单词
std::cout << "Hello, world!" << std::endl;
Tip 0-2
Exercises 中的0-10
#include <iostream>
int
main
(
)
{
std
::
cout
<<
"Hello, world!"
<<
std
::
endl
;
return
0
;
}
Chapter 1 Working with strings
1.1 Input
// ask for a person's name, and greet the person
#include <iostream>
#include <string>
int main() {
// ask for the person's name
std::cout << "Please enter your first name: ";
//read the name
std::string name; //define name
std::cin >> name;
// write a greeting
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
variable 变量
object 对象
definition 定义
local variable 局部变量
destroy 销毁
interface 接口
initialize 初始化
whitespace 空白
buffer 缓冲
flush 刷新
本节需要注意两点:
- 输入会忽略前面的空白(whitespace)比如:空格、Tab、回车等,
- 输出缓存刷新的三种情况
- 缓存满了
- 等待输入
- 强制输出
Please enter your first name:
Zach
Hello, Zach!
同时,exercises中的1-6也涉及内存刷新的问题:
#include <iostream>
#include <string>
int main()
{
std::cout << "What is your name? ";
std::string name;std::cin >> name;
std::cout << "Hello, " << name
<< std::endl << "And what is yours? ";
std::cin >> name;
std::cout << "Hello, " << name
<< "; nice to meet you too!" << std::endl;
return 0;
}
输出结果将会是:
What is your name? Zach Tom
Hello, Zach
And what is yours? Hello, Tom; nice to meet you too!
1.2 framing a name
#include <iostream>
#include <string>
int main(){
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;
// build the message that we intend to write
const std::string greeting = "Hello, " + name + "!";
// build the second and fourth lines of the output
const std::string spaces(greeting.size() + 2, ' ');
const std::string second = '*' + spaces + '*';
// build the first and fifth lines of the output
const std::string first(second.size(), '*');
// write it all
std::cout << std::endl;
std::cout << first << std::endl;
std::cout << second << std::endl;
std::cout << "* " << greeting << " *" << std::endl;
std::cout << second << std::endl;
std::cout << first << std::endl;
return 0;
}
注意上面这段程序与书中示例略有不同,主要改动为:
const std::string spaces(greeting.size(), ' ');
const std::string second = '* ' + spaces + ' *'; //书中原文
const std::string spaces(greeting.size() + 2, ' ');
const std::string second = '*' + spaces + '*';
因为,我认为对于spaces的表述,这样更合理。
convert 转换
concatenate 把什么连在一起
overloaded 重载
const constant 常量
construct 构造
member function 成员函数
character literal 字符字面量
这本书相对于其他C++书,很早地介绍重载的概念,需要注意理解。同一个操作符(operator)对于不同类型(type)的操作数(operand),具有不同的效果。也需要注意操作符的结合性(associativity)不随操作数而变,始终保持如一。
同时,本节比较重要的还有常量的概念。
1.3 Details
这里需要注意的是两个字符串字面量不能相加:
std::cout << "ada" + "fsd" << std::endl;
上面的代码会报错。至于为什么?因为对于 ‘+’,没有对应形式的重载函数。 exercises中的1-2也说明了这个问题。
const 常量,只会保证它的值,而不会改变它的生命周期。常量必须在定义时初始化,否则后面无法赋值。exercises中的1-3、1-4和1-5说明了const作用域的问题,两个程序都是valid的。但是要注意1-4,我们稍作改动,如下:
#include <iostream>
#include <string>
int main() {
{
const std::string s = "a string";
std::cout << s << std::endl;
{
const std::string s = "another string";
std::cout << s << std::endl;
}
std::cout << s << std::endl;
}
return 0;
} //valid
上面的程序输出将会是:
a string
another string
a string
第三个cout还是会输出“a string”,解释这一点,需要考虑程序的堆栈结构。