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.
Syntax :
std::setbase (int base); decimal : if base is 10 hexadcimal : 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.
// CPP Program to illustrate// std::setbase manipulator#include <iostream>#include <iomanip> // std::setbaseintmain(){// set base to hexadecimalstd::cout << std::setbase(16);// displaying 255 in hexadecimalstd::cout << 255 << std::endl;// set base to Octalstd::cout << std::setbase(8);// displaying 255 in Octalstd::cout << 255 << std::endl;return0;}chevron_rightfilter_noneOutput:
ff 377
-
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).
Syntax :std::setw (int n); where n is Number of characters to be used as field width.
// CPP Program to illustrate// std::setw manipulator#include <iostream>#include <iomanip> // std::setwintmain(){// set width of 10std::cout << std::setw(10);std::cout << 100 << std::endl;std::string str ="GFG";// set width of 12std::cout << std::setw(12);std::cout << str << std::endl;return0;}chevron_rightfilter_noneOutput:
100 GFG -
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.
Example:
#include <iostream>#include<iomanip>#include<string>usingstd::cout;usingstd::string;usingstd::endl;intmain() {string temp="Hello setw";cout<<std::setw(5)<<temp<<endl;return0;}chevron_rightfilter_noneOutput:
Hello setw
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).
Syntax :
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 :
// CPP Program to test std::setfill manipulator#include <iostream>#include <iomanip> // std::setfill, std::setwintmain(){// setfill is x and width is set as 10std::cout << std::setfill('x') << std::setw(10);std::cout << 77 << std::endl;std::string str ="Geeks";// setfill is G and width is set as 10// And std::left is used set str to left sidestd::cout << std::left << std::setfill('G') << std::setw(10);std::cout << str << std::endl;return0;}chevron_rightfilter_noneOutput:
xxxxxxxx77 GeeksGGGGG
Pattern using std::setw and std::fill :
// CPP Program to print // pattern using std::setw and std::fill #include <iostream> #include <iomanip> // std::setfill, std::setw int main() { int n = 5; for (int i = 1; i <= n; i++) { std::cout << std::left << std::setfill(' ') << std::setw(n); std::cout << std::string(i, '*') << std::endl; } } |
Output:
* ** *** **** *****
This article is contributed by Shubham Rana. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- Iterate over characters of a string in C++
- Python program to add two binary numbers
- Difference between int (*p)[3] and int* p[3]?
- 3-way comparison operator (Space Ship Operator) in C++ 20
- How to generate a vector with random values in C++?
- Difference between std::set vs std::vector in C++ STL
- Vector of Maps in C++ with Examples
- Data Conversion in C++
- Movie tickets Booking management system in Python
- Order of execution in initializer list in C++
- Unusual behaviour with character pointers
- Header Guard in C++
- Can we write a print statement within if parentheses?
- If memory allocation using new is failed in C++ then how it should be handled?
- Draw an ellipse divided by straight line into two colored part in C++ Graphics
Improved By : Aakash_Panchal

