C++ stw与setfill

来源:互联网 发布:网页制作三剑客软件 编辑:程序博客网 时间:2024/06/05 19:03

注意问题:

所使用的头文件为iomanip.h

例如:
cout<<'s'<<setw(8)<<'a'<<endl;
则在屏幕显示
s        a 
//s与a之间有7个空格,setw()只对其后面紧跟的输出产生作用,如上例中,表示'a'共占8个位置,不足的用空格填充。若输入的内容超过setw()设置的长度,则按实际长度输出。
setw()默认填充的内容为空格,可以setfill()配合使用设置其他字符填充。

cout<<setfill('*')<<setw(5)<<'a'<<endl;
则输出:
****a //4个*和字符a共占5个位置。


ep:

// test_max.cpp : 定义控制台应用程序的入口点。#include "stdafx.h"#include <iostream>#include <iomanip>//setw(),setfill所在头文件using namespace std;class NUM{public:NUM(int i):nm(i){}void incr() const{nm++;}void decr() const{nm--;}public:mutable int nm;};int main(void){NUM a(0);string str="";for(int i=0;i<5;i++){a.incr();cout<<setfill('*')<<setw(a.nm)<<str.c_str()<<endl;}for(int i=0;i<5;i++){a.decr();cout<<setw(a.nm)<<str.c_str()<<setfill('*')<<endl;}system("pause");return 0;}


0 0
原创粉丝点击