仅使用iostream和iomanip库时引用C ++中的ios库

问题描述

我想问一个关于C ++中的ios库的问题。

我是C ++的初学者,正在学习输入/输出流-iostream

考虑以下代码:

#include <iostream>
#include <iomanip> // required for manipulators

int main () {
    std::cout << "no boolalpha - default (10 == 10): " << (10 == 10) << std::endl;
    std::cout << "no boolalpha - default (10 == 20): " << (10 == 20) << std::endl;

    // set to true/false formatting
    std::cout << std::boolalpha;
    std::cout << "boolalpha (10 == 10): " << (10 == 10) << std::endl;
    std::cout << "boolalpha (10 == 20): " << (10 == 20) << std::endl;

    // setf method
    std::cout.setf(std::ios::boolalpha);
    std::cout << "boolalpha (10 == 10): " << (10 == 10) << std::endl;
    std::cout << "boolalpha (10 == 20): " << (10 == 20) << std::endl;

    // reset to default 0/1
    std::cout << std::resetiosflags(std::ios::boolalpha);
    std::cout << "Default (10 == 10): " << (10 == 10) << std::endl;
    std::cout << "Default (10 == 20): " << (10 == 20) << std::endl;
    return 0;

我知道我可以通过将std::boolalpha插入std::out中来切换输出流以显示布尔输出。

但是,我也可以使用setf方法来切换标志:

std::cout.setf(std::ios::boolalpha);

但是,尽管我还没有在头文件中导入setf库,但提供给ios方法的参数引用了ios库类。

此代码仍然有效。

为什么这样做?我以为我需要声明<ios>头文件来使此工作有效,尽管代码可以完美地编译。

ios library hierarchy,for reference

解决方法

<iostream>标头包含<ios>reference

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...