Skip to content

Commit caa3fa3

Browse files
committed
Updated to Chapter 12, Section 3
1 parent 46ca36a commit caa3fa3

File tree

12 files changed

+162
-72
lines changed

12 files changed

+162
-72
lines changed

.vscode/Test.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#include<iostream>
2-
#include<sstream>
3-
#include<iomanip>
1+
#include <iostream>
2+
#include <string>
3+
#include <sstream>
4+
#include <iomanip>
45
int main() {
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';
6+
std::string str {"abcdef"};
7+
std::ostringstream strio {str, std::ios_base::out | std::ios_base::trunc};
8+
// std::ostringstream strio {"abcdef", std::ios_base::app};
9+
strio.put('g');
10+
std::cout << str;
11+
// std::cout << strio.str();
1212
return 0;
1313
}

.vscode/Test.exe

46.6 KB
Binary file not shown.

code_in_book/13.1/output_info.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include<iostream>
2-
#include<sstream>
3-
#include<iomanip>
1+
#include <iostream>
2+
#include <sstream>
3+
#include <iomanip>
44
enum Sex : bool {male, female};
55
struct PersonalInfo {
66
const std::string name;

code_in_book/13.1/output_info.exe

-206 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <sstream>
4+
#include <iomanip>
5+
int main() {
6+
std::string input {".4+1.2\n3.14*2\n1/10\n5-3"}; //input中有我们预设的初值
7+
std::istringstream sin; //定义
8+
sin.str(input); //把input的内容赋值到sin
9+
std::ostringstream sout {}; //用空串来初始化ostringstream对象
10+
while (!sin.eof()) { //这里最好用!sin.eof();如果直接写sin,后文中还要作检测
11+
double num1, num2;
12+
char op;
13+
sin >> num1 >> op >> num2; //从sin底层字符串中直接获取输入内容
14+
switch (op) {
15+
case '+':
16+
sout << num1 + num2 << ' '; //把内容输出到sout底层字符串中
17+
break;
18+
case '-':
19+
sout << num1 - num2 << ' '; //同上
20+
break;
21+
case '*':
22+
sout << num1 * num2 << ' ';
23+
break;
24+
case '/':
25+
sout << num1 / num2 << ' ';
26+
break;
27+
}
28+
}
29+
std::cout << sout.str(); //把output中的内容输出到屏幕上
30+
return 0;
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
\section{字符串输入/输出\texttt{sstream}}
2+
\lstinline@std::basic_stringstream@ 继承自 \lstinline@std::basic_iostream@,表示字符串输入/输出。而 \lstinline@std::basic_istringstream@ 和 \lstinline@std::basic_ostingstream@ 分别继承自 \lstinline@std::basic_istream@ 和 \lstinline@std::basic_ostream@。我们在上一节中已经看到了它的一点影子,那就是用 \lstinline@std::istringstream@ 对象来产生一个新的输入流,再把 \lstinline@std::cin@ 重定向到它。\par
3+
这三个类都定义在 \lstinline@sstream@ 库中。它们最一般的用途是向字符串中输入/输出内容。程序把一段内容通过流输入到字符串中,这是``输出'';程序从一段内容中通过流读取信息,这是``输入'',不要搞混。我们只需始终记住:信息从程序中传到外界是输出,从外界传到程序中是输入。\par
4+
对于 \lstinline@std::stringstream@ 系列的类来说,它们都有内置的底层字符串,用以存储内容。我们可以用 \lstinline@str@ 成员函数来访问或修改它。
5+
\begin{lstlisting}
6+
std::basic_string<CharT, Traits, Allocator> str()const; //访问
7+
void str(const std::basic_string<CharT, Traits, Allocator> &s); //修改
8+
\end{lstlisting}\par
9+
注意这个访问函数重载,它的返回类型不是引用,所以我们不能靠它来修改数据,而是必须以提供参数的方式进行。\par
10+
下面是一个简单的使用字符串输入/输出的示例。我们先把待输入的内容存进一个 \lstinline@std::istringstream@ 对象(的底层字符串)中,再陆陆续续把输出内容输出到 \lstinline@std::ostringstream@ 对象(的底层字符串)中。但是我们不能直接看到这些输出内容,所以为了检验,我们还会用 \lstinline@std::cout@ 把 \lstinline@std::ostringstream@ 对象中的内容输出到屏幕中。\par
11+
\lstinputlisting[caption=\texttt{string\_stream\_calculator.cpp}]{code_in_book/13.2/string_stream_calculator.cpp}
12+
这段代码的运行结果是:\\\noindent\rule{\linewidth}{.2pt}\texttt{
13+
1.6 6.28 0.1 2
14+
}\\\noindent\rule{\linewidth}{.2pt}\par
15+
在这里我们对 \lstinline@sin@ 使用了 \lstinline@str@ 成员函数来修改内容,而对 \lstinline@sout@ 使用了 \lstinline@str@ 成员函数来访问内容。\par
16+
读者需要注意,\lstinline@sin@ 的底层字符串和 \lstinline@input@ 是两个事物,我们只是拿 \lstinline@str@ 成员函数把后者的值赋给前者而已,然后它们就没有任何关系了。
17+
\begin{lstlisting}
18+
std::string input {"12345"};
19+
std::istringstream sin {input};
20+
input = "abcde"; //改变input的值,不影响sin的底层字符串
21+
std::cout << sin.str(); //12345
22+
\end{lstlisting}
23+
这个与我们此前用的 \lstinline@rdbuf@ 重定向操作不同。
24+
\begin{lstlisting}
25+
std::istringstream sin {"12345"};
26+
std::cin.rdbuf(sin.rdbuf()); //std::cin重定向到sin的缓冲区
27+
std::cout << std::cin.get() << ' '; //49
28+
std::cout << sin.get() << ' '; //50
29+
std::cout << std::cin.get() << ' '; //51
30+
std::cout << sin.get() << ' '; //52
31+
\end{lstlisting}
32+
在这里,当我们进行重定向之后,\lstinline@std::cin@ 和 \lstinline@sin@ 就指向了同一个缓冲区,所以它们就会共用输入内容。\par
33+
字符串输入/输出的内容不多,本节只是做一个过渡,接下来就让我们看看文件输入/输出的相关知识。\par

main.aux

+25-23
Original file line numberDiff line numberDiff line change
@@ -251,19 +251,21 @@
251251
\@writefile{lof}{\contentsline {figure}{\numberline {13.5}{\ignorespaces 计算器程序中的输入处理}}{369}{figure.13.5}\protected@file@percent }
252252
\@writefile{lof}{\contentsline {figure}{\numberline {13.6}{\ignorespaces \lstinline @getline@ 与 \lstinline @get@ 成员函数处理分隔符的差别}}{371}{figure.13.6}\protected@file@percent }
253253
\@writefile{lol}{\contentsline {lstlisting}{\numberline {13.1}\texttt {output\_info.cpp}}{377}{lstlisting.13.1}\protected@file@percent }
254+
\@writefile{toc}{\contentsline {section}{\numberline {13.3}字符串输入/输出\texttt {sstream}}{378}{section.13.3}\protected@file@percent }
255+
\@writefile{lol}{\contentsline {lstlisting}{\numberline {13.2}\texttt {string\_stream\_calculator.cpp}}{379}{lstlisting.13.2}\protected@file@percent }
254256
\gdef \LT@i {\LT@entry
255257
{1}{42.00002pt}\LT@entry
256258
{1}{136.19997pt}\LT@entry
257259
{1}{117.00002pt}\LT@entry
258260
{1}{52.00002pt}\LT@entry
259261
{1}{86.48302pt}}
260-
\@writefile{toc}{\contentsline {part}{精讲篇}{381}{part*.262}\protected@file@percent }
261-
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 A\hspace {.3em}}C++运算符基本属性}{381}{appendix.A}\protected@file@percent }
262+
\@writefile{toc}{\contentsline {part}{精讲篇}{383}{part*.263}\protected@file@percent }
263+
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 A\hspace {.3em}}C++运算符基本属性}{383}{appendix.A}\protected@file@percent }
262264
\@writefile{lof}{\addvspace {10.0pt}}
263265
\@writefile{lot}{\addvspace {10.0pt}}
264-
\newlabel{ch:appendix_A}{{A}{381}{C++运算符基本属性}{appendix.A}{}}
265-
\@writefile{lot}{\contentsline {table}{\numberline {A.1}{截至C++17的所有运算符}}{381}{table.A.1}\protected@file@percent }
266-
\newlabel{tab:A-1}{{A.1}{381}{截至C++17的所有运算符}{table.A.1}{}}
266+
\newlabel{ch:appendix_A}{{A}{383}{C++运算符基本属性}{appendix.A}{}}
267+
\@writefile{lot}{\contentsline {table}{\numberline {A.1}{截至C++17的所有运算符}}{383}{table.A.1}\protected@file@percent }
268+
\newlabel{tab:A-1}{{A.1}{383}{截至C++17的所有运算符}{table.A.1}{}}
267269
\gdef \LT@ii {\LT@entry
268270
{1}{33.67001pt}\LT@entry
269271
{1}{33.81001pt}\LT@entry
@@ -275,11 +277,11 @@
275277
{1}{26.72002pt}\LT@entry
276278
{1}{27.283pt}\LT@entry
277279
{1}{86.44002pt}}
278-
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 B\hspace {.3em}}ASCII码表(0到127)}{383}{appendix.B}\protected@file@percent }
280+
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 B\hspace {.3em}}ASCII码表(0到127)}{385}{appendix.B}\protected@file@percent }
279281
\@writefile{lof}{\addvspace {10.0pt}}
280282
\@writefile{lot}{\addvspace {10.0pt}}
281-
\@writefile{lot}{\contentsline {table}{\numberline {B.1}{33个ASCII控制字符}}{383}{table.B.1}\protected@file@percent }
282-
\newlabel{tab:B-1}{{B.1}{383}{33个ASCII控制字符}{table.B.1}{}}
283+
\@writefile{lot}{\contentsline {table}{\numberline {B.1}{33个ASCII控制字符}}{385}{table.B.1}\protected@file@percent }
284+
\newlabel{tab:B-1}{{B.1}{385}{33个ASCII控制字符}{table.B.1}{}}
283285
\gdef \LT@iii {\LT@entry
284286
{1}{33.67001pt}\LT@entry
285287
{1}{33.81001pt}\LT@entry
@@ -293,20 +295,20 @@
293295
{1}{33.67001pt}\LT@entry
294296
{1}{33.81001pt}\LT@entry
295297
{1}{26.72002pt}}
296-
\@writefile{lot}{\contentsline {table}{\numberline {B.2}{95个ASCII可打印字符}}{384}{table.B.2}\protected@file@percent }
297-
\newlabel{tab:B-2}{{B.2}{384}{95个ASCII可打印字符}{table.B.2}{}}
298-
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 C\hspace {.3em}}相关数学知识}{385}{appendix.C}\protected@file@percent }
298+
\@writefile{lot}{\contentsline {table}{\numberline {B.2}{95个ASCII可打印字符}}{386}{table.B.2}\protected@file@percent }
299+
\newlabel{tab:B-2}{{B.2}{386}{95个ASCII可打印字符}{table.B.2}{}}
300+
\@writefile{toc}{\contentsline {chapter}{\numberline {附录 C\hspace {.3em}}相关数学知识}{387}{appendix.C}\protected@file@percent }
299301
\@writefile{lof}{\addvspace {10.0pt}}
300302
\@writefile{lot}{\addvspace {10.0pt}}
301-
\@writefile{toc}{\contentsline {section}{\numberline {C.1}数制转换}{385}{section.C.1}\protected@file@percent }
302-
\@writefile{lof}{\contentsline {figure}{\numberline {C.1}{\ignorespaces 一个简单的数字时钟}}{385}{figure.C.1}\protected@file@percent }
303-
\@writefile{lof}{\contentsline {figure}{\numberline {C.2}{\ignorespaces 从计数到乘方}}{386}{figure.C.2}\protected@file@percent }
304-
\@writefile{lof}{\contentsline {figure}{\numberline {C.3}{\ignorespaces 一个12进制乘法表}}{387}{figure.C.3}\protected@file@percent }
305-
\@writefile{lof}{\contentsline {figure}{\numberline {C.4}{\ignorespaces $(1a.c3)_{16}$的形式化表示}}{389}{figure.C.4}\protected@file@percent }
306-
\@writefile{lof}{\contentsline {figure}{\numberline {C.5}{\ignorespaces 通过短除法把十进制数转换成$R$进制}}{392}{figure.C.5}\protected@file@percent }
307-
\@writefile{lof}{\contentsline {figure}{\numberline {C.6}{\ignorespaces 二进制与八进制的转换}}{393}{figure.C.6}\protected@file@percent }
308-
\@writefile{lof}{\contentsline {figure}{\numberline {C.7}{\ignorespaces 二进制与十六进制的转换}}{394}{figure.C.7}\protected@file@percent }
309-
\@writefile{toc}{\contentsline {section}{\numberline {C.2}布尔代数基础}{394}{section.C.2}\protected@file@percent }
310-
\@writefile{lof}{\contentsline {figure}{\numberline {C.8}{\ignorespaces 玛雅数字与阿拉伯数字的对应关系}}{395}{figure.C.8}\protected@file@percent }
311-
\@writefile{toc}{\contentsline {chapter}{跋}{397}{appendix*.271}\protected@file@percent }
312-
\gdef \@abspage@last{406}
303+
\@writefile{toc}{\contentsline {section}{\numberline {C.1}数制转换}{387}{section.C.1}\protected@file@percent }
304+
\@writefile{lof}{\contentsline {figure}{\numberline {C.1}{\ignorespaces 一个简单的数字时钟}}{387}{figure.C.1}\protected@file@percent }
305+
\@writefile{lof}{\contentsline {figure}{\numberline {C.2}{\ignorespaces 从计数到乘方}}{388}{figure.C.2}\protected@file@percent }
306+
\@writefile{lof}{\contentsline {figure}{\numberline {C.3}{\ignorespaces 一个12进制乘法表}}{389}{figure.C.3}\protected@file@percent }
307+
\@writefile{lof}{\contentsline {figure}{\numberline {C.4}{\ignorespaces $(1a.c3)_{16}$的形式化表示}}{391}{figure.C.4}\protected@file@percent }
308+
\@writefile{lof}{\contentsline {figure}{\numberline {C.5}{\ignorespaces 通过短除法把十进制数转换成$R$进制}}{394}{figure.C.5}\protected@file@percent }
309+
\@writefile{lof}{\contentsline {figure}{\numberline {C.6}{\ignorespaces 二进制与八进制的转换}}{395}{figure.C.6}\protected@file@percent }
310+
\@writefile{lof}{\contentsline {figure}{\numberline {C.7}{\ignorespaces 二进制与十六进制的转换}}{396}{figure.C.7}\protected@file@percent }
311+
\@writefile{toc}{\contentsline {section}{\numberline {C.2}布尔代数基础}{396}{section.C.2}\protected@file@percent }
312+
\@writefile{lof}{\contentsline {figure}{\numberline {C.8}{\ignorespaces 玛雅数字与阿拉伯数字的对应关系}}{397}{figure.C.8}\protected@file@percent }
313+
\@writefile{toc}{\contentsline {chapter}{跋}{399}{appendix*.272}\protected@file@percent }
314+
\gdef \@abspage@last{408}

main.log

+44-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex 2023.12.3) 2 FEB 2024 22:23
1+
This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex 2023.12.3) 3 FEB 2024 09:42
22
entering extended mode
33
\write18 enabled.
44
file:line:error style messages enabled.
@@ -1986,9 +1986,24 @@ Overfull \hbox (61.46246pt too wide) in paragraph at lines 313--314
19861986
[]\TU/SimSun(0)/m/n/10 我 们 可 以 更 改 [][][] 作 用 下 的 内 容 对 齐 方 式:| [][][][][][][] 是 默 认 的 右 对 齐,| [][][][][][][]
19871987
[]
19881988

1989-
[377]) (./generalized_parts/../generalized_parts/13_input_output_streams_overview/03_sstream.tex) (./generalized_parts/../generalized_parts/13_input_output_streams_overview/04_fstream.tex))) (././intensive_part.tex [378] [379
1989+
[377]) (./generalized_parts/../generalized_parts/13_input_output_streams_overview/03_sstream.tex
1990+
Overfull \hbox (91.639pt too wide) in paragraph at lines 2--2
1991+
[][][][][][] \TU/SimSun(0)/m/n/10 继 承 自 [][][][][],| 表 示 字 符 串 输 入\TU/lmr/m/n/10 /\TU/SimSun(0)/m/n/10 输 出。| 而 [][][][][]
1992+
[]
1993+
1994+
[378]
1995+
Overfull \hbox (34.0146pt too wide) in paragraph at lines 10--10
1996+
[]\TU/SimSun(0)/m/n/10 下 面 是 一 个 简 单 的 使 用 字 符 串 输 入\TU/lmr/m/n/10 /\TU/SimSun(0)/m/n/10 输 出 的 示 例。| 我 们 先 把 待 输 入 的 内 容 存 进 一 个 [][][][][]
1997+
[]
19901998

1991-
] [380]) (./other_parts/appendix.tex (./other_parts/appendices/A.tex
1999+
2000+
Overfull \hbox (20.02336pt too wide) in paragraph at lines 10--10
2001+
\TU/SimSun(0)/m/n/10 中。| 但 是 我 们 不 能 直 接 看 到 这 些 输 出 内 容,| 所 以 为 了 检 验,| 我 们 还 会 用 [][][][][] 把 [][][][][]
2002+
[]
2003+
2004+
(./code_in_book/13.2/string_stream_calculator.cpp) [379]) (./generalized_parts/../generalized_parts/13_input_output_streams_overview/04_fstream.tex))) (././intensive_part.tex [380] [381
2005+
2006+
] [382]) (./other_parts/appendix.tex (./other_parts/appendices/A.tex
19922007
附录 A
19932008

19942009
Underfull \hbox (badness 6063) in paragraph at lines 16--16
@@ -2000,41 +2015,41 @@ Underfull \hbox (badness 7468) in paragraph at lines 46--46
20002015
[][][]\TU/SimSun(0)/m/n/8.33344 注 意,| 重 载 运 算 符 对 参 数 有 特 殊 要 求:| 必 须 接 收 至 少 一 个 自 定 义 类 型 的 参 数,| 可 以 是 类 或 枚 举 类。| 比 如 说,|
20012016
[]
20022017

2003-
[381
2018+
[383
20042019

2005-
]) (./other_parts/appendices/B.tex [382]
2020+
]) (./other_parts/appendices/B.tex [384]
20062021
附录 B
2007-
[383
2022+
[385
20082023

2009-
]) (./other_parts/appendices/C.tex [384]
2024+
]) (./other_parts/appendices/C.tex [386]
20102025
附录 C
20112026
File: appendices/../images/other_parts/C_digital_clock.png Graphic file (type bmp)
20122027
<appendices/../images/other_parts/C_digital_clock.png>
2013-
[385
2028+
[387
20142029

20152030
]
20162031
File: appendices/../images/other_parts/C_from_counting_to_exponentiation.png Graphic file (type bmp)
20172032
<appendices/../images/other_parts/C_from_counting_to_exponentiation.png>
20182033
File: appendices/../images/other_parts/C_duodecimal_multiplication_table.png Graphic file (type bmp)
20192034
<appendices/../images/other_parts/C_duodecimal_multiplication_table.png>
2020-
[386]
2035+
[388]
20212036
Underfull \vbox (badness 2753) has occurred while \output is active []
20222037

2023-
[387] [388]
2038+
[389] [390]
20242039
File: appendices/../images/other_parts/C_formatted_representation_of_a_number.png Graphic file (type bmp)
20252040
<appendices/../images/other_parts/C_formatted_representation_of_a_number.png>
2026-
[389] [390]
2041+
[391] [392]
20272042
File: appendices/../images/other_parts/C_short_division.png Graphic file (type bmp)
20282043
<appendices/../images/other_parts/C_short_division.png>
2029-
[391] [392]
2044+
[393] [394]
20302045
File: appendices/../images/other_parts/C_bin_to_oct.png Graphic file (type bmp)
20312046
<appendices/../images/other_parts/C_bin_to_oct.png>
2032-
[393]
2047+
[395]
20332048
File: appendices/../images/other_parts/C_bin_to_hex.png Graphic file (type bmp)
20342049
<appendices/../images/other_parts/C_bin_to_hex.png>
20352050
File: appendices/../images/other_parts/C_maya_numbers.png Graphic file (type bmp)
20362051
<appendices/../images/other_parts/C_maya_numbers.png>
2037-
[394])) (./other_parts/afterword.tex [395] [396
2052+
[396])) (./other_parts/afterword.tex [397] [398
20382053

20392054
])
20402055
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
@@ -2045,28 +2060,35 @@ File: images/other_parts/back_cover.pdf Graphic file (type pdf)
20452060
<use images/other_parts/back_cover.pdf, page 1>
20462061
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
20472062
<use images/other_parts/back_cover.pdf, page 1>
2048-
[397]
2063+
[399]
20492064
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
20502065
<use images/other_parts/back_cover.pdf, page 1>
20512066
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
20522067
<use images/other_parts/back_cover.pdf, page 1>
20532068
File: images/other_parts/back_cover.pdf Graphic file (type pdf)
20542069
<use images/other_parts/back_cover.pdf, page 1>
2055-
[398] (./main.aux)
2070+
[400] (./main.aux)
20562071
***********
20572072
LaTeX2e <2023-11-01>
20582073
L3 programming layer <2022/08/05>
20592074
***********
2060-
Package rerunfilecheck Info: File `main.out' has not changed.
2061-
(rerunfilecheck) Checksum: 691B0B1C26C27BA9BA830FF0B9AFA75B;9283.
2075+
2076+
2077+
Package rerunfilecheck Warning: File `main.out' has changed.
2078+
(rerunfilecheck) Rerun to get outlines right
2079+
(rerunfilecheck) or use package `bookmark'.
2080+
2081+
Package rerunfilecheck Info: Checksums for `main.out':
2082+
(rerunfilecheck) Before: C0C3077EFF7A555021F3FBF420D5FE95;9436
2083+
(rerunfilecheck) After: 428C59EA71D8D2EC51747775CB0B0844;9436.
20622084
)
20632085
Here is how much of TeX's memory you used:
2064-
28178 strings out of 474924
2065-
742731 string characters out of 5765037
2086+
28197 strings out of 474924
2087+
743336 string characters out of 5765037
20662088
2050189 words of memory out of 5000000
2067-
49045 multiletter control sequences out of 15000+600000
2089+
49058 multiletter control sequences out of 15000+600000
20682090
571845 words of font info for 168 fonts, out of 8000000 for 9000
20692091
1348 hyphenation exceptions out of 8191
20702092
93i,18n,111p,1239b,1918s stack positions out of 10000i,1000n,20000p,200000b,200000s
20712093

2072-
Output written on main.pdf (406 pages).
2094+
Output written on main.pdf (408 pages).

0 commit comments

Comments
 (0)