cout.fill() 和setw()如何混用

sb_cs_dn 2011-10-02 04:10:18

char *p1 = L"printed in hexadecimal";
int len = strlen(p1);
int i =15;
int j = 1024;

cout << setw(len) << "default" << cout.fill(' ') << " : " << setw(3) << 3 << cout.fill('0') << endl;
cout << setw(len) << "printed in octal" << cout.fill(' ') << setw(3) << 3 << cout.fill('0') << endl;




结果乱了。

default0 : 3
printed in octal0 3


cout.fill() 和setw()如何混用
...全文
194 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sb_cs_dn 2011-10-07
  • 打赏
  • 举报
回复


default0 : 3
printed in octal0 3


多0了。且不对称。
星羽 2011-10-03
  • 打赏
  • 举报
回复
楼主要什么效果?
ljhhh0123 2011-10-02
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstring>

using namespace std;
int main(int argc, char* argv[])
{
const char *p1 = "printed in hexadecimal";
int len = strlen(p1);
int i =15;
int j = 1024;
cout.width(len);
cout << "default: ";
cout.setf(ios_base::hex,ios_base::basefield);//十六进制
cout.setf(ios_base::showbase);
cout << i << endl;

cout.width(len);
cout << "printed in octal " ;
cout << j << endl;

return 0;
}

结果:
             default: 0xf
printed in octal 0x400
asd605499941a 2011-10-02
  • 打赏
  • 举报
回复
cout<<setfill('x')<<setw(10)<<77<<endl;
5t4rk 2011-10-02
  • 打赏
  • 举报
回复
取消以前的设置

resetiosflags(ios_base::left)//使得第一次设置失效,因此第二次显示又默认向右对齐了。


详情参考

http://blog.sina.com.cn/s/blog_5dd76ecf0100ipb5.html
Linux-Torvalds 2011-10-02
  • 打赏
  • 举报
回复
cout.width()
cout.fill()
一般是这样子的。
飞天御剑流 2011-10-02
  • 打赏
  • 举报
回复
你想得到什么效果?
十八道胡同 2011-10-02
  • 打赏
  • 举报
回复
我记得set之后 你需要在set回来的,http://msdn.microsoft.com/zh-cn/library/8bbhbaew(VS.90).aspx

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧