CppStream Manipulators

Stream Manipulators

Raw std::cout output is functional but unformatted — numbers print with however many decimal digits they happen to have, and there’s no built-in column alignment. Stream manipulators, mostly from the <iomanip> header, let you control exactly how data is formatted as it’s written to a stream.

std::endl vs \n

Both end the current line, but they aren’t quite the same.

Manipulator

Behavior

std::endl

Inserts a newline and forces the output buffer to flush immediately.

"\n"

Inserts a newline character only — no forced flush.

Prefer \n in most code
Flushing has a real performance cost, especially inside loops that print many lines. Since buffers are flushed automatically when the program ends (or fills up), most modern C++ style guides recommend `"\n"` by default, and reserve `std::endl` for the specific moments you need output visible immediately — for example, right before a long-running operation or a crash-prone section of code.
Formatting floating-point numbers

<iomanip> provides std::fixed and std::setprecision() to control how many decimal places a floating-point value shows.

CPP
#include <iostream>
#include <iomanip>

int main() {
    double pi = 3.14159265358979;

    std::cout << pi << std::endl;                          // default formatting
    std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
    return 0;
}
3.14159
3.14

std::fixed switches to fixed-point notation (as opposed to scientific notation), and std::setprecision(2) then fixes the number of digits after the decimal point. Once set, both settings stay in effect for all subsequent output until changed again.

Column alignment with setw() and setfill()

std::setw(n) sets the minimum width of the next value printed, padding it with spaces (or a custom fill character) to reach that width. std::setfill(c) changes the padding character used.

CPP
#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::setw(6) << 42 << std::endl;
    std::cout << std::setfill('0') << std::setw(6) << 42 << std::endl;
    return 0;
}
    42
000042
Worked example: a formatted table

Combining std::setw, std::fixed, and std::setprecision produces neatly aligned, readable tabular output — useful for printing reports or numeric results.

CPP
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

int main() {
    std::vector<std::pair<std::string, double>> prices = {
        {"Apples", 1.5},
        {"Bread", 3.257},
        {"Milk", 2.1},
    };

    std::cout << std::left << std::setw(10) << "Item"
              << std::right << std::setw(8) << "Price" << std::endl;

    std::cout << std::fixed << std::setprecision(2);
    for (const auto& [name, price] : prices) {
        std::cout << std::left << std::setw(10) << name
                   << std::right << std::setw(8) << price << std::endl;
    }

    return 0;
}
Item        Price
Apples       1.50
Bread        3.26
Milk         2.10
Quick reference
  • std::fixed — use fixed-point (not scientific) notation for floating-point numbers.

  • std::setprecision(n) — set the number of digits shown (after the decimal point, when combined with std::fixed).

  • std::setw(n) — set the minimum field width for the next inserted value only.

  • std::setfill(c) — set the character used to pad values to their field width.

  • std::left / std::right — control alignment within the field width.