Skip to content

Commit

Permalink
更新《C++程序设计原理与实践》笔记第11章
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZy979 committed Jun 6, 2024
1 parent 84788d3 commit eef7dfc
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions _posts/2023-01-25-ppp-note-ch11-customizing-input-and-output.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ ofstream logfile(name.str()); // e.g., open myfile17.log
通常情况下,我们用一个字符串来初始化`istringstream`,然后使用`>>`从字符串中读取字符。相反,通常用一个空字符串初始化`ostringstream`,然后用`<<`向其中填入字符,并使用`str()`获取结果。
注意:`ostringstream`构造函数和`str(s)`设置的初始字符串会被`<<`覆盖!例如:
```cpp
ostringstream oss("123456");
oss << "000";
cout << oss.str();
```

输出结果为 "000456" 而不是 "123456000" 。要避免这一问题,需要通过构造函数的第二个参数将模式指定为`app``ate`

## 11.5 面向行的输入
运算符`>>`按照对象类型的标准格式读取输入。例如,在读取一个`int`时,会读取到非数字字符为止;在读取一个`string`时,会跳过空白符,并读取到下一个空白符为止;在读取一个`char`时,会跳过空白符,并读取下一个非空白字符。例如:

Expand Down

0 comments on commit eef7dfc

Please sign in to comment.