C + + formatted output, C + + output format control

Source: Internet
Author: User
Tags setf

C + + formatted output, C + + output format control in the output data, for the sake of simplicity, often do not specify the format of the output, by the system according to the type of data to take the default format, but sometimes you want the data to output in the specified format, such as the requirement to output an integer in 16 or eight binary form, Only two decimal places are reserved for the small number of outputs. There are two ways to achieve this. One is the method of using the controls that we have already described (see: C + + input cout and output cin), and the 2nd type is to use the relevant member functions of the Stream object. Separately described below. Use controls to control the format of output format control it is not mentioned here, just for illustrative purposes, see: C + + input cout and output cin.

[Example 13.2] control the output format with a control character.
  1. #include <iostream>
  2. #include <iomanip>//Don't forget to include this header file
  3. Using namespace std;
  4. int main()
  5. {
  6. int a;
  7. cout<<"input A:";
  8. CIN>>a;
  9. cout<<"Dec:"<<dec<<a<<endl; //output integers in decimal form
  10. cout<<"hex:"<<<a<<endl; //Output integer A in 16 binary form
  11. cout<<"Oct:"<<setbase(8) <<a<<endl; //Output integer A in eight binary form
  12. char *pt="China"; //pt pointing to the string "China"
  13. cout<<setw(ten) <<pt<<endl; //Specify field width as, output string
  14. cout<<setfill(' * ') <<setw(ten) <<pt<<endl; //Specify field width, output string, blank to ' * ' Fill
  15. double Pi=22.0/7.0; //Calculate PI value
  16. //Output as exponential, 8 decimal places
  17. cout<<setiosflags(iOS:: Scientific) <<setprecision(8);
  18. cout<<"pi="<<pi<<endl; //Output PI value
  19. cout<<"pi="<<setprecision(4) <<pi<<endl; //change to decimal place
  20. cout<<"pi="<<setiosflags(iOS:: fixed) <<pi<<endl ; //change to decimal form output
  21. return 0;
  22. }
The results of the operation are as follows:
Input a:34 (Enter the value of a)
dec:34 (decimal form)
HEX:22 (16 in binary form)
Oct:42 (in octal form)
China (field width is)
China (field width is, blank is filled with ' * ')
pi=3.14285714e+00 (exponential output, 8 decimal places)
pi=3.1429e+00 (exponential output, 4 decimal places)
pi=3.143 (fractional output, precision still) control the output format with the member function of the stream object in addition to controlling the output format with a control, you can control the output format by calling member functions in the Stream object cout that control the output format. The commonly used member functions for controlling output formats are shown in table 13.4.

Table 13.4 Flow member functions for output format control
Stream member functions control with the same function function
Precision (N) Setprecision (N) Set the precision of a real number to n bits
Width (n) SETW (N) Set field width to n bits
Fill (c) Setfill (c) Set the fill-in character C
SETF () Setiosflags () Set the output format state, in parentheses should give the format state, the content is the same as the control setiosflags in parentheses, as shown in table 13.5
UNSETF () Resetioflags () Terminates the output format state that has been set, and the contents should be specified in parentheses

The flow member function SETF and the parameters in the control setiosflags parentheses represent the format state, which is specified by the format flag. The format flag is defined as an enumeration value in class iOS. Therefore, when referencing these format flags, precede the class name iOS and the domain operator "::". The format flags are shown in table 13.5.

table 13.5 Formatting flags for formatting states
Format Flags function
Ios::left Output data aligned to the left in a wide range of fields
Ios::right The output data is aligned to the right within the wide range of the domain
Ios::internal The sign bit of a number is left-aligned within the width of the field, the value is right-aligned, and the middle is filled with padding characters
iOS::d EC Set the cardinality of an integer to 10
Ios::oct Set the cardinality of an integer to 8
Ios::hex Set the cardinality of an integer to 16
Ios::showbase The cardinality of the Force output integer (octal number starts with 0, hexadecimal number starts with 0x)
Ios::showpoint Force the DOT and mantissa of the output floating point 0
Ios::uppercase In uppercase notation in scientific notation format e and in hexadecimal output letters
Ios::showpos Show "+" for positive numbers
Ios::scientific Floating-point numbers are exported in scientific notation format
Ios::fixed Floating-point numbers output in fixed-point format (fractional form)
Ios::unitbuf Flush all streams after each output
Ios::stdio Clear stdout after each output, stderr

[Example 13.3] output data with the Flow control member function.
  1. #include <iostream>
  2. Using namespace std;
  3. int main( )
  4. {
  5. int a=
  6. cout. SETF(iOS:: Showbase); Show cardinality symbol (0x or)
  7. cout<<"Dec:"<<a<<endl; //output A in decimal form by default
  8. cout. UNSETF(iOS::d ec); //Terminating decimal format settings
  9. cout. SETF(iOS:: Hex); //Set the status to hexadecimal output
  10. cout<<"hex:"<<a<<endl; //Output A in 16 binary form
  11. cout. UNSETF(iOS:: Hex); //terminating hexadecimal formatting
  12. cout. SETF(iOS:: Oct); //Set the status to output in octal
  13. cout<<"Oct:"<<a<<endl; //Output A in eight binary form
  14. cout. Unseft(iOS:: Oct);
  15. char *pt="China"; //pt pointing to the string "China"
  16. cout. Width(ten); //Specify a domain width of
  17. cout<<pt<<endl; //output string
  18. cout. Width(ten); //Specify a domain width of
  19. cout. Fill(' * '); //Specify blanks to fill with ' * '
  20. cout<<pt<<endl; //output string
  21. double Pi=22.0/7.0; //Output PI value
  22. cout. SETF(iOS:: Scientific); //designation with scientific notation output
  23. cout<<"pi="; //Output "pi="
  24. cout. Width(+); //Specify a domain width of
  25. cout<<pi<<endl; //Output PI value
  26. cout. UNSETF(iOS:: Scientific); //terminating scientific notation status
  27. cout. SETF(iOS:: fixed); //Specify output in fixed-point form
  28. cout. Width(N); //Specify a domain width of
  29. cout. SETF(iOS:: Showpos); //Positive output "+" number
  30. cout. SETF(iOS:: internal); //digits appear on the left
  31. cout. Precision(6); //Reserved decimal places
  32. cout<<pi<<endl; //Output pi, note the position of the number "+"
  33. return 0;
  34. }
The operating conditions are as follows:
dec:21 (decimal form)
hex:0x15 (16 binary form, beginning with x)
oct:025 (octal form, with opening)
China (field width is)
China (field width is, blank is filled with ' * ')
pi=**3.142857e+00 (exponential output, domain wide, default decimal)
+***3.142857 (decimal output, precision, leftmost output number "+")

a few notes on the program:
1) The member function width (n) and the control character SETW (n) are only valid for the first output item thereafter. Such as:
cout. Width (6);
cout <<20 <<3.14<<endl;
Output is 203.14

At the first output entry 20 o'clock, the field width is 6, so there are 4 spaces before 20, and width (6) has no effect at output 3.14, which is output by the system's default field width (output by the actual length of the data). If it is required to output data in the same field as the specified width n output, you cannot call width (n) only once, and you must call width (N&GT) before outputting each item, which is done in the program above.

2) in table 13.5, the output format status is divided into 5 groups, each group can only choose one (such as Dec, Hex and Oct only one, they are mutually exclusive). After setting the output format state with the member function SETF and the control setiosflags, if you want to change to another state of the same group, you should call the member function UNSETF (corresponding to the member function self) or resetiosflags (corresponding to the control character Setiosflags) , the state of the original setting is terminated first. Then you can see this in the program by setting other states. Program at the beginning although not with the member function self and the control setiosflags set the DEC output format state, but the system is specified by default Dec, so to change to hex or Oct, you should also first use the UNSETF function to terminate the original settings. If you delete lines 7th and 10th in the program, although the hex and Oct formats are set with the member function setf in line 8th and 11th, the hex and Oct settings do not work because the DEC format is not terminated, and the system still outputs in decimal form.

Similarly, the call to the UNSETF function on line 8th of the program is also indispensable.

3) When you use the SETF function to format the state, you can include two or more format flags, since these formatting flags are defined as enumeration values in the iOS class, each with a binary representation, so you can use the bitwise OR operator "|" Combine multiple format flags. If the 5th and 6th lines can be replaced by the following line:
COUT.SETF (ios::internal I ios::showpos); Contains two status flags, with "|" Combination

4) can be seen: control of the output format can be used either as a control (example 13.2), or with the cout flow of the relevant member functions (such as example 13.3), the role of the two are the same. The control character is defined in the header file Iomanip, so you must include the Iomanip header file when you use the control. The member functions of the cout stream are defined in the header file iostream, so only the header file iostream is included, not iomanip. Many program personnel feel that it is easy to use a control character and can use multiple controls consecutively in a cout output statement. Reproduced from the network

C + + formatted output, C + + output format control

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.