Skip to content

Commit 46ca36a

Browse files
committed
Updated to Chapter 13, Section 2
1 parent dba45be commit 46ca36a

File tree

173 files changed

+504
-13822
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+504
-13822
lines changed

.vscode/Test.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
#include<iostream>
2-
#include<exception>
3-
#include<stdexcept>
4-
#include<vector>
52
#include<sstream>
6-
#include<stdio.h>
3+
#include<iomanip>
74
int main() {
8-
std::istringstream sin {"25 1.3 Alan Turing"}; //需要包含sstream库
9-
std::cin.rdbuf(sin.rdbuf()); //重定向之后,cin的源就是sin中存的字符串
10-
int i;
11-
double d;
12-
char str[50];
13-
std::cin >> i >> d >> str;
14-
std::cout << i << '\n' << d << '\n' << str << '\n';
15-
std::cin >> str;
16-
std::cout << str << std::endl;
5+
std::cout.setf(std::ios_base::hex, std::ios_base::basefield); //十六进制输出
6+
std::cout.setf(std::ios_base::showbase); //显示前缀
7+
std::cout << std::setw(8) << 123 << '\n'; //默认右对齐
8+
std::cout.setf(std::ios_base::left, std::ios_base::adjustfield); //改为左对齐
9+
std::cout << std::setw(8) << 123 << '\n';
10+
std::cout.setf(std::ios_base::internal, std::ios_base::adjustfield); //两端对齐
11+
std::cout << std::setw(8) << 123 << '\n';
1712
return 0;
1813
}

.vscode/Test.exe

-50.5 KB
Binary file not shown.

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"valarray": "cpp",
6060
"xhash": "cpp",
6161
"xtree": "cpp",
62-
"string": "cpp"
62+
"string": "cpp",
63+
"iomanip": "cpp"
6364
}
6465
}

code_in_book/13.1/output_info.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<iostream>
2+
#include<sstream>
3+
#include<iomanip>
4+
enum Sex : bool {male, female};
5+
struct PersonalInfo {
6+
const std::string name;
7+
const Sex sex;
8+
unsigned age;
9+
double height;
10+
};
11+
std::ostream& operator<<(std::ostream &out, const Sex s) { //可以进行运算符重载
12+
return out << (s == male ? "M" : "F"); //<<的优先级高于?:所以要套括号
13+
}
14+
int main() {
15+
PersonalInfo persons[] {
16+
{ "Alice", female, 28, 165.1 },
17+
{ "Bob", male, 32, 185.45 },
18+
{ "Charlie", male, 45, 175.3 },
19+
{ "Diana", female, 37, 170.26 }
20+
};
21+
std::cout << std::setw(10) << "Name" << std::setw(5) << "Sex"
22+
<< std::setw(5) << "Age" << std::setw(9) << "Height\n";
23+
for (auto person : persons) {
24+
std::cout << std::setw(10) << person.name << std::setw(5) << person.sex
25+
<< std::setw(5) << person.age << std::setw(9) << person.height
26+
<< '\n';
27+
}
28+
return 0;
29+
}

code_in_book/13.1/output_info.exe

206 KB
Binary file not shown.

generalized_parts/01_welcome_to_cpp/01_start_with_a_cpp_program.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ \subsection*{语言标准的选择}
2323
C++语言标准的更新总会带来很多新的功能,也会对以往标准中的缺陷和漏洞加以弥补,所以越新的标准往往就越强大、易用。另一方面,新标准通常都能兼容旧标准\footnote{只是通常如此。有很多反例,比如C++17标准移除了古已有之的 \lstinline@std::auto_ptr@。},所以你也无需担心为了适应新标准而放弃很多旧知识,或者换用新标准后很多代码编译失败了。\par
2424
可惜并非所有编译器都能支持C++新标准的特性。因此,在编写本书时,考虑到许多编译器对语言标准支持的情况,本书选择使用C++17标准。\textbf{今后若无特殊说明,默认本书所有知识和代码遵循C++17标准。}\par
2525
\subsubsection*{Windows下运行的可能问题}
26-
在Windows系统中,很多IDE允许你在一个辅助窗口中运行程序(我本人比较习惯这种方式)。有些IDE在程序执行完毕后,会保留辅助窗口,等待用户``按任意键继续'',于是我们有足够的时间看到程序运行的结果;还有一些IDE在程序结束时会立刻关闭辅助窗口,我们就来不及看程序运行的结果。\par
26+
在Windows系统中,很多IDE允许你在一个辅助窗口中运行程序。有些IDE在程序执行完毕后,会保留辅助窗口,等待用户``按任意键继续'',于是我们有足够的时间看到程序运行的结果;还有一些IDE在程序结束时会立刻关闭辅助窗口,我们就来不及看程序运行的结果。\par
2727
为了解决这个问题,我们可能需要对代码做一些修改:
2828
\begin{lstlisting}
2929
//...前面的代码不变

generalized_parts/13_input_output_streams_overview/02_iostream.tex

Lines changed: 325 additions & 2 deletions
Large diffs are not rendered by default.
Binary file not shown.
9.27 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)