iomanip


Header: <iomanip>

The <iomanip> header is part of the C++ Standard Library that provides a set of manipulators for formatting input and output operations. These manipulators allow for precise control over the formatting of data when using I/O streams. The <iomanip> header is particularly useful for tasks such as setting field widths, precision for floating-point numbers, alignment, and base for integer output.

Key Characteristics

Example 1: Basic Formatting with Field Width and Alignment

#include <iostream>
#include <iomanip>
#include <string>

int main() {
    std::string name = "John";
    int age = 30;
    double salary = 55000.50;

    std::cout << std::left << std::setw(15) << "Name" 
              << std::setw(10) << "Age" 
              << std::setw(15) << "Salary" << std::endl;

    std::cout << std::left << std::setw(15) << name
              << std::setw(10) << age
              << std::setw(15) << salary << std::endl;

    std::cout << std::right << std::setw(15) << "Alice"
              << std::setw(10) << 25
              << std::setw(15) << 60000.75 << std::endl;

    return 0;
}

Explanation:

Example 2: Precision and Fixed-Point Notation

#include <iostream>
#include <iomanip>
#include <cmath>

int main() {
    double pi = M_PI;
    double small = 0.0000123;
    double large = 123456789.0;

    std::cout << "Default output:" << std::endl;
    std::cout << pi << std::endl << small << std::endl << large << std::endl;

    std::cout << "\nFixed, 2 decimal places:" << std::endl;
    std::cout << std::fixed << std::setprecision(2);
    std::cout << pi << std::endl << small << std::endl << large << std::endl;

    std::cout << "\nScientific, 3 significant digits:" << std::endl;
    std::cout << std::scientific << std::setprecision(3);
    std::cout << pi << std::endl << small << std::endl << large << std::endl;

    return 0;
}

Explanation:

Example 3: Base Manipulation for Integer Output

#include <iostream>
#include <iomanip>

int main() {
    int number = 255;

    std::cout << "Decimal: " << number << std::endl;
    std::cout << "Hexadecimal: " << std::hex << number << std::endl;
    std::cout << "Octal: " << std::oct << number << std::endl;

    std::cout << "\nFormatted output:" << std::endl;
    std::cout << std::setfill('0');
    std::cout << "Decimal: " << std::dec << std::setw(10) << number << std::endl;
    std::cout << "Hexadecimal: 0x" << std::hex << std::setw(8) << number << std::endl;
    std::cout << "Octal: 0" << std::oct << std::setw(11) << number << std::endl;

    return 0;
}

Explanation:

Example 4: Custom Fill Characters and Boolean Formatting

#include <iostream>
#include <iomanip>

int main() {
    bool t = true;
    bool f = false;

    std::cout << "Default boolean output:" << std::endl;
    std::cout << t << " " << f << std::endl;

    std::cout << "\nAlphabetic boolean output:" << std::endl;
    std::cout << std::boolalpha << t << " " << f << std::endl;

    std::cout << "\nCustom-width fields with different fill characters:" << std::endl;
    std::cout << std::setfill('.') << std::left << std::setw(10) << "Name"
              << std::setfill('_') << std::right << std::setw(5) << "Age" << std::endl;

    std::cout << std::setfill('.') << std::left << std::setw(10) << "John"
              << std::setfill('_') << std::right << std::setw(5) << 30 << std::endl;

    return 0;
}

Explanation:

Example 5: Monetary Formatting

#include <iostream>
#include <iomanip>
#include <locale>

int main() {
    std::locale::global(std::locale("en_US.UTF-8"));
    std::cout.imbue(std::locale());

    long double price = 1234567.89;

    std::cout << "Default monetary output:" << std::endl;
    std::cout << std::showbase << std::put_money(price) << std::endl;

    std::cout << "International monetary output:" << std::endl;
    std::cout << std::showbase << std::put_money(price, true) << std::endl;

    return 0;
}

Explanation:

Additional Considerations

  1. Persistence: Many manipulators (like setw) only affect the next output operation, while others (like setprecision) persist until changed.

  2. Performance: Excessive use of manipulators can impact performance in I/O-intensive operations.

  3. Locale Dependency: Some formatting (like monetary) depends on the current locale settings.

  4. Stream State: Manipulators can affect the state of the stream, which might persist across multiple output operations.

  5. Combining with <iostream>: <iomanip> is typically used in conjunction with <iostream> for comprehensive I/O formatting.

Summary

The <iomanip> header in C++ provides a powerful set of tools for formatting input and output:

These manipulators are essential for creating well-formatted, readable output in C++ programs. They are particularly useful in scenarios where precise control over the appearance of data is required, such as in financial applications, tabular data presentation, or when interfacing with systems that expect specific data formats.

When using <iomanip>, it's important to understand which manipulators have a persistent effect on the stream and which are temporary. Proper use of these formatting tools can significantly enhance the readability and professionalism of program output, making it easier for users to interpret the data presented.

Related

Previous Page | Course Schedule | Course Content