Formatted output--PART2

Source: Internet
Author: User

/*
The use of member functions in the iOS class to control the IO format always requires a separate statement, not directly embedded into the IO statement, which is inconvenient, so C + + provides an operator to control the IO format. The operator is divided into two types with and without parameters, with the definition of the parameter in the header file Iomanip, without the parameter definition in the header file iostream

first look at the flow operators defined in the header file iostream without parameters
Boolalpha output bool value (i.e. true or false output) using text
fixed fixed-point format output, that is, the floating-point number in the form of a decimal point
scientific The floating-point number by scientific notation
Dec uses decimal for input and output
The OCT uses octal for input and output
Hex is 16 binary for input and output
internal output format is center aligned
right-aligned output format
left-aligned output format
showpos plus number in front of positive numbers
Showpoint shows the end of the 0
showbase display binary, that is, add 0x before the hexadecimal number, add 0 before the octal number
Uppercase Uppercase Display
unitbuf The buffer is emptied after each output operation, such as cout<<unitbuf<< "Test" << "File"; buffers are flushed two times
SKIPWS ignores whitespace characters in the input stream, that is, when the characters we enter are preceded by spaces, they are automatically ignored when stored
The above 15 are standard controls that automatically call the SETF () function and provide the correct parameters, so you can use the

Noboolalpha
Noshowpos
Noshowpoint
Noshowbase
Nouppercase
Nounitbuf
NOSKIPWS
indicates that the flag bit Ios::basefield, Ios::adjustfield, Ios::floatfield can clear the corresponding 8 flag bits, the remaining 7 flag bits can be cleared by UNSETF (), You can also clear the corresponding flag with the above 7 commands

ws skips input whitespace for input
Endl outputs a newline character and refreshes the output stream for output
ends outputs a null character, usually used to end a string, for output
Flush flushes the output stream for output

note: The difference between null and ' \ s ': ' \ n ' and the null value are 0, but '% ' refers to an empty string that is used to determine the end of the string , and Null refers to a null pointer, with

Next, take a look at the parameter flow operator defined in header file Iomanip
SETW (n)             with the iOS class member function width (), set the field width for output
setprecision (n)     member functions Precision () in the iOS class for output  
setbase (n)     Sets the cardinality of an integer to n (n desirable 0 or 10 for decimal, 8 for octal, 16 for 16, and 0 for default) for input and output

resetiosflags (Fmtflag) clears the specified flag bit, multiple with "|" Separated for input and output  
*/

#include <iostream>
using namespace std;
int main ()
{
int x;
cin>>hex>>x; Input in hex (+)
cout<<x<<endl; Output by default in decimal
cin>>x; Because the previous Cin>>hex will automatically call SETF (Ios::hex), and SETF () will remain in effect, the hexadecimal input is still used (
cout<<x<<endl; Output by default in decimal
cin>>oct>>x; This will automatically call SETF (Ios::oct|ios::basefield), with octal input (+)
cout<<x<<endl; Output by default in decimal

cout<< ' a ';
cout<< ' B ' <<ends; Ends for outputting a null character null
cout<< ' C ' <<endl; Endl for outputting a newline character and refreshing the output stream (ab c)

bool b=123;
cout<<boolalpha<<b<<endl; True
cout<<noboolalpha<<b<<endl; 1

return 0;
}

Formatted output--PART2

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.