Cout formatting output details

Source: Internet
Author: User
// Use SETF and other library functions // use // using namespace STD when using stream manipulation; // all the following SETF () have corresponding unsetf () the namespace used to cancel setting // All setiosflags () can be canceled with resetiosflags () // The flag fmtflags can use ios_base: Or IOs: int laneri = 12345; double lanerd = 56789; // 1. Set integer hexadecimal output // reload 1: fmtflags ios_base: SETF (fmtflags _ mask); // reload 2: fmtflags ios_base :: SETF (fmtflags _ mask, fmtflags _ unset); // when using overload 1, you must first cancel the current base, then you can set a new base. // when you use the overload 2 function, set the second parameter to the current base. Or, if you do not know the current base, set it to ios_base :: Basefield clears the current // all possible base signs // usable: IOS: Dec, IOS: Oct, IOS: Hex, IOS :: basefield (= dec | Oct | HEX) cout. unsetf (IOs: Dec); // equivalent to 1 cout. SETF (IOs: Hex); cout. SETF (IOs: Hex, ios_base: basefield); // equivalent 2 cout <laneri <Endl; cout <setiosflags (IOs: Hex) <laneri <Endl; // equivalent 3 cout <STD: Hex <laneri <Endl; // equivalent value 4 // the input/output operator can also have an equivalent effect (the namespace uses STD:, otherwise there will be extra characters ), note that this method is not only effective for the sentence // 2. It displays the leading hexadecimal character (0, 0x) cout. SETF (IOs: showbase); cout <Setiosflags (IOs: showbase) <laneri <Endl; cout <STD: showbase <laneri <Endl; // 3. Use scientific notation // only valid for the variable whose data type is decimal (or the literal value is decimal) // affect precision (see the description of precision) // It has an impact on iOS: fixed (For details, refer to fixed instructions), but it will not be affected by fixed cout. SETF (IOs: Scientific); cout <lanerd <Endl; cout <setiosflags (IOs: Scientific) <lanerd <Endl; cout <STD :: scientific <lanerd <Endl; // 4. Set the floating point/fixed point display mode for decimal places. // mainly depends on precision (see the description of precision) // when IOS is set :: IOS:: Fixed will be affected, and STD: fixed will not cout. SETF (IOs: fixed); cout <lanerd <Endl; cout <setiosflags (IOs: fixed) <lanerd <Endl; cout <STD :: fixed <lanerd <Endl; // 5. Set the display precision of the fractional data type, which is affected by scientific and fixed. // when fixed | scientific is set, precision (n) indicates the fixed display of N decimal places after the decimal point // when not set (fixed & Scientific), precision (n) indicates a fixed display of N digits // where, when the number of digits m is less than N, and showpoint is not set, only the M-bit integer is displayed. For example: precision (3), 12-> 12 // where, it is an integer and the number of BITs P is greater than N. Whether showpoint is set or not, it is rounded to scientific notation. Example: precision (3), 1234-> 1.23e + 003 cout. precision (3); cout <lanerd <Endl; cout <setprecision (3) <3.1415926 <Endl; // 6. display the decimal point of the variable of the floating point type. // if it is an integer, use the scientific notation when it is greater than the precision width. If it is smaller than the precision, add 0 after the decimal point, displayed when the value is equal to precision // decimal point but no decimal point // example: no fixed, precision (6): 1234567-> 1.23457e + 006; 12345-> 12345.0; 123456-> 123456. // set fixed, precision (6): 1234567-> 1234567.000000; 12345-> 12345.000000; 123456-> 123456.000000 cout. SETF (IOs: showpoint); cout <setiosflags (IOs: showpoint) <lanerd <Endl; cout <STD: showpoint <lanerd <Endl; // 7. Set the minimum display width on the screen. // the actual number of characters must be greater than or equal to this number. If the value is less than this number, use fill () other Placeholders are filled by the configured characters. // Note: The width setting is only valid for the next "<" output. // For example, cout <SETW (10) <right <"laner" <"Linke"; only "laner" occupies 10 characters, and Linke is not a cout. width (12); // cout <SETW (12) <3.14 <Endl; // 8. The display alignment mode is left aligned with cout by default. SETF (IOs: right); cout <setiosflags (IOs: Right) <laneri <Endl; cout <STD: Right <6.28 <Endl; // 9. Fill in characters when the display width is insufficient. The default value is ''cout. fill ('*'); cout <setfill ('$') <laneri <Endl; ========================================================== ==============================================/// appendix: ios_base :: fmtflags ========================================== ====================================

Cout formatting output details

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.