《C++ Primer Fifth Edition》 阅读及学习笔记

07 January 2015


前言

In 2011, the C++ standards committee issued a major revision to the ISO C++ standard.(2011年,C++标准协会发布了一个重大的ISO C++标准) This revised standard is latest step in C++’s evolution and continues the emphasis on programmer efficiency.(这次的标准改进是C++进化史上的最新一步,进一步强调的程序员的效率) The primary goals of the new standard are to(新标准的主要目标是):

为何读这本书?

现代的C++可以理解为由以下三部分组成:

  1. C语言的超集的低级语言(low-level language)部分
  2. 允许我们定义自己的类型以及组织大规模程序和系统的更高级语言功能(More advanced language features)
  3. 标准库,提供了有用的数据结构和算法

关于编译器

C++ Primer第五版这本书在编写的时候(2012年7月),常用的编译器是4.7.0版本的GNU编译器。只有一小部分特性当时还没实现,包括:构造器继承(inheriting constructors)、成员函数的引用限定符(reference qualifiers for member functions)以及正则表达式库(the regular-expression library)。

Chapter 1. Getting Started(搞起来)

1.1. Writing a Simple C++ Program(写一个简单的C++程序)

1.2. A First Look at Input/Output(初窥输入输出)

1. 标注输入流:istream类型的对象cin(standard input)
2. 标准输出流:ostream类型的对象cout(standard output)
3. cerr以及clog,通常将cerr当作标准错误流(standard error)用于警告和错误信息(warning and error messages),clog用于程序运行时的普通信息(general information)。
#include <iostream>
int main()
{
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "The sum of " << v1 << " and " << v2
	<< " is " << v1 + v2 << std::endl;
	return 0;
}

字符串(string literal)

Warning

标准库的命名空间

1.3. A Word about Comments(一言以蔽C++注释)

1.4. Flow of Control(控制流程)

1.4.1. The while Statement(while语法)

复合赋值运算符(compound assignment operator),如 += 运算符。
前缀自增运算符(prefix increment operator),如 ++val 运算符。
赋值运算符(assignment operator),如 =

1.4.1.The for Statement(for语法)

#include <iostream>
int main()
{
	int sum = 0;
	// sum values from 1 through 10 inclusive
	for (int val = 1; val <= 10; ++val)
	sum += val; // equivalent to sum = sum + val
	std::cout << "Sum of 1 to 10 inclusive is "
	<< sum << std::endl;
	return 0;
}

1.4.3. Reading an Unknown Number of Inputs(从输入中读取未知数字)

#include <iostream>
int main()
{
	int sum = 0, value = 0;
	// read until end-of-file, calculating a running total of all values read
	while (std::cin >> value)
	sum += value; // equivalent to sum = sum + value
	std::cout << "Sum is: " << sum << std::endl;
	return 0;
}

while (std::cin » value) 如果流有效(在流没有遇到错误的时候),即判定成功(true)。当我们输入一个EOF(end-of-file)或者非法的输入(输入的并非整数)时,输入流将失效。一个处于失效状态的输入流将导致条件判断为false。

如何从键盘中输入EOF:

1.4.4. The if Statement(if 语句)

Key Concept: Indentation and Formatting of C++ Programs(C++程序的缩进及格式)

1.5. Introducing Classes(关于类的介绍)

1.5.1. The Sales_item Class (没干货,略)

1.5.2. A First Look at Member Functions(メンバー関数を見るのは始めてだ)

代码如:

#include <iostream>
#include "Sales_item.h"
int main()
{
	Sales_item item1, item2;
	std::cin >> item1 >> item2;
	// first check that item1 and item2 represent the same book
	if (item1.isbn() == item2.isbn()) {
		std::cout << item1 + item2 << std::endl;
		return 0; // indicate success
	} else {
		std::cerr << "Data must refer to same ISBN"
		<< std::endl;
		return -1; // indicate failure
	}
}

1.6. The Bookstore Program (没干货,略)

Defined Terms(术语)