std::setbase, std::setw , std::setfill in C++
The useful input/output manipulators are std::setbase, std::setw and std::setfill. These are defined in and are quite useful functions.
- std::base : Set basefield flag; Sets the base-field to one of its possible values: dec, hex or oct according to argument base.
std::setbase (int base); decimal : if base is 10 hexadecimal : if base is 16 octal : if base is 8 zero : if base is any other value.
- Implementation : This code uses the std::setbase manipulator to set hexadecimal as the base field selective flag.
- std::setw : Set field width; Sets the field width to be used on output operations. Behaves as if member width were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams).
std::setw (int n); where n is Number of characters to be used as field width.
- Note: Here argument given to setw() is minimum width of the output, so if we have output with more width than argument’s value then output width will not be exactly the argument given to setw() but will be equal to the output size(i.e., the output will not get truncated). Default width of setw() is 0.
- std::setfill : Set fill character; Sets c as the stream’s fill character. Behaves as if member fill were called with c as argument on the stream on which it is inserted as a manipulator (it can be inserted on output streams).
std::setfill (char_type c); char_type is the type of characters used by the stream (i.e., its first class template parameter, charT).
- Implementation :
Pattern using std::setw and std::fill :
* ** *** **** *****
This article is contributed by Shubham Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
My Personal Notes arrow_drop_up
Please Login to comment...