ios_base类

举报
用户已注销 发表于 2021/11/19 02:38:34 2021/11/19
【摘要】 目录 一,_Fmtfl 格式 二,width 格式 三,precision 格式 四,sync_with_stdio函数 ios_base类是输入输出流的一个基础类。 The class ios_base is a multipurpose class that serves as the base c...

目录

一,_Fmtfl 格式

二,width 格式

三,precision 格式

四,sync_with_stdio函数


ios_base类是输入输出流的一个基础类。

The class ios_base is a multipurpose class that serves as the base class for all I/O stream classes.

一,_Fmtfl 格式

这个成员的每一位,都用来控制流的一个属性

初始化:_Fmtfl  = skipws | dec; 初始值是513

读取:flags()

替换:flags(fmtflags _Newfmtflags)

加格式:setf(fmtflags _Newfmtflags)

在给定的mask内设置格式:setf(fmtflags _Newfmtflags, fmtflags _Mask)

相关库函数


  
  1. _NODISCARD fmtflags __CLR_OR_THIS_CALL flags() const {
  2. return _Fmtfl;
  3. }
  4. fmtflags __CLR_OR_THIS_CALL flags(fmtflags _Newfmtflags) { // set format flags to argument
  5. const fmtflags _Oldfmtflags = _Fmtfl;
  6. _Fmtfl = _Newfmtflags & _Fmtmask;
  7. return _Oldfmtflags;
  8. }
  9. fmtflags __CLR_OR_THIS_CALL setf(fmtflags _Newfmtflags) { // merge in format flags argument
  10. const ios_base::fmtflags _Oldfmtflags = _Fmtfl;
  11. _Fmtfl |= _Newfmtflags & _Fmtmask;
  12. return _Oldfmtflags;
  13. }
  14. fmtflags __CLR_OR_THIS_CALL setf(
  15. fmtflags _Newfmtflags, fmtflags _Mask) { // merge in format flags argument under mask argument
  16. const ios_base::fmtflags _Oldfmtflags = _Fmtfl;
  17. _Fmtfl = (_Oldfmtflags & ~_Mask) | (_Newfmtflags & _Mask & _Fmtmask);
  18. return _Oldfmtflags;
  19. }
  20. void __CLR_OR_THIS_CALL unsetf(fmtflags _Mask) { // clear format flags under mask argument
  21. _Fmtfl &= ~_Mask;
  22. }

示例:


  
  1. int main()
  2. {
  3. cout << cout.flags() << endl;
  4. cout.setf(ios::unitbuf);
  5. cout << cout.flags() << endl;
  6. cout.unsetf(ios::unitbuf);
  7. cout << cout.flags() << endl;
  8. return 0;
  9. }

输出

513
515
513

其中ios::unitbuf是常数2

其他的 _Fmtfl 格式:

二,width 格式

width用来控制整数输出的宽度,即占几个字符

相关库函数


  
  1. _NODISCARD streamsize __CLR_OR_THIS_CALL width() const {
  2. return _Wide;
  3. }
  4. streamsize __CLR_OR_THIS_CALL width(streamsize _Newwidth) { // set width to argument
  5. const streamsize _Oldwidth = _Wide;
  6. _Wide = _Newwidth;
  7. return _Oldwidth;
  8. }

示例:


  
  1. int main()
  2. {
  3. for (int i = 0; i < 100; i++) {
  4. if (i % 10 == 0)cout << endl;
  5. cout.width(3);
  6. cout << i;
  7. }
  8. return 0;
  9. }

三,precision 格式

precision用来控制浮点数的输出精度

相关库函数


  
  1. _NODISCARD streamsize __CLR_OR_THIS_CALL precision() const {
  2. return _Prec;
  3. }
  4. streamsize __CLR_OR_THIS_CALL precision(streamsize _Newprecision) { // set precision to argument
  5. const streamsize _Oldprecision = _Prec;
  6. _Prec = _Newprecision;
  7. return _Oldprecision;
  8. }

示例:


  
  1. int main()
  2. {
  3. cout.precision(3);
  4. cout << 1.414213562;
  5. return 0;
  6. }

 输出:1.41

四,sync_with_stdio函数

Sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation.

相关库函数


  
  1. static bool __CLRCALL_OR_CDECL sync_with_stdio(
  2. bool _Newsync = true) { // set C/C++ synchronization flag from argument
  3. _BEGIN_LOCK(_LOCK_STREAM) // lock thread to ensure atomicity
  4. const bool _Oldsync = _Sync;
  5. _Sync = _Newsync;
  6. return _Oldsync;
  7. _END_LOCK()
  8. }

 其中_Sync是私有静态成员

__PURE_APPDOMAIN_GLOBAL static bool _Sync;

这表明所有的流共享一个_Sync成员。

文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/120775553

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。