🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

C++ Manipulators

C++ Manipulators

1. std::setw(int width)

The setw manipulator sets the field width for the next input or output operation. It is particularly useful for formatting output columns.


#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::setw(10) << "Name" << std::setw(10) << "Age" << std::endl;
    std::cout << std::setw(10) << "John" << std::setw(10) << 25 << std::endl;
    std::cout << std::setw(10) << "Alice" << std::setw(10) << 30 << std::endl;

    return 0;
}

2. std::setprecision(int n)

The setprecision manipulator sets the precision (number of decimal places) for floating-point numbers.


#include <iostream>
#include <iomanip>

int main() {
    double pi = 3.1415926535;

    std::cout << std::setprecision(4) << pi << std::endl;  // 3.142
    std::cout << std::setprecision(2) << pi << std::endl;  // 3.1

    return 0;
}

3. std::fixed and std::scientific

These manipulators control the format of floating-point numbers. std::fixed forces fixed-point notation, while std::scientific forces scientific notation.


#include <iostream>
#include <iomanip>

int main() {
    double number = 12345.6789;

    std::cout << std::fixed << number << std::endl;      // 12345.678900
    std::cout << std::scientific << number << std::endl;  // 1.234568e+04

    return 0;
}

4. std::setfill(char c)

The setfill manipulator sets the fill character used in field width.


#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::setfill('*') << std::setw(10) << "Hello" << std::endl;  // *****Hello

    return 0;
}

5. std::left, std::right, and std::internal

These manipulators control the alignment of output within a field. std::left (default) left-aligns the output, std::right right-aligns it, and std::internal aligns the sign of the value to the left and the digits to the right.


#include <iostream>
#include <iomanip>

int main() {
    int number = -42;

    std::cout << std::setw(5) << std::left << number << std::endl;    // -42
    std::cout << std::setw(5) << std::right << number << std::endl;   //   -42
    std::cout << std::setw(5) << std::internal << number << std::endl;  // -  42

    return 0;
}

6. std::boolalpha and std::noboolalpha

These manipulators control the output of boolean values. std::boolalpha sets the output to display true or false, while std::noboolalpha uses the numeric representation.


#include <iostream>
#include <iomanip>

int main() {
    bool flag = true;

    std::cout << std::boolalpha << flag << std::endl;      // true
    std::cout << std::noboolalpha << flag << std::endl;     // 1

    return 0;
}

These manipulators can be used individually or combined to control the formatting of output in a variety of ways. They are often used with the insertion (<<) and extraction (>>) operators to customize the appearance of data in the console or other streams.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.