当前位置:网站首页>iomanip头文件在实战中的作用
iomanip头文件在实战中的作用
2022-06-25 22:06:00 【彼岸的饭碗】
iomanip全称为IO Manipulators,意思是输入输出操纵器。iomanip头文件中一共有400余行代码,极大的题高了输入输出格式变换的便捷程度。但是在写代码解决的问题的过程中,鄙人总结了它一些常见用途。
它的作用
1.十进制向其他进制的转化
这里我们仅仅举16进制的例子,如果要转化为其他进制只需要将hex更换即可
#include "iostream"
#include "iomanip"
using namespace std;
int main(){
//使用cout << hex 和 setiosflags(ios::showbase|ios::uppercase)
cout << "使用cout << hex 和 setiosflags(ios::showbase|ios::uppercase)" <<endl;
cout << hex; //代表十六进制
cout << setiosflags(ios::showbase | ios::uppercase); //需要前缀并且要大写
cout << 100 <<endl;
return 0;
}
执行结果:
当然如果不想要进制前缀的话,也可以这样:
#include "iostream"
#include "iomanip"
using namespace std;
int main(){
cout << "使用resetiosflags重设setioflags的设置" <<endl;
cout <<resetiosflags(ios :: showbase | ios :: uppercase);
cout << 100 <<endl;
return 0;
}
或者直接暴力转换16进制。
#include "iostream"
#include "iomanip"
using namespace std;
int main(){
cout << "使用hex:" <<endl;
cout << hex;
cout << 100 <<endl;
return 0;
}
执行结果:最后隆重介绍一下我认为最便捷的一种进制转化方法
#include "iostream"
#include "iomanip"
using namespace std;
int main(){
//另一种实现十六进制的方式
cout << "使用setbase(16)来设置十六进制" <<endl;
cout << setbase(16);
cout << 100 <<endl;
cout << "使用setbase(8)来设置八进制" <<endl;
cout << setbase(8);
cout << 100 <<endl;
return 0;
}
执行结果:
(温馨提示:经过我的多次实验,setbase(x)中的x只能为8或者16,若是其他数字只能按照十进制来计算)
2.实现头部字符填充
#include "iostream"
#include "iomanip"
using namespace std;
int main(){
cout << "下面两行作为对比setfill 和 setw的效果范围:" << endl;
cout << setfill('*') << setw(8);
cout <<100 << endl;;
cout << 100 << endl;
return 0;
}
执行结果:(值得注意的是,第二次输出的100却没有字符填充的待遇)
3.规定小数点位数
1) 第一种方式
#include "iostream"
#include "iomanip"
using namespace std;
int main(){
cout << "下面两行为setprecision的效果,效果为3和9" <<endl;
double pi=3.1415926;
cout <<setprecision(3);
cout << pi <<endl;
cout <<setprecision(9);
cout << pi << endl;
return 0;
}
执行结果:
可见,所规定的字符长度并不计算小数点所占的位数,并且值得注意的是,由于pi的长度(不带小数点)只有8位,因此第二次输出pi的时候仍旧输出8位,可见这一种输出方式具有着一定的灵活性(比较聪明)。
2) 第二种方式
在对小数位数进行限定的过程中,我们有时候并不希望输出程序有多聪明,而是希望他能按照我们的意思去进行输出。比如,我们在oj平台中提交代码的时候,能够AC的往往是具有着严格数据规范的,因此我们可以使用以下的一种方式。
#include "iostream"
#include "iomanip"
using namespace std;
int main(){
double pi=3.1415926;
cout << "下面两行为setprecision加上fixed" <<endl;
cout << fixed;
cout << setprecision(3);
cout << pi <<endl;
cout << setprecision(9);
cout << pi << endl;
return 0;
}
执行结果:
其实,也就是比上面多一个fixed罢了,这时候setprecision(x)规定的x便是输出数字的小数点后面的位数。当然如果觉得这种方法繁琐,也可以使用传统的printf("%.3f",m); 来进行数据格式的规范。
当然本篇文章到此也就告一段落,希望大家多多补充,原创不易,点个赞再走呗。
边栏推荐
猜你喜欢
QT custom implemented calendar control
UE4 learning record 2 adding skeleton, skin and motion animation to characters
Bi-sql stored procedure (I)
Online customer service - charging standards and service provision of third parties
CSDN add on page Jump and off page specified paragraph jump
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
SSL/TLS、对称加密和非对称加密和TLSv1.3
平衡二叉树AVL
Hibernate core api/ configuration file / L1 cache details
期末复习【机器学习】
随机推荐
The package name of the manifest file in the library project and the app project are not the same
Solving typeerror: Unicode objects must be encoded before hashing
C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
Qtcreator formatting code
关于Swoole协程容器
Apache Doris1.0版本集群搭建、负载均衡与参数调优
xtrabackup的备份还原
CSDN add on page Jump and off page specified paragraph jump
Kylin
库项目和App项目中清单文件的包名不要相同
先序线索二叉树
第五章 习题(124、678、15、19、22)【微机原理】【习题】
Hbuilderx uses the gaude map to obtain the current location
C. Fibonacci Words-April Fools Day Contest 2021
Architecture part -- the use of UMI framework and DVA
C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
LeetCode-1528-重新排列字符串-哈希表-字符串
excel如何实现中文单词自动翻译成英文?这个公式教你了
BI-SQL丨存储过程(一)
中序线索二叉树