Open In App

Manipulators in C++

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Manipulators are helping functions that can modify the input or output stream. They can be included in the I/O statement to alter the format parameters of a stream. They are defined inside <iomanip> and some are also defined inside <iostream> header file.

For example, if we want to print the hexadecimal value of 100 then we can print it as:

cout << setbase(16) << 100

Types of Manipulators

There are various types of manipulators classified on the basis type of entity they manipulate:

1. Output Stream Manipulators

Output stream manipulators are used to control and format the output stream, such as setting the width, precision, or alignment of printed data. They allow for a better presentation of output.

Following table lists some common output stream manipulators:

ManipulatorDescriptionHeader File
endlInserts a newline and flushes the output stream.iostream
flushFlushes the output stream manually.iostream
setw(x)Sets the width of the next output field to x.iomanip
setprecision(x)Sets the precision for floating-point numbers to x.iomanip
fixedDisplays numbers in fixed-point notation.iomanip
scientificDisplays numbers in scientific notation.iomanip
showpointForces the display of the decimal point.iomanip
noshowpointHides the decimal point unless necessary.iomanip

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;
}

Output
Hello
        42
3.14
3.142
3.142e+00
4.200e+01

2. Input Stream Manipulators

Input stream manipulators are used to modify the behaviour of the input stream. They help in processing input efficiently, such as skipping unnecessary whitespaces with ws.

Following table lists some common input stream manipulators:

ManipulatorDescriptionHeader File
wsSkips leading whitespaces in the input stream.iostream
noskipwsDisables skipping of leading whitespaces.iostream

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

Boolean manipulators are used to format boolean values in output. They allow displaying boolean values as true or false or as 1 and 0, depending on the requirement.

Following table lists some common boolean manipulators:

ManipulatorDescriptionHeader File
boolalphaDisplays true or false for boolean values.iostream
noboolalphaDisplays 1 or 0 for boolean values.iostream

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;
}

Output
true
1

4. Alignment and Sign Manipulators

These manipulators control how text and numbers are aligned or how their signs are displayed in the output.

Following table lists some common alignment and sign manipulators:

ManipulatorDescriptionHeader File
leftAligns output to the left.iomanip
rightAligns output to the right.iomanip
internalAligns signs and base prefixes to the left.iomanip
showposDisplays a + sign for positive numbers.iostream
noshowposHides the + sign for positive numbers.iostream

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;
}

Output
42        
        42
+42
42

5. Base Manipulators

Base manipulators are used to format numbers in different bases, such as decimal, hexadecimal, or octal. They help in representing numbers in a way suited to specific applications.

Following table lists some common base manipulators:

ManipulatorDescriptionHeader File
hexFormats output in hexadecimal base.iostream
decFormats output in decimal base.iostream
octFormats output in octal base.iostream

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;
}

Output
2a
42
52


Next Article
Practice Tags :

Similar Reads