iomanip of C++ Standard Library

Posted by alexhard on Wed, 31 Jul 2019 11:30:14 +0200

Links to the original text: https://www.cnblogs.com/lsgxeva/p/7696410.html

https://www.cnblogs.com/lsgxeva/p/7696410.html

 

istream & istream::get(char *, int, char = '\n');
istream & istream::getline(char *, int, char = '\n');
Function: Extract a specified number of strings from the text and add an empty character at the end of the string array.
Difference: get() does not extract termination characters from the stream, termination characters are still in the input stream. getline() extracts termination characters from the stream, but termination characters are discarded.

 

C++ Language
Header file: # include < iomanip >
Description: It's the I/O flow control header file, just like the formatted output in C.

 

number Method and description
1 setiosflags - It is used to set format flags.
2 resetiosflags - Used to reset format flags.
3 setbase - It is used to set the basefield flag.
4 setfill - It is used to set fill characters
5 setprecision - It is used to set decimal precision.
6 setw - It is used to set the field width.
7 get_money - It is used to get the value of money.
8 put_money - It is used to set the value of the calculated currency.
9 get_time - It is used to get dates and times.
10 put_time - It is used to place (or set) dates and times.

 

Controlling, Controlling, Complying, Controlling and Controlling Make use of
 dec  Setting integers to decimal
 hex Setting integers to hexadecimal
 oct Setting integers to octal
 setbase(n) Set the integer to n (n=8,10,16)
 setfill(n)

Set character padding, c can be character constant or character variable

 setprecision(n) Set the effective number of floating-point numbers to n-bit
 setw(n) Set the field width to n bits
 setiosflags(ios::fixed) Set floating-point numbers to display in fixed decimal digits
 setiosflags(ios::scientific)   Setting Floating Points in Scientific Numbering
 setiosflags(ios::left) Output left alignment
 setiosflags(ios::right) Output right alignment
 setiosflags(ios::skipws) Neglect leading space
 setiosflags(ios::uppercase) Output E and X in uppercase or lowercase by scientific counting.
 setiosflags(ios::showpos) Display "+" number when output positive number
 setiosflags(ios::showpoint) Mandatory display of decimal points
 resetiosflags() 

Terminate the output format status that has been set and specify the content in parentheses

 

It should be noted here that the default number of significant digits is set precision (6), i.e. the number of digits added up before and after decimal points is six significant digits (note rounding).
In addition, scientific counting output E and hexadecimal output default is lowercase, to replace capitals need to add uppercase
The setw(n) setting width, if the actual width is larger than the set width, then the setw function will fail at this time.

 

#include <iostream>
#include <iomanip>
#include <fstream>

int main()
{
    // The prefix 0 denotes the octal prefix 0x denotes the hexadecimal system without the prefix denotes the decimal system.
    int a = 123;
    double pi = 22.0/7.0;

    // setbase(n) Sets the integer to n (n=8,10,16)
    // oct octal dec decimal hex hex
    // setiosflags(ios::showbase) Displays the prefix of the decimal system
    // Number default decimal display output
    std::cout << a << std::endl;
    std::cout << "oct: " << std::showbase << std::setbase(8) << a << " " << std::oct << a << std::endl;
    std::cout << "dec: " << std::showbase << std::setbase(10) << a << " " << std::dec << a << std::endl;
    std::cout << "hex: " << std::showbase << std::setbase(16) << a << " " << std::hex << a << std::endl;

    // setprecision(n) Sets the effective number of floating-point numbers to n-bit
    // The default number of valid digits is 6 digits, i.e. set precision (6), i.e. the number of digits added up before and after decimal points is 6 valid digits (note rounding)
    std::cout << pi << std::endl;
    std::cout << std::setprecision(12) << pi << std::endl;

    // setfill(n) Sets character padding, c can be character constant or character variable
    // setw(n) sets the field width to n bits. If the actual width is larger than that set, the setw function is invalid at this time, only for the next first output item.
    // setiosflags(ios::left) output left alignment
    // setiosflags(ios::right) Output Right Alignment Default Right Alignment
    std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << pi << std::endl;
    std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << std::right << pi << std::endl;
    std::cout << std::setfill('*') << std::setw(20) << std::setprecision(12) << std::left << pi << std::endl;

    // setiosflags(ios::fixed) Sets floating-point numbers to display in fixed decimal digits
    std::cout << std::fixed << std::setprecision(12) << pi << std::endl;

    // Setiosflags (ios:: science) Sets floating-point numbers to represent scientific counting output E and hexadecimal output by scientific counting. By default, uppercase is added to replace capitals with lowercase.
    std::cout << std::scientific << std::setprecision(12) << pi << std::endl;
    std::cout << std::scientific << std::uppercase << std::setprecision(12) << pi << std::endl;

    // resetiosflags() terminates the output format status that has been set, and the contents should be specified in parentheses
    std::cout << std::setiosflags(std::ios::scientific) << std::setprecision(12) << pi << "   " << std::resetiosflags(std::ios::scientific) << pi << std::endl;

    system("pause");
    return 0;
}

 

 

Operation results:

 

Topics: iOS