首页 > 编程知识 正文

string头文件的作用,c++头文件

时间:2023-05-05 08:14:28 阅读:172954 作者:4266

所有iomanip都称为IO Manipulators,表示输入和输出操纵器。 iomanip头文件总共包含400多行代码,这使得输入/输出格式转换更加方便。 但是在编写代码解决问题的过程中,朴素的人总结了它的一般用途。

其作用

1.十进制向其他进制的转化

这里只举了十六进制的例子,要变换成其他的进制只需更换hex即可

# include ' iostream ' # include ' iomanip ' usingnamespacestd; 使用int main ()//cout hex和setiosflags (IOs :3360 show base|IOs 33603360 upper case ) cout '进行couthex和setiosflags ) IOs 3333://表示十六进制coutsetiosflags (IOs :3360 show base|IOs 33603360 upper case ); //需要前缀,将cout 100 endl大写; 返回0; }执行结果:

当然,如果不希望超前前缀,可以:

# include ' iostream ' # include ' iomanip ' usingnamespacestd; 使用intmain{cout}resetiosflags设置SETI oflags ' endl; coutresetiosflags (IOs :3360 show base ); cout 100 endl; 返回0; }或直接暴力转换十六进制。

# include ' iostream ' # include ' iomanip ' usingnamespacestd; int main () { cout是hex:' endl; cout hex; cout 100 endl; 返回0; }执行结果:

最后,我将介绍我认为最方便的进制转换方法

# include ' iostream ' # include ' iomanip ' usingnamespacestd; 实现int main ()//十六进制的另一种方法(cout )是使用setbase )设置十六进制(十六进制) endl。 coutsetbase(16; cout 100 endl; cout '使用setbase(8)生成八进制(endl; coutsetbase(8); cout 100 endl; 返回0; }执行结果:

(温馨提示:经过我多次实验,setbase(x )的x只有8或16,其他数字只能用十进制计算) ) ) ) ) )。

2.实现头部字符填充

# include ' iostream ' # include ' iomanip ' usingnamespacestd; intmain((cout )下的两行作为比较setfill和setw的效果范围: ) endl。 coutsetfill('* ' ) setw(8) ) 8; cout 100 endl; cout 100 endl; 返回0; }执行结果:

(值得注意的是,第二次输出的100是没有挤字待遇的)

3.规定小数点位数

1) 第一种方式

# include ' iostream ' # include ' iomanip ' usingnamespacestd; int main () { cout下两种行为为setprecision效应,效应为3和9 )9' endl; double pi=3.1415926; coutsetprecision(3; cout pi endl; coutsetprecision(9); cout pi endl; 返回0; }执行结果:

这样,指定字符的长度并不计算小数点所占的位数。 此外,需要注意的是,pi的长度为8位,没有小数点,因此在第二次输出pi时保持8位输出。 这种输出方式具有一定程度的灵活性。

2) 第二种方式

在限定小数点后位数的过程中,我们希望他能够按照我们的意愿输出,而不是让程序输出得有多聪明。 例如,在oj平台上提交代码时,可以使用以下方法之一,因为AC通常具有严格的数据规范。 # include ' iostream ' # include ' iomanip ' usingnamespacestd; int main () { double pi=3.1415926; cout '以下两种行为setprecision和fixed' endl; cout fixed; coutsetprecision(3); cout pi endl; coutsetprecision(9); cout pi endl; 返回0; }执行结果:

实际上,比上面多一个fixed。 此时,setprecision(x )规定的x为输出数字的小数点以下的位数。 当然如果你觉得这个方法麻烦,也可以用传统的printf('%.3f ',m )。 进行数据格式的规格。

当然,这篇文章也就此告一段落。 原创并不容易,请在得到评价后再进行。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。