- Asked 2/10
- 2073 Shrawan · 4 marks
- 2070 Asar · 1+3 marks
Manipulators available in C++
Short answer
Manipulators are functions that change stream format when inserted/extracted. Common ones: endl (newline), setw(n), setfill(ch), setprecision(n), dec/oct/hex, left/right, fixed/scientific. They are chainable, e.g., cout << setw(10) << x.
What do you mean by manipulators? Explain different manipulators available in C++.
Manipulators
Manipulators are special functions or objects used with the insertion (<<) or extraction (>>) operator to change the format of I/O. They can be chained in one statement, e.g. cout << setw(8) << x << endl;. Those taking arguments need <iomanip>.
Common manipulators
| Manipulator | Purpose |
|---|---|
endl | Inserts newline and flushes the buffer |
ends | Inserts null character |
flush | Flushes the output buffer |
dec, oct, hex | Sets number base |
setw(n) | Sets field width |
setfill(ch) | Sets fill character |
setprecision(n) | Sets precision |
setiosflags(f) / resetiosflags(f) | Sets / clears format flags |
left, right | Justification |
fixed, scientific | Floating point format |
showpoint, showpos, showbase | Display options |
ws | Skips leading whitespace on input |
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setfill('.') << left << setw(10) << "Pen"
<< right << setw(6) << 25 << endl;
cout << fixed << setprecision(2) << 3.14159 << endl;
cout << hex << showbase << 255 << endl;
return 0;
}
Output:
Pen...........25
3.14
0xff
The fill character set by setfill('.') stays in effect until it is changed.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Consensus; highly available service · Distributed System, chapter 6