当前位置: 首页 > news >正文

网站建设找汉狮wordpress回到顶部

网站建设找汉狮,wordpress回到顶部,市场营销产品推广策划方案,wordpress jiaocheg目录 std::cout 格式化输出简介使用成员函数使用流操作算子 std::cout 格式化输出简介 C 通常使用cout输出数据#xff0c;和printf()函数相比#xff0c;cout实现格式化输出数据的方式更加多样化#xff1b; 一方面#xff0c;cout 作为 ostream 类的对象#xff0c;该类… 目录 std::cout 格式化输出简介使用成员函数使用流操作算子 std::cout 格式化输出简介 C 通常使用cout输出数据和printf()函数相比cout实现格式化输出数据的方式更加多样化 一方面cout 作为 ostream 类的对象该类中提供有一些成员方法可实现对输出数据的格式化另一方面为了方面用户格式化输出数据C 标准库专门提供了一个 iomanip 头文件该头文件中包含有大量的格式控制符也就是流操作算子使用上更加方便 cout 格式化输出有两种方法一种是直接使用成员函数另一种是使用流操作算子下面分别进行介绍: 使用成员函数 cout 的基类是 ostream 类ostream 类 中还包含一些可实现格式化输出的成员方法这些成员方法都是从 ios 基类以及 ios_base类中继承来的cout以及 cerr、clog也能调用: 成员函数说明flags(fmtfl)当前的格式全部替换为fmtflfmtfl 可以是一种格式也可以是多种格式precision(n)设置输出浮点数的精度为 nwidth(w)指定输出宽度为 w 个字符fill(c)在指定输出宽度的情况下输出宽度不足用 c 填充setf(fmtfl, mask)在当前格式的基础上追加 fmtfl 格式并且删除 mask 格式其中mask参数可以省略unsetf(mask)在当前格式的基础上删除 mask 格式 对于上面函数中的 flags 函数的fmtfl参数setf() 函数中的 fmtfl 参数以及 unsetf() 函数可以选择下面的值: 标志作用ios::boolapha把 true 和 false 输出为字符串ios::left输出数据在本域宽范围内向左对齐ios::right输出数据在本域宽范围内向右对齐ios::internal数值的符号位在域宽内左对齐数值右对齐中间由填充字符填充ios::dec设置整数的基数为 10ios::oct设置整数的基数为 8ios::hex设置整数的基数为 16ios::showbase强制输出整数的基数八进制数以 0 开头十六进制数以 0x 打头ios::showpoint强制输出浮点数的小点和尾数 0ios::uppercase在以科学记数法格式 E 和以十六进制输出字母时以大写表示ios::showpos对正数显示“”号ios::scientific浮点数以科学记数法格式输出ios::fixed浮点数以定点格式小数形式输出ios::unitbuf每次输出之后刷新所有的流 setf(fmtfl, mask)和 unsetf(fmtfl, mask) 中的mask 参数可以传递为下面的几个: 标志作用ios::adjustfieldios::left , ios::right , ios::internalios::basefieldios::dec ,ios::oct ios::hexios::floatfieldios::scientific , ios::fixed 使用 cout 成员函数的示例程序如下: bool testCoutUsingflags() {{// setf(fmt,mask) 在当前格式的基础上追加 fmt flag, 并且删除 mask 格式std::cout.setf(std::ios::boolalpha);bool a true;std::cout bool a: a std::endl;int m 100;std::cout.setf(std::ios::hex, std::ios::basefield);std::cout int m: m bool a a std::endl;// 去除相应的标志std::cout.unsetf(std::ios::boolalpha);std::cout hex print int m: m bool a a std::endl;std::cout.setf(std::ios::dec, std::ios::basefield);int n 0x110;std::cout dec print int n: n std::endl;std::cout.setf(std::ios::oct, std::ios::basefield);std::cout.setf(std::ios::showbase);std::cout oct print int n: n std::endl;}{std::cout.setf(std::ios::fixed, std::ios::floatfield);float f 1 / 3.0f;std::cout float f in fixed is: f std::endl;std::cout.precision(10);std::cout float f precision 10 in fixed is: f std::endl;std::cout.setf(std::ios::scientific, std::ios::floatfield);std::cout.precision(3);std::cout float f in scientific is: f std::endl;std::cout.setf(std::ios::uppercase);std::cout float f in scientific uppercase is: f std::endl;}{std::cout.width(50);std::cout.fill(_);std::cout.setf(std::ios::left, std::ios::adjustfield);std::cout this is my demo std::endl;std::cout.unsetf(std::ios::adjustfield);std::cout.width(50);std::cout.fill(_);std::cout.setf(std::ios::right, std::ios::adjustfield);std::cout this is my demo std::endl;}return true; }输出结果如下 bool a:true int m:64 bool a true hex print int m:64 bool a 1 dec print int n:272 oct print int n:0420 float f in fixed is:0.333333 float f precision 10 in fixed is:0.3333333433 float f in scientific is:3.333e-01 float f in scientific uppercase is:3.333E-01 this is my demo___________________________________ this is my demo this is my demo使用流操作算子 流操作算子实在iomanip中定义的要使用这些流操作算子必须包含此头文件: 注意下面的 *号表示默认使用的算子比如: 在默认情况下整数使用十进制形式输出 流操作算子作用*dec以十进制形式输出整数hex以十六进制形式输出整数oct以八进制形式输出整数fixed以小数形式输出浮点数scientific以科学计数法计数形式输出浮点数left左对齐宽度不足的时候填充字符到右边*right右对齐宽度不足的时候填充字符到左边setbase(b)设置输出整数的进制b8、10或者16setw(w)指定输出宽度为w个字符或者输入字符串读取w个字符setfill(c)指定输出宽度的情况下输出宽度不足用字符c填充(默认是使用空格填充)setprecision(n)输出浮点数的精度为 n, 就是小数点后应该暴露的位数boolaplha把 true 和 false 输出为字符串noboolalpha把true 和 false 输出为0,1showbase输出数值进制的前缀noshowbase不输出数值进制的前缀showpoint总是输出小数点*noshowpoint只有当小数部分存在时才显示小数点showpos在非负数中显示*noshowpos在非负数中不显示uppercase十六进制使用 A~E,设置显示前缀那么前缀输出为0X*nouppercase十六进制使用 a~e,设置显示前缀那么前缀输出为0x 使用流操作算子控制输出的综合程序如下 bool testCoutUsingStreamflags() {// stream operation flag will take effect all time util you set another flagbool a false;std::cout bool a: std::boolalpha a std::endl;std::cout bool a: std::noboolalpha a std::endl;// showbase and dec oct hexint m 80;std::cout dec int m: std::dec std::showbase m std::endl;std::cout hex int m: std::hex std::showbase m std::endl;std::cout oct int m: std::oct std::showbase m std::endl;// std::showpos 显示 -std::cout dec int m: std::dec std::showbase std::showpos m std::endl;// setw and setfillstd::cout std::left std::setw(50) std::setfill(_) hello world std::endl;std::cout std::right std::setw(50) std::setfill(_) hello world std::endl;std::cout std::internal std::setw(50) std::setfill(_) hello world m: m std::endl;std::cout std::noshowpos std::endl;int n 100;std::cout std::setbase(16) std::showbase int n: n std::endl;float f 1 / 3.0f;// 0.333333std::cout std::fixed f f std::endl;// 0.33333std::cout std::fixed std::setprecision(5) f f std::endl;// 3.33333e-1std::cout std::scientific std::setprecision(5) f f std::endl;// 3.33333E-1std::cout std::scientific std::uppercase std::setprecision(5) f f std::endl;// std::showpoint 强制输出浮点数的小点和尾数0 std::cout std::fixed std::showpoint std::setprecision(5) f f std::endl;{// print pointer 指针有做特殊的处理int* ptr new int(10);std::cout std::showbase std::hex ptr std::endl;std::cout std::showbase std::hex reinterpret_castintptr_t*(ptr) std::endl;delete ptr;}return true; }输出结果如下: basic test main enter basicCplusplusCout::testbasicCplusplusCoutImpl enter bool a:false bool a:0 dec int m:80 hex int m:0x50 oct int m:0120 dec int m:80 hello world_______________________________________ _______________________________________hello world ____________________________________hello world m:80int n:0x64 f 0.333333 f 0.33333 f 3.33333e-01 f 3.33333E-01 f 0.33333 000001E419046150 000001E419046150
http://www.hkea.cn/news/14450421/

相关文章:

  • 阆中 网站建设在线视频用什么网址
  • 个人网站毕业设计论文怎么做一网站首页
  • 网站弹出qq聊天窗口北京网站优化济南兴田德润简介电话
  • 辛集市住房和城乡建设局网站百度指数批量查询工具
  • 598网站建设wordpress获取地址栏参数
  • 建站 discuz网页设计与制作html代码
  • 房地产企业网站模板免费下载wordpress加相册
  • 做问卷网站wordpress英文变中文
  • 常州网站建设企业网站查询网站所有关键词排名
  • 北京网站设计网站公司荧光字网站
  • 湖北现代城市建设集团网站wordpress时光轴页面
  • 系统学做网站vi设计的基本要素
  • 平罗县住房和城乡建设局网站成都市四方建设工程监理有限公司网站
  • 怎么更改网站域名亚马逊没有网站怎么做seo
  • 海尔集团网站的网络营销是什么个人网页制作代码模板
  • 网站设计的网站合肥网络推广软件系统
  • 有什么网站可以接活做设计手机助手
  • 做爰在线观看网站微信网站系统
  • 坪山网站建设哪家效益快wordpress模板和下载不同
  • 抚顺外贸网站建设有做淘宝网站的
  • 纸牌网站建设招聘信息网站
  • 织梦网站首页在哪里改php小程序商城
  • 懒懒淘客怎么做自己的网站php网页制作源代码
  • 浙江电信关于网站备案信息核实的公告杭州哪里找网站建设的兼职
  • 做网站来钱快网站如何做微信支付宝支付
  • 哈尔滨网站建设培训学校营销团队找产品合作
  • 张家港做企业网站郑州网站建设十大公司
  • 如何查询网站备案进度百度竞价调价软件
  • 网站规划与开发石景山周边网站建设
  • 沈阳微网站建设在线做编程题的网站