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 :
- Format numbers
- Set precision (decimals)
- Align text
- Change number base (like decimal to hexadecimal)
- Control spacing
They are most commonly used with cout for output formatting.
Types of Manipulators
There are various types of manipulators classified based on the type of entity they manipulate:
1. Output Stream Manipulators
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:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// Output a new line and flush the stream
cout << "Hello" << endl;
// Set width to 10 for the next output
cout << setw(10) << 42 << endl;
// Set precision to 3 for floating-point numbers
cout << setprecision(3) << 3.14159 << endl;
// Use fixed-point notation
cout << fixed << 3.14159 << endl;
// Use scientific notation
cout << scientific << 3.14159 << endl;
// Show the decimal point even for whole numbers
cout << showpoint << 42.0;
return 0;
}
Output
Hello
42
3.14
3.142
3.142e+00
4.200e+012. Input Stream Manipulators
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:
#include <iostream>
using namespace std;
int main() {
char c1, c2;
// Input skips whitespace by default
cin >> c1;
// Input the next character without skipping whitespace
cin >> noskipws >> c2;
cout << "c1: " << c1 << ", c2: " << c2;
return 0;
}
Input
s xOutput
c1: s, c2: 3. Boolean Manipulators
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:
#include <iostream>
using namespace std;
int main() {
bool value = true;
// Display boolean as true/false
cout << boolalpha << value << endl;
// Display boolean as 1/0
cout << noboolalpha << value;
return 0;
}
Output
true 1
4. Alignment and Sign Manipulators
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:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n = 42;
// Align output to the left
cout << left << setw(10) << n << endl;
// Align output to the right
cout << right << setw(10) << n << endl;
// Show positive sign for numbers
cout << showpos << n << endl;
// Don't show positive sign for numbers
cout << noshowpos << n;
return 0;
}
Output
42
42
+42
425. Base Manipulators
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:
#include <iostream>
using namespace std;
int main() {
int n = 42;
// Output in hexadecimal base
cout << hex << n << endl;
// Output in decimal base
cout << dec << n << endl;
// Output in octal base
cout << oct << n;
return 0;
}
Output
2a 42 52