输入输出流简介

标准输入输出流

标准输入流

cin>>x
-读入整形数时,以第一个非数字为终结
-读入字符串时以第一个空格、tab或换行符为终结

cin.getline(str,len,ch)//读入一个字符串
						//ch被从流中提出,但不存入str
ch = cin.get();         //读入一个单独的字符
cin.ingore(len,ch);     //忽略一串字符,ch同上
int x;
while(cin>>x){
	...
}
return 0;
//键盘读入时用ctrl—z结束,文件读入时读到文件末尾

标准输出流

输出作用
cout输出到标准设备
cerr输出错误信息
clog输出错误日志
  • cout<<y;
//输出一个字符,put可以连用
cout.put('A').put('a');

流操纵算子

整数型

//整数型
int n=10;
cout<<n<<endl;
cout<<hex<<n<<endl //十六进制
	<<dec<<n<<endl //十进制
	<<oct<<n<<endl;//八进制
/*
输出结果为:
10
a
10
12

*/

浮点数

#include<iostream>
#include<iomanip>
using namespace std;

int main(){
	double x=1234567.89,y=12.34567;
	int n=1234567;
	int m=12;
	//输出为6位小数,int型不受影响
	cout<<setprecision(6)<<x<<endl
		<<y<<endl<<n<<endl<<m;
	return 0;
}
/*输出为:
1.23457e+006
12.3457
1234567
12
#include<iostream>
#include<iomanip>
using namespace std;

int main(){
	double x=1234567.89,y=12.34567;
	int n=1234567;
	int m=12;
	//要求必须输出成小数的形式
	cout<<setiosflags(ios::fixed)
		<<setprecision(6)<<x<<endl
		<<y<<endl<<n<<endl<<m;
	return 0;
}
/*输出为(保留6位小数):
1234567.890000
12.345670
1234567
12

设置域宽

#include<iostream>
#include<iomanip>
using namespace std;

int main(){
	char string[100];
	cin.width(5);
	cin>>string;
	cout<<string<<endl;
	cin>>string;
	cout<<string<<endl;
	return 0;
}
/*输入为:1234567890
输出为:
1234
567890
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值