当前位置:网站首页>Cout format output common functions and flags summary
Cout format output common functions and flags summary
2022-07-25 06:06:00 【N-order magic cube】
surface 1 Stream member function and controller for controlling output format
| Stream member functions | Control characters with the same function | effect |
|---|---|---|
| precision(n) | setprecision(n) | Set the precision of the real number to n position |
| width(n) | setw(n) | Set the field width to n position |
| fill(c) | setfill(c) | Set fill symbol c |
| setf(ios::state) | setiosflags(ios::state) | Set output format status , The format status... Should be given in parentheses , Content and controller setiosflags The contents in brackets are the same , As shown in the table 2 Shown |
| unsetf(ios::state) | resetioflags(ios::state) | Terminate the set output format state , What should be specified in brackets |
surface 2 Set the format flag of the format state
| Format mark | effect |
|---|---|
| ios::left | The output data is aligned to the left in a wide range of the local area |
| ios::right | The output data is aligned to the right within a wide range of the domain |
| ios::internal | The sign bit of the value is left aligned within the field width , Align values right , The middle is filled with fill characters |
| ios::dec | Set the base of the integer to 10 |
| ios::oct | Set the base of the integer to 8 |
| ios::hex | Set the base of the integer to 16 |
| ios::showbase | Force the cardinality of the output integer ( Octal number 0 Lead , Hexadecimal number 0x Lead ) |
| ios::showpoint | Forces the output of the floating-point number of small points and mantissa 0 |
| ios::uppercase | In the form of scientific notation E And in hexadecimal output letters in uppercase |
| ios::showpos | A positive number shows “+” Number |
| ios::scientific | Floating point number scientific notation format output |
| ios::fixed | Fixed point format for floating point numbers ( Decimal form ) Output |
| ios::boolalpha | With “true”/“false” Output Boolean values in the form of |
| ios::unitbuf | Refresh all streams after each output |
| ios::stdio | Clear after each output stdout, stderr |
#include <iostream>
using namespace std;
int main(){
int a = 31;
cout.width(8);
cout.fill('*');
cout.unsetf(ios::dec);
cout.setf(ios::hex);
cout << a << endl;
}
Output is :
******1f
Use controllers to control the output format
We can also use the controller to control the output format , In this way, there is no need to call the member function of the flow , You can call the relevant control characters directly in the output input stream , We can use the following code to implement the same output format as above :
#include <iostream>
#include <iomanip> // Don't forget to include this header file
using namespace std;
int main(){
int a = 31;
cout << setw(8) << setfill('*') << resetiosflags(ios::dec) << setiosflags(ios::hex) << a << endl;
}
Be careful :
When using control characters, you need to include header files <iomanip>.
surface 2 Some of the format flags in are mutually exclusive ( such as dec、hex and oct You can only choose one ), If you want to change the setting to another mutually exclusive state , Member functions should be called unsetf( Corresponding to member function self) or resetiosflags( Corresponding to the control character setiosflags), Terminate the original set state , Then set other States . For example, the default decimal system was terminated in the previous example (ios::dec), Then set hexadecimal (ios::hex), If you don't terminate the original decimal mark , The output will still be in decimal .
Member functions in various functions width(n) And control characters setw(n) Only valid for the first subsequent output item , Other functions and flags are always valid for the output and input stream .
surface 2 The format flag in ios Class is defined as an enumeration value , Each format is marked with a bit Of 0 or 1 representative ( as follows enum _Ios_Fmtflags Shown ), So you can use bits or operators “|” Combine multiple format flags , In the stream member function setf()/unsetf() And the controller setioflags()/resetioflags() Simultaneous setting . such as :cout.setf(ios::dec | ios::showpos);
enum _Ios_Fmtflags
{
_S_boolalpha = 1L << 0,
_S_dec = 1L << 1,
_S_fixed = 1L << 2,
_S_hex = 1L << 3,
_S_internal = 1L << 4,
_S_left = 1L << 5,
_S_oct = 1L << 6,
_S_right = 1L << 7,
_S_scientific = 1L << 8,
_S_showbase = 1L << 9,
_S_showpoint = 1L << 10,
_S_showpos = 1L << 11,
_S_skipws = 1L << 12,
_S_unitbuf = 1L << 13,
_S_uppercase = 1L << 14,
_S_adjustfield = _S_left | _S_right | _S_internal,
_S_basefield = _S_dec | _S_oct | _S_hex,
_S_floatfield = _S_scientific | _S_fixed,
_S_ios_fmtflags_end = 1L << 16,
_S_ios_fmtflags_max = __INT_MAX__,
_S_ios_fmtflags_min = ~__INT_MAX__
};
Format flags simplify control output formats
In the above two methods , To set the format flag bit, you need to call the function (setf and setiosflags), More trouble , Fortunately C++ There is also a set of functions about format flags , It can be used directly in input and output streams , For example, to implement the previous output format, we can write :
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int a = 31;
cout << setw(8) << setfill('*') << hex << a << endl;
}
above hex It's a function like this , except hex, surface 2 All format flags in are set in this way , If you want to terminate, just add “no” Can , such as showpos and noshowpos( surface 2 None of the first six items no edition ).
In addition, it can be noted that , Here we use hex When setting hexadecimal, you don't have to terminate the decimal system as before , Why is that ? First look at it. hex Function definition of :
inline ios_base&
hex(ios_base& __base)
{
__base.setf(ios_base::hex, ios_base::basefield);
return __base;
}
You can see hex It's actually called setf function , But here it is. setf As we used above setf Functions are different , This is overloaded with two formal parameters setf function , Its definition is as follows :
fmtflags
setf(fmtflags __fmtfl, fmtflags __mask)
{
fmtflags __old = _M_flags;
_M_flags &= ~__mask;
_M_flags |= (__fmtfl & __mask);
return __old;
}
You can see here setf First the mask The corresponding bits are set 0, Then the position to be set 1. Specific to the hex function , It is to _S_basefield(_S_dec | _S_oct | _S_hex) All first 0, then _S_hex Location 1. With this setf function , We are using hex There is no need to terminate first dec 了 , You can use it directly hex.
边栏推荐
- 阻塞队列分析
- Unity Animator动画与状态机
- 暑期总结2
- Use abp Zero builds a third-party login module (4): wechat applet development
- Sword finger offer 32 - I. print binary tree from top to bottom
- UML modeling tools Visio, rational rose, powerdesign
- [ultra detailed diagram] FPN + mask RCNN
- A little experience about von Mises distribution
- R language ggpubr package ggarrange function combines multiple images and annotates_ Figure add annotation, annotation, annotation information for the combined image, and use the right parameter to ad
- G1 garbage collector
猜你喜欢

ECS is exclusive to old users, and the new purchase of the remaining 10 instances is as low as 3.6% off

In depth analysis: is the hottest business model in 2022 linked by 2+1 a legal model?
![[daily practice] day (14)](/img/83/d924fa0fc5ae01d151a0880da62f7e.png)
[daily practice] day (14)

基于ISO13209(OTX)实现EOL下线序列

Idea commonly used 10 shortcut keys
![(15) [driver development] over written copy](/img/1c/68dfff5671add1fe91567e4df34135.png)
(15) [driver development] over written copy

(2022牛客多校)D-Link with Game Glitch(spfa)
![(14) [driver development] configuration environment vs2019 + wdk10 write XP driver](/img/90/0d94d26be8128d77de65919763fda5.png)
(14) [driver development] configuration environment vs2019 + wdk10 write XP driver

HTB-Beep

node.express中req.body总是undefind解决
随机推荐
(15) [driver development] over written copy
Calculate BDP value and wnd value
2021 ICPC Shaanxi warm up match b.code (bit operation)
Prometheus operator configures promethesrule alarm rules
mysql数据库备份和恢复
Leetcode/ number of 1 in the first n digit binary
Y76. Chapter IV Prometheus large factory monitoring system and practice -- Prometheus advanced (VII)
[typescript manual]
PHP warehouse inventory management system source code WMS source code
(牛客多校二)J-Link with Arithmetic Progression(最小二乘法/三分)
Vim配置Golang开发环境
VO, dto, do, Po distinction and use
context must be a dict rather解决
MySQL中建表时 pk、nn、qu、b、un、zf、ai、g代表的意思
HTB-Granpa
Sword finger offer 54. the k-th node of the binary search tree
对于von Mises distribution(冯·米塞斯分布)的一点心得
HTB-Devel
R language uses data.table function to create data.table data (use: operator to create continuous numeric vector)
Qt 5界面修改无效的问题解决QtDesigner修改之后无效的解决办法