C++ cout的使用总结

cout 是C++中 ostream 类型的对象

cout 是C++中 ostream 类型的对象,该类被封装在 < iostream > 库中,该库定义的名字都在命名空间 std 中,所以 cout 全称是 std::cout 。

1、cout支持多种数据类型,如int、float、double、char、string等,它们都会被自动转换成字符串进行输出。

#include<bits/stdc++.h>
using namespace std;
int main(){
	int a = 1;
	bool b = true;
	double d = 1.2;
	char c = 'c';
	cout<<"a = "<<a<<endl<<"b = "<<b<<endl<<"c = "<<c<<endl<<"d = "<<d<<endl;
	return 0;
}

2、cout输出的内容可以直接与文件流对象相连,实现将输出内容写入到文件中。例如:

std::cout<<std::endl;将刷新输出缓冲区,确保输出内容被立即显示。

3、避免输出时出现中文乱码问题

为了避免输出时出现中文乱码问题,可以使用std::wcout来输出宽字符类型的字符串。同时,需要在编译选项中加上-finput-charset=UTF-8来指定输入文件的编码格式。

1、cout以不同进制来输出数字,默认是10进制,其他进制输出方法如下:

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
	int i = 90;
	cout << i << endl;
	cout << dec << i << endl; //dec 是十进制(效果和默认一样)
	cout << oct << i << endl; //oct 是八进制输出
	cout << hex << i << endl; //hex 是十六进制输出(字母默认是小写字母)
	cout << setiosflags(ios::uppercase);//setiosflags(ios::uppercase) 表示将字母大写输出,包含在库 < iomanip > 中。
	cout << hex << i << endl; //hex 是十六进制输出(大写字母)
	cout << setbase(8) << i << endl;//setbase(n) 表示以 n 进制显示,包含在库 < iomanip > 中,n 只能取 8, 10, 16 三个值。
}

2、输出数字位数的控制

保留几位小数需要使用的头文件是#include <iomanip>
cout<<fixed<<setprecision(2)<<1.23456<<endl;
示例代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
	cout<<fixed<<setprecision(2)<<1.23456<<endl;
	cout<<setprecision(2)<<fixed<<1.23456<<endl;
}

fixed和setprecision()先后顺序不影响输出结果

C++默认浮点数输出有效位数是 6 位(若前面整数位数大于 6 位,使用科学计数法输出),而通过以下几种方式可以更改输出精度:
1.使用 setprecision(n) 即可设置浮点数输出的有效位数
(若前面整数位数大于 n 位,使用科学计数法输出)
2.使用 setiosflags(ios::fixed) 或 fixed,表示对小数点后面数字的输出精度进行控制
所以,和 setprecision(n) 结合使用即可设置浮点数小数点后面数字的输出精度,位数不足的补零
以上均采用 “四舍五入” 的方法控制精度,三个控制符均包含在 std 命名空间中。
示例代码:

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
	double i = 123.456789;
	cout << i << endl;
	cout << setprecision(3) << i << endl;
	cout << setprecision(9) << i << endl;
	cout << setiosflags(ios::fixed);
	cout << i << endl;
	cout << fixed << setprecision(3) << i << endl;
	cout << setprecision(9) << fixed <<  i << endl;
}

3、显示小数点和正负号showpointshowpos

示例代码如下:

#include<bits/stdc++.h>
using namespace std;
int main(){
	double a=5,b=-1.2;
	cout<<a<<"\t\t"<<b<<endl;
	cout<<showpoint<<a<<"\t\t"<<b<<endl;
	cout<<showpos<<a<<"\t"<<b<<endl;
}

另外一种写法:

#include<bits/stdc++.h>
using namespace std;
int main(){
	double a=5,b=-1.2;
	cout<<a<<"\t\t"<<b<<endl;
	cout<<setiosflags(ios::showpoint);
	cout<<a<<"\t\t"<<b<<endl;
	cout<<setiosflags(ios::showpos);
	cout<<a<<"\t"<<b<<endl;
}

输出结果

4、设置域宽和对齐方式setw()leftright

输出默认右对齐

#include<bits/stdc++.h>
using namespace std;
int main(){
	int i = 10;
	//设置域宽为3 
	cout<<setw(3)<<i<<endl;
	//设置域宽为3,左对齐
	cout<<setw(3)<<left<<i<<endl;
	//设置域宽为3,右对齐 
	cout<<setw(3)<<right<<i<<endl;
}

输出结果:

其他方式:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int i = 10;
	//设置域宽为3 
	cout<<setw(3)<<i<<endl;
	//设置域宽为3,左对齐
	cout<<setiosflags(ios::left);
	cout<<setw(3)<<i<<endl;
	//设置域宽为3,右对齐 
	cout<<setiosflags(ios::right);
	cout<<setw(3)<<i<<endl;
}

5、设置填充字符setfill()

示例代码:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int hour = 14,minute = 18,second = 10;
	cout<<setw(2)<<setfill('0')<<hour<<":"<<minute<<":"<<second;
	return 0;
}

运行效果:

控制符 作用
setbase(n) 以n进制方式输出(n=8,10,16)
setfill(ch) 设置字符填充,ch可以是字符常量或字符变量
setprecision(n) 设置输出有效位数为n位
setw(n) 设置字符宽度为n位,只对后一个有影响
setiosflags(ios::uppercase) 以大写字母显示
setiosflags(ios::fixed) 实现对小数点后的数字的控制
setiosflags(ios::scientific) 以科学计数法显示
setiosflags(ios::showpoint) 强制显示小数点
setiosflags(ios::showpos) 强制显示正号
setiosflags(ios::left) 设置输出左对齐
setiosflags(ios::right) 设置输出右对齐
resetiosflags(…) 终止括号中的输出格式
posted @ 2024-03-13 14:20  -Corlin-  阅读(1333)  评论(0编辑  收藏  举报