![]() |
VOOZH | about |
A manipulator is a special function in C++ that is used with input/output streams (like cin, cout ) to change the way data is shown or read. We can use manipulators when we want to :
They are most commonly used with cout for output formatting.
There are various types of manipulators classified based on the type of entity they manipulate:
These manipulators format how data is shown on the screen. The following table lists some common output stream manipulators:
Manipulator | What it Does | Example Usage |
|---|---|---|
endl | Inserts a newline and flushes output | cout<<"Hello"<<endl; |
flush | Flushes the output buffer immediately | cout<<flush; |
setw(n) | Sets width of the next output field (spaces used for padding) | cout<<setw(10)<<42; |
setprecision(n) | Sets the number of digits after decimal point | cout<< setprecision(3)<<3.14159; |
fixed | Uses fixed-point notation for floating numbers. | cout<<fixed<<setprecision(2)<<3.14; |
scientific | Uses scientific notation (exponential) | cout<< scientific << 1234.5; |
showpoint | Always shows the decimal point (even if no digits after decimal) | cout<< showpoint<< 2.0; |
noshowpoint | Hides unnecessary decimal point | cout<< noshowpoint<< 2.0; |
Example:
Hello 42 3.14 3.142 3.142e+00 4.200e+01
These manipulators help control how data is read from input streams. Following table lists some common input stream manipulators:
Manipulator | What it Does | Example Usage |
|---|---|---|
ws | Skips all leading whitespace characters (spaces, tabs, newlines) before reading input | cin>>ws>>someString; |
noskipws | Stops skipping whitespace (reads every character including spaces) | cin>> noskipws >> ch; |
Example:
Input
s xOutput
c1: s, c2: These manipulators control how boolean values (true/false) are displayed. Following table lists some common boolean manipulators:
Manipulator | What it Does | Example Usage |
|---|---|---|
boolalpha | Prints true or false instead of 1 or 0. | cout<< boolalpha<< true; |
noboolalpha | Prints 1 or 0 instead of true/ false | cout<< noboolalpha<< true; |
Example:
true 1
These control the alignment of output and how signs are shown. Following table lists some common alignment and sign manipulators:
Manipulator | What it Does | Example Usage |
|---|---|---|
left | Left-aligns output in the given width | cout<< left<< setw(10) << 42; |
right | Right-aligns output (default for numbers) | cout<< right<< setw(10)<< 42; |
internal | Places the sign to the left, padding after the sign | cout<< internal << setw(10) << -42; |
showpos | Shows a + sign for positive numbers | cout << showpos << 42; |
noshowpos | Hides the + sign for positive numbers | cout<< noshowpos <<42; |
Example:
42 42 +42 42
These manipulators change the number base used for integer output. Following table lists some common base manipulators:
Manipulator | What it Does | Example Usage |
|---|---|---|
dec | Prints numbers in decimal (base 10) | cout << dec << 255; |
hex | Prints numbers in hexadecimal (base 16) | cout << hex << 255; |
oct | Prints numbers in octal (base 8) | cout << oct <<255; |
Example:
2a 42 52