Tag Archives: cout

Format control of cout cout.width () and cout.fill ()

When I was doing C++ high-precision template output today, I found that the high-precision template output used cout.width() and cout.fill() in order to output the four-digit Numbers stored in each unit
We looked for information about cout.width() and Cout.fill ()
About cout. Width () :

a, control int width() will be used to adjust the width of the field. Since width is a member function, it will be called by an object. For example, cout.width() will display the current field width, which defaults to 0, and cout.width(3) will set the field width to 3. Note how C++ accommodates fields by assigning them just the right width to accommodate fields, so the default width in C++ is 0 to accommodate all fields.
b, width default alignment is right, that is, if cout.width(12) if the field is not so wide, the left side of the field will be filled with Spaces to reach the width of 12 fields.
c. Also note that width only affects the next output after it is set. After the output of the next field, subsequent fields are restored to their default values, such as cout.width(12); cout< < 2< < 3; Output 2 will be displayed as the width of 12 fields, but 3 will be displayed as the default.
d, int width() will reverse the previous field width value when it is called.
About cout. The fill () :
The member function fill() can be used to change the fill characters, such as cout.fill(‘ *’), which fills in the blank with *.
But be warned: the fill function will remain in effect after it is set, unless it is reset. This is quite different from width(). Width only affects the next output after it is set. After the next output, the following fields are restored to the default value of 0.
And cout. The fill (‘ * ‘) returns the set ‘*’ before the fill characters’ so if cout< < Fill (‘*’) will print this space
So the output of the following program is:
123456789
****** 123 // (note the space between asterisks and Numbers)

#include <iostream>
using namespace std;

void main()
{
cout<<"123456789"<<endl;
cout.width(8);
cout<<cout.fill('*')<<123<<endl;
} 

C++ also has a setw() function similar to cout.width()
These two functions do the same thing. Both control the width of the output character field and are not filled with Spaces. In C++, setw(int n) is used to control the output interval.
for example: cout< < ‘s'< < setw(8)< < ‘a'< < endl;
is displayed on the screen with 7 Spaces between
s a
s and a, and setw() only affects the output immediately following it. In the above example, means ‘a’ occupies 8 positions, and the insufficient space is filled.
If the input exceeds the length set by setw(), the actual length is output.
setw() defaults to Spaces, which can be filled with other characters using setfill().
Such as cout. The fill (‘ * ‘); After cout< < ‘s’ & lt; < setw(8)< < ‘a'< < endl; Output S ******a with seven asterisks between S and A
When setw(n) is used to set the output width, the default is right alignment. If you want it to be left aligned, you just insert STD ::left, and again, right aligned just insert STD ::right, but right aligned is the default and you don’t have to declare it explicitly.
In c++, the following functions are interusable:
setw or width()
setfill or fill()
setprecision or precision()
where the preceding set* requires header #include< iomanip>
 
 
There are some other format controls, just to mention:
Ios ::dec in decimal notation integer
ios::hex in hexadecimal notation
ios::hex in hexadecimal notation
ios::hex in hexadecimal notation
setfill(*) set padding characters *
setprecision(n) set display decimal precision n
setw(n) set field width n characters
setiosflags(ios::fixed) fixed floating point display
The index of setiosflags(ios::scientific) indicates that
setiosflags(ios::left) is left-align
setiosflags(ios::right) right-align
setiosflags(ios::skipws) ignores the leading blank
setiosflags(ios::uppercase) and outputs
Setiosflags (ios:: Lowercase) lowercase hexadecimal number output
Ios ::dec represents integers in decimal
ios::hex represents integers in hexadecimal
ios::oct represents integers in decimal
ios::showbase adds a prefix for integers in decimal
ios::internal inserts a number of padding characters in the middle of the symbol bit and value to align both ends of the string
ios::left inserts padding characters at the end of the string to align the string to the left
The padding characters are inserted before the string to cause the string to come right aligned
ios::boolalpha will represent values of the bool type as true or flase, Instead of 1 or 0
the ios: : fixed will be treated as character points according to the common fixed point format (not scientific notation)
the ios: : scientific will be treated as character points according to the scientific notation (with index domain)
the ios: : showpoint in decimal floating-point said forced insert decimal point (the default is don’t show the decimal floating-point representation of the integer)
the ios: : showpos forced to add in front of the positive number +
the ios: : skipws Empty the cache
in ios::unitbuf after an insert (each output) operation