C++ 流的使用 Part II - string stream 和 流的格式化输出
来源:互联网 发布:ubuntu kde plasma 5 编辑:程序博客网 时间:2024/04/30 22:03
字符串流的使用。
ios::showpos : 在数字正数前加 "+"。
ios::skipws : 输入流中忽略空格。
ios::showpoint : 显示浮点值的小数点。
测试结果:
格式化域 ios::basefield
ios::left : 使输出左对齐。
ios::right : 使输出右对齐。
ios::width() : 返回当前设置的宽度
ios::width(n) : 设置当前的宽度
ios::fill() : 返回当前设置填充字符
ios::fill(char) : 设置当前的填充字符
ios::precision() : 返回当前设置浮点数精度
ios::precision(n) : 设置当前的浮点数精度
继承图
示例代码:istringstream
#include <iostream>#include <string>#include <sstream>using namespace std;void main(){string strTest("12 45.67 ABCD");int i = 0;double d = 0;string str = "";istringstream s(strTest);s >> i >> d >> str ; // the same as atof() and atoi(), atof() atoi() with higher performancecout << "int = " << i << endl;cout << "double = " << d << endl;cout << "string = " << str << endl;cin >> i;}
测试结果:
示例代码:ostringstream
#include <iostream>#include <string>#include <sstream>using namespace std;void main(){int i = 123;double d = 4.56;string str = "ABCDE";ostringstream os;os << "int =" << i << endl;os << "double =" << d << endl;os << "string =" << str << endl;string result = os.str();cout << "Result : " << endl << result << endl;cin >> i;}
输出流的格式化
开关标志
ios::unitbuf : 单元缓冲区每次插入后立即刷新(马上进行输出)。
ios::showpos : 在数字正数前加 "+"。
ios::skipws : 输入流中忽略空格。
ios::showpoint : 显示浮点值的小数点。
代码示例 输出格式化数字:
#include <iostream>#include <string>#include <sstream>using namespace std;void main(){int i = 123;double d = 4.56;string str = "ABCDE";ostringstream os;os << "int =" << i << endl;os << "double =" << d << endl;os << "string =" << str << endl;string result = os.str();cout << "Result : " << endl << result << endl;cin >> i;}
测试结果:
格式化域 ios::basefield
ios::hex : 输出 16 进制
ios::dec : 输出 10 进制
ios::scientific : 输出科学计数法
ios::left : 使输出左对齐。
ios::right : 使输出右对齐。
ios::width() : 返回当前设置的宽度
ios::width(n) : 设置当前的宽度
ios::fill() : 返回当前设置填充字符
ios::fill(char) : 设置当前的填充字符
ios::precision() : 返回当前设置浮点数精度
ios::precision(n) : 设置当前的浮点数精度
代码示例 输出控制:
#include <iostream>#include <string>#include <iomanip>using namespace std;void main(){ int i = 100;double d = 123.456;string str = "ABCD";cout << "i = " << i <<endl;cout << "d = " << d <<endl;cout << "i (hex) = " << hex << i << endl;cout << "i (dec) = " << dec << i << endl;cout << "i (oct) = " << oct << i << endl;cout << "d (scientific) = " << scientific << d << endl;cout << "d (fixed) = " << fixed << setprecision(6) << d << endl; // total has 6 number after the pointcout.fill('#');cout.setf(ios::left);cout.width(10);cout<< i << " the fill chart is " << cout.fill() << endl;cout.fill('*');cout.setf(ios::right);cout.width(10);cout<< i << " the fill chart is " << cout.fill() << endl;cout.fill('-');cout.setf(ios::right);cout.width(10);cout<< str << " the fill chart is " << cout.fill() << endl;cin >> i;}
测试结果:
0 0
- C++ 流的使用 Part II - string stream 和 流的格式化输出
- 输出流的格式化
- string类型的格式化输出
- 使用String日期和时间的格式化
- C++ 输出流的格式化
- C++ 输出流的格式化
- C#WPF 格式化输出- IValueConverter接口的使用 Convert和ConvertBack
- python(5): string的格式化输出
- 对STL的string进行格式化输出
- 【C语言笔记】格式化的输入和输出
- 【stanford C++】字符串(String)与流(Stream)
- .net Stream MemoryStream 的使用注意事项 及 把流输出到客户端.
- 使用ios的成员函数进行格式化输入和输出
- Java 流(Stream)简介:1、基本的输入流和输出流
- c#byte[]和string,Image;Stream 和 文件之间的转换
- JAVA字符串格式化-String.format()和MessageFormat的使用
- hadoop stream指定输出的part文件个数,并利用mapreduce进行压缩
- c++string字符串的输出
- 黑马--银行业务调度系统
- 绍兴哪里可以报二级建造师以及预算员培训建造师培训
- epoll示例
- 生活需要运气:10招让幸运女神眷顾你!
- CRS USB Mode and Voice Prompts
- C++ 流的使用 Part II - string stream 和 流的格式化输出
- Android实现应用下载并自动安装apk包
- Windows编程基础 第五章 鼠标消息 定时器 菜单
- Android中HorizontalScrollView的使用
- google proto buffer
- 从头到尾彻底理解KMP
- 开源 免费 java CMS - FreeCMS2.1 会员站内信
- HDU 2035-人见人爱A^B(乘方取模)
- Java基础09天 学习笔记_面向对象(内部类,异常,RuntimeException, 自定义异常)