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 use with cout for output formatting.
Types of Manipulators
There are various types of manipulators classified on the basis type of entity they manipulate:
Output Stream Manipulators
These manipulators format how data is shown on the screen.
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
C++
#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;
}
OutputHello
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
C++
#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 x
Output
c1: s, c2:
3. Boolean Manipulators
These manipulators control how boolean values (true
/false
) are displayed.
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
C++
#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;
}
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
C++
#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;
}
5. 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
C++
#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;
}
Advantages of Manipulators
- Better Control Over Output Formatting
- Improved Readability of Output
- Easier Boolean and Number Base display
- Cleaner and Shorter Code
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems