Chapter 1.1

1. 每个C++程序都必须有且只能有一个main函数,main函数的返回类型必须是int。操作系统通过调用main函数来运行C++程序。

2. 一个函数的定义包含四部分:返回类型、函数名、形参列表以及函数体。

3. 当return语句包含一个值时,此返回值的类型必须与函数的返回类型相兼容。

4. 类型是程序设计最基本的概念之一。一种类型不仅定义了数据元素的内容,还定义了这类数据上可以进行的运算。

Chapter 1.2

1. C++包含了一个全面的标准库,标准库就是一个类型和函数的集合,每个C++编译器都必须支持。

2. 标准库中包含了iostream库,用来提供IO机制。iostream库包含了两个基础类型istream和ostream,分别表示输入流和输出流。一个流就是一个字符序列,术语“流”想要表达的是,随着时间的推移,字符是顺序生成或消耗的。

3. 标准库定义了4个IO对象:1. cin,一个istream类型的对象,也被称为标准输入,用来处理输入 2. cout,一个ostream类型的对象,也被称为标准输出,用来处理输出 3. cerr,一个ostream类型的对象,也被称为标准错误,通常写入到与标准输出相同的设备,用来输出错误信息或其他不属于程序正常逻辑的输出内容。默认情况下,写到cerr的数据是不缓冲的 4. clog,一个ostream类型的对象,用来报告程序的执行信息。默认情况下,写到clog的数据是被缓冲的。

4. #include指令和头文件的名字必须写在同一行中。

5. 输出(<<)运算符的计算结果就是其左侧运算对象。代码:

std::cout << "Enter two numbers:" << std::endl;

使用了输出运算符两次。因为输出运算符的结果是其左侧运算对象,所以第一个运算符的结果成为了第二个运算符的左侧

运算对象。因此,我们的代码等价于:

// 表达式 std::cout << "Enter two numbers:" 的运算结果是std::cout
std::cout << "Enter two numbers:";
std::cout << std::endl;

6. 输入(>>)运算符与输出运算符类似,返回其左侧运算对象作为其计算结果。因此,代码:

int v1 = , v2 = ;
std::cin >> v1 >> v2;

等价于:

// 表达式 std::cin >> v1 的运算结果是std::cin
int v1 = , v2 = ;
std::cin >> v1;
std::cin >> v2;

7. IO设备通常将输入(或输出)数据保存在一个缓冲区中。

8. std::endl是一个被称为操纵符的特殊值。写入std::endl的效果是结束当前行,并将与设备关联的缓冲区中的内容刷到设备中。

9. 标准库定义的所有名字都在命名空间std中。

 

Chapter 1.3

1. 编译器会忽略注释,因此注释对程序的行为或性能不会有任何影响。

2. C++中有两种注释:单行注释和界定符对注释。

单行注释以双斜线(//)开始,以换行符结束。这种注释可以包含任何文本,包括额外的双斜线。

界定符对注释以“/*”开始,以“*/”结束,可以包含除“*/”外的任意内容。

3. 界定符对注释不能嵌套。下面的代码中嵌套使用界定符对注释就会产生错误:

int main()
{
/* /* 注释1 */ 注释2 */
// "注释2 */" 会被当做源码
return ;
}

Chapter 1.4

1. 所谓语句块,就是用花括号包围的零条或多条语句的序列。语句块也是语句的一种,在任何要求使用语句的地方都可以使用语句块。

2. while语句的形式为:

while ( condition )    // 所谓condition就是一个产生真或假的结果的表达式
statement

while语句的执行过程是交替地检测condition和执行关联的语句statement,直至condition为假时停止。

3.

for ( int val = ; val <= ; ++val )
statement;

上述代码使用了for语句。每个for语句都包含两部分:循环头和循环体。循环头控制循环体的执行次数,它由三部分组成:初始化语句、循环条件以及表达式。在上述代码中,初始化语句为“int val = 1”,初始化语句只在for语句入口处执行一次。在上述代码中,循环条件为“val <= 10”,循环体每次执行前都会先检查循环条件,只要循环条件为真,循环体就会被执行。在上述代码中,表达式为“++val”,表达式在循环体之后执行,执行完表达式后,for语句就会重新检测循环条件。

4. 当使用一个istream对象作为条件时,其效果是检测流的状态。如果流是有效的,则条件为真。当遇到文件结束符或遇到一个无效输入时,istream对象的状态会变为无效。处于无效状态的istream对象会使条件变为假。

5. 当用键盘向程序输入数据时,对于如何指定文件结束,不同操作系统有不同的约定。在Windows系统中,输入文件结束符的方法是Ctrl + Z。在Unix系统中,包括Mac OS X系统中,输入文件结束符的方法是Ctrl + D。

Chapter 1.5

1. 每个类实际上都定义了一个新的类型,其类型名就是类名。

2. 大多数操作系统支持文件重定向,这种机制允许我们将标准输入和标准输出与命名文件关联起来:

$ addItems <infile >outfile

假定$是操作系统提示符,应用程序为addItems.exe,则上述命令会从一个名为infile的文件读取销售记录,并将输出结果写入到一个名为outfile的文件中,两个文件都位于当前目录中。

Exercises Section 1.2

Q_1. Write a program to print Hello, World on the standard output.

A_1.

#include <iostream>

int main()
{
std::cout << "Hello, World" << std::endl; return ;
}

Q_2. Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead.

A_2.

#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2; std::cout << "The product of " << v1 << " and " << v2 << " is " << v1 * v2 << std::endl; return ;
}

Q_3. We wrote the output in one large statement. Rewrite the program to use a separate statement to print each operand.

A_3.

#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2; std::cout << "The sum of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 + v2;
std::cout << std::endl; return ;
}

Q_4. Explain whether the following program fragment is legal.

std::cout << "The sum of " << v1;
<< " and " << v2;
<< " is " << v1 + v2 << std::endl;

If the program is legal, what does it do? If the program is not legal, why not? How would you fix it?

A_4. 这个程序是非法的,v1后的分号表示了一条语句的结束,所以程序会认为“<< " and " << v2;”是一条独立的语句,而这条语句缺少了左侧运算对象。同理,“<< " is " << v1 + v2 << std::endl;”也缺少了左侧运算对象。

修正如下:

std::cout << "The sum of " << v1    // 去除分号
<< " and " << v2 // 去除分号
<< " is " << v1 + v2 << std::endl;

Exercises Section 1.3

Q_1. Indicate which, if any, of the following output statements are legal:

std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;
std::cout << /* "*/" /* "/*" */;

After you've predicted what will happen, test your answers by compiling a program with each of these statements. Correct any errors you encounter.

A_1.

std::cout << "/*";    // 合法

std::cout << "*/";    // 合法

std::cout << /* "*/" */;
// 非法,修正为:
std::cout << /* "*/" */"; std::cout << /* "*/" /* "/*" */; // 合法

Exercises Section 1.4.1

Q_1. Write a program that uses a while to sum the numbers from 50 to 100

A_1.

#include <iostream>

int main()
{
int sum = , val = ;
while ( val <= )
{
sum += val;
++val;
} std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; return ;
}

Q_2. In addition to the ++ operator that adds 1 to its operand, there is a decrement operator (--) that subtracts 1. Use the decrement operator to write a while that prints the numbers from ten down to zero.

A_2.

#include <iostream>

int main()
{
int val = ;
while ( val >= )
{
std::cout << val << std::endl;
--val;
} return ;
}

Q_3. Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.

A_3.

#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2; while ( v1 <= v2 )
{
std::cout << v1 << std::endl;
++v1;
} return ;
}

Exercises Section 1.4.2

Q_1. What does the following for loop do? What is the final value of sum?

int sum = ;
for (int i = -; i <= ; ++i)
sum += i;

A_1. 上述代码的功能是计算-100到100的和,最终sum的结果是0。

Q_2. Rewrite the exercises from § 1.4.1 (p. 13) using for loops

A_2.

#include <iostream>

int main()
{
int sum = ;
for ( int val = ; val <= ; ++val )
{
sum += val;
} std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; return ;
}
#include <iostream>

int main()
{
for ( int val = ; val >= ; --val )
{
std::cout << val << std::endl;
} return ;
}
#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2; for ( ; v1 <= v2; ++v1 )
{
std::cout << v1 << std::endl;
} return ;
}

Exercises Section 1.4.3

Q_1. Write your own version of a program that prints the sum of a set of integers read from cin.

A_1.

#include <iostream>

int main()
{
int sum = ;
for ( int val; std::cin >> val; )
{
sum += val;
} std::cout << "Sum is: " << sum << std::endl; return ;
}

Exercises Section 1.4.4

Q_1. Revise the program you wrote for the exercises in § 1.4.1 (p. 13) that printed a range of numbers so that it handles input in which the first number is smaller than the second.

A_1.

#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2;
if ( v1 > v2 )
{
// 如果v1大于v2,就进行交换
int temp = v1;
v1 = v2;
v2 = temp;
} while ( v1 <= v2 )
{
std::cout << v1 << std::endl;
++v1;
} return ;
}

Exercises Section 1.5.1

Q_1. http://www.informit.com/title/032174113 contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output.

A_1.

#include <iostream>
#include "Sales_item.h" int main()
{
Sales_item book;
while ( std::cin >> book )
{
std::cout << book << std::endl;
} return ;
}

Q_2. Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.

A_2.

#include <iostream>
#include "Sales_item.h" int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2;
std::cout << item1 + item2 << std::endl; return ;
}

Q_3. Write a program that reads several transactions for the same ISBN. Write the sum of all the transactions that were read.

A_3.

#include <iostream>
#include "Sales_item.h" int main()
{
Sales_item sum, val; // 读取第一笔交易
std::cin >> sum; // 累加所有交易
while ( std::cin >> val )
{
sum = sum + val;
} std::cout << sum << std::endl; return ;
}

Exercises Section 1.5.2

Q_1. Write a program that reads several transactions and counts how many transactions occur for each ISBN.

A_1.

#include <iostream>
#include "Sales_item.h" int main()
{
Sales_item lastItem;
if ( std::cin >> lastItem )
{
int count = ;
Sales_item curItem;
while ( std::cin >> curItem )
{
if ( lastItem.isbn() == curItem.isbn() )
{
++count;
}
else
{
std::cout << lastItem.isbn() << " occurs " << count; lastItem = curItem;
count = ;
}
} std::cout << lastItem.isbn() << " occurs " << count;
} return ;
}

C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1的更多相关文章

  1. C++primer(第五版)读书笔记&习题解答---CHAPTER 3

    C++标准库类型包括:string,vector和迭代器,其中string是可变长的字符序列,vector存放的是某种给定类型对象的可变长序列,迭代器是string和vector的配套类型,常被用于访 ...

  2. C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 3

    Chapter 3.1 1. using声明具有如下的形式: using namespace::name; Chapter 3.2 1. C++标准一方面对库类型所提供的操作做了规定,另一方面也对库的 ...

  3. C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 2

    Chapter 2.1 1. 数据类型决定了程序中数据和操作的意义. 2. C++定义了一套基本数据类型,其中包括算术类型和一个名为void的特殊类型.算术类型包含了字符.整型.布尔值以及浮点数.vo ...

  4. Primer C++第五版 读书笔记(一)

    Primer C++第五版 读书笔记(一) (如有侵权请通知本人,将第一时间删文) 1.1-2.2 章节 关于C++变量初始化: 初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义 ...

  5. C++Primer第五版学习笔记

    <C++ Primer>Learning Note 程序实例下载地址:http://www.informit.com/title/0321714113 第一章            开始 ...

  6. 实验楼课程管理程序-深入学习《C++ Primer第五版》实验报告&学习笔记1

    本片博客为实验楼的训练营课程深入学习<C++ Primer第五版>的实验报告和学习笔记. 原课程地址为:https://www.shiyanlou.com/courses/405# 原文出 ...

  7. C++ 11 从C++ primer第五版的学习笔记

    1. auto (page107) auto 推断会忽略const   const int ci = i, & cr = ci; auto b = ci; // b is an int (to ...

  8. C++Primer第5版学习笔记(四)

    C++Primer第5版学习笔记(四) 第六章的重难点内容         你可以点击这里回顾第四/五章的内容       第六章是和函数有关的知识,函数就是命名了的代码块,可以处理不同的情况,本章内 ...

  9. C++Primer第5版学习笔记(三)

    C++Primer第5版学习笔记(三) 第四/五章的重难点内容           你可以点击这里回顾第三章内容       因为第五章的内容比较少,因此和第四章的笔记内容合并.       第四章是 ...

随机推荐

  1. 牛客练习赛16 C 任意点【并查集/DFS/建图模型】

    链接:https://www.nowcoder.com/acm/contest/84/C 来源:牛客网 题目描述 平面上有若干个点,从每个点出发,你可以往东南西北任意方向走,直到碰到另一个点,然后才可 ...

  2. 整数划分问题(记忆化搜索和DP方法)

    一. 问题 现在有一正整数N,要把它分为若干正整数之和,问有多少种本质不同的分法? (1)其中最大数不超过m, 有多少种分法? (2)分割后的正整数的数目不超过m个, 有多少种分法? (3)分成最大数 ...

  3. (转)NSString 类的使用

    官网连接地址:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/Creat ...

  4. 代理模式(Proxy)--动态代理(CGLIB)

    上一篇:代理模式(Proxy)--动态代理(jdk) (1)CGLIB技术是第三方代理技术,可以对任何类生成代理,代理的原则是对目标对象进行继承代理 (2)如果目标对象被final修饰,则无法被CGL ...

  5. 使用Bundle在Activity间传递数据

    使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...

  6. AFNetworking 2.0 Tutorial

    Update 1/18/2014: Fully updated for iOS 7 and AFNetworking 2.0 (original post by Scott Sherwood, upd ...

  7. MailKit---发送邮件

    一封最复杂的电子邮件的基本情况为:含有邮件正文和邮件附件,邮件正文可以同时使用HTML格式和普通文本格式表示,并且HTML格式的正文中又引用了其他的内嵌资源.对于这种最复杂的电子邮件,可以采用如图所示 ...

  8. [转]WCF RESTful service and WebGrid in ASP.NET MVC 5

    使用WebClient调用WCF服务 流程:从View获取实体类-->序列化-->写入内存流中-->传给远端的WCF服务 Get.POST.PUT.DELETE,客户端以流的方式调用 ...

  9. 解决中文环境下zabbix监控图形注释乱码

    zabbix监控的图形界面能够更直观的查看监控状态,当我们把zabbix的语言切换为中文的时候,会发现监控图形中一些中文参数会乱码,例如下面的效果 但是图形界面在原生的英文环境下完全没有乱码问题.为了 ...

  10. 甲骨文Java Archive

    甲骨文Java Archive 甲骨文Java Archive提供自助下载访问我们的一些历史的Java版本. 警告: 这些旧版本的JRE和JDK来帮助开发人员提供了在旧系统调试问题. 他们没有更新最新 ...