Namespaces
Variants

std:: boolalpha, std:: noboolalpha

From cppreference.net
< cpp ‎ | io ‎ | manip
Input/output manipulators
Floating-point formatting
Integer formatting
Boolean formatting
boolalpha noboolalpha
Field width and fill control
Other formatting
Whitespace processing
Output flushing
Status flags manipulation
Time and money I/O
(C++11)
(C++11)
(C++11)
(C++11)
Quoted manipulator
(C++14)
定义于头文件 <ios>
std:: ios_base & boolalpha ( std:: ios_base & str ) ;
(1)
std:: ios_base & noboolalpha ( std:: ios_base & str ) ;
(2)
1) 在流 str 中启用 boolalpha 标志,如同调用 str. setf ( std:: ios_base :: boolalpha )
2) 禁用流 str 中的 boolalpha 标志,如同调用 str. unsetf ( std:: ios_base :: boolalpha )

std::boolalpha 是一种 I/O 操纵器,因此可以通过诸如 out << std :: boolalpha 这样的表达式调用,其中 out std::basic_ostream 类型的对象;或者通过诸如 in >> std :: boolalpha 这样的表达式调用,其中 in std::basic_istream 类型的对象。

目录

参数

str - I/O 流的引用

返回值

str (指向经过操作后的流的引用)。

示例

#include <iostream>
#include <sstream>
int main()
{
    // boolalpha 输出
    std::cout << "默认 true: " << true << '\n'
              << "默认 false: " << false << '\n'
              << std::boolalpha 
              << "boolalpha true: " << true << '\n'
              << "boolalpha false: " << false << '\n'
              << std::noboolalpha 
              << "noboolalpha true: " << true << '\n'
              << "noboolalpha false: " << false << '\n';
    // boolalpha 解析
    bool b1, b2;
    std::istringstream is("true false");
    is >> std::boolalpha >> b1 >> b2;
    std::cout << '"' << is.str() << "\" 解析为: "
              << std::boolalpha << b1 << ' ' << b2 << '\n';
}

输出:

default true: 1
default false: 0
boolalpha true: true
boolalpha false: false
noboolalpha true: 1
noboolalpha false: 0
"true false" parsed as: true false

另请参阅

清除指定的 ios_base 标志
(函数)
设置指定的 ios_base 标志
(函数)
提供用作布尔值 true false 名称的字符串
( std::numpunct<CharT> 的虚受保护成员函数)