【C++备忘录】输出流

/*
标准输出流对象cout
	cout.flush()
	cout.put()			//单字符
	cout.write()		//字符串,长度
	cout.width()		//输出宽度
	cout.fill()			//填充空位
	cout.setf(标记)		//
	*/

#include
using namespace std;
#include

void main81()
{
cout << “hello” << endl;
cout.put(‘h’).put(‘e’).put(‘l’);
char *p = “hello itcast”;
cout.write(p, strlen§) << endl;
cout.write(p, strlen§ - 4) << endl;
cout.write(p, strlen§ + 4) << endl;
system(“pause”);
return ;
}

void main82()
{
//使用类成员函数
cout << “”;
cout.width(30);
cout.fill(’*’);
cout.setf(ios::showbase); //#include //输出类型 "0x"16进制 "0"8进制
//cout.setf(ios::internal); //设置中间隔开
cout << hex << 123 << “\n”; //hex_16 dec_10 setbase(8)_8

cout << endl << endl;
//

//使用控制符
cout << “”
<< setw(30)
<< setfill(’*’)
<< setiosflags(ios::showbase) //基数
<< setiosflags(ios::internal)
<< hex
<< 123
<< “\n”
<< endl;

system(“pause”);

}

int main()
{
int a;
cout<<“input a:”;
cin>>a;
cout<<“dec:”<<dec<<a<<endl; //以十进制形式输出整数
cout<<“hex:”<<hex<<a<<endl; //以十六进制形式输出整数a
cout<<“oct:”<<setbase(8)<<a<<endl; //以八进制形式输出整数a
char pt=“China”; //pt指向字符串"China"
cout<<setw(10)<<pt<<endl; //指定域宽为,输出字符串
cout<<setfill(’
’)<<setw(10)<<pt<<endl; //指定域宽,输出字符串,空白处以’*'填充
double pi=22.0/7.0; //计算pi值
//按指数形式输出,8位小数
cout<<setiosflags(ios::scientific)<<setprecision(8);
cout<<“pi=”<<pi<<endl; //输出pi值
cout<<“pi=”<<setprecision(4)<<pi<<endl; //改为位小数
cout<<“pi=”<<setiosflags(ios::fixed)<<pi<<endl; //改为小数形式输出
system(“pause”);
return 0;
}

int main22( )
{
double a=123.456,b=3.14159,c=-3214.67;
cout<<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(2);
cout<<setw(10)<<a<<endl;
cout<<setw(10)<<b<<endl;
cout<<setw(10)<<c<<endl;
system(“pause”);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值